ICA connections using Powershell – Part 3

In part 1 of this series, I talked about the basics. Part 2 was about the overall session appearance. This part will be about using the events which are available in the ICO SDK. The ICO SDK has a lot of events we can use, but we will be talking about a few so that you get a basic idea on how to use them.

Available Events

First of all, let’s start with the code we use to view all events in the ICAClientClass. We already had the following code:

[System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Citrix\ICA Client\WfIcaLib.dll")
$ICA = New-Object WFICALib.ICAClientClass
$ICA.Address = "XASRV001"
$ICA.Username = "TestUser01"
$ICA.SetProp("Password","MyUsersPassword")
$ICA.Domain = "LAB"
$ICA.Application = ""
$ICA.Launch = $true
$ICA.OutputMode = [WFICALib.OutputMode]::OutputModeNormal
$ICA.DesiredHRes = 1024
$ICA.DesiredVRes = 768
$ICA.DesiredColor = [WFICALib.ICAColorDepth]::Color16bit
$ICA.Connect()

This will connect an ICA session. To view all event available, we can use the following code:

[System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Citrix\ICA Client\WfIcaLib.dll")
$ICA = New-Object WFICALib.ICAClientClass
$ICA | Get-Member -MemberType Event

This will output the following:

Name                       MemberType Definition
----                       ---------- ----------
OnACRReconnected           Event      WFICALib._IICAClientEvents_OnACRReconnectedEventHandler OnACRReconnected()
OnACRReconnectFailed       Event      WFICALib._IICAClientEvents_OnACRReconnectFailedEventHandler OnACRReconnectFail...
OnCGPDisconnect            Event      WFICALib._IICAClientEvents_OnCGPDisconnectEventHandler OnCGPDisconnect()
OnCGPReconnect             Event      WFICALib._IICAClientEvents_OnCGPReconnectEventHandler OnCGPReconnect()
OnCGPUnwarn                Event      WFICALib._IICAClientEvents_OnCGPUnwarnEventHandler OnCGPUnwarn()
OnCGPWarn                  Event      WFICALib._IICAClientEvents_OnCGPWarnEventHandler OnCGPWarn()
OnChannelDataReceived      Event      WFICALib._IICAClientEvents_OnChannelDataReceivedEventHandler OnChannelDataRece...
OnClick                    Event      WFICALib._IICAClientEvents_OnClickEventHandler OnClick(System.Int32, System.In...
OnConnect                  Event      WFICALib._IICAClientEvents_OnConnectEventHandler OnConnect()
OnConnectFailed            Event      WFICALib._IICAClientEvents_OnConnectFailedEventHandler OnConnectFailed()
OnConnecting               Event      WFICALib._IICAClientEvents_OnConnectingEventHandler OnConnecting()
OnDisconnect               Event      WFICALib._IICAClientEvents_OnDisconnectEventHandler OnDisconnect()
OnDisconnectFailed         Event      WFICALib._IICAClientEvents_OnDisconnectFailedEventHandler OnDisconnectFailed()
OnDisconnectSessions       Event      WFICALib._IICAClientEvents_OnDisconnectSessionsEventHandler OnDisconnectSessio...
OnDisconnectSessionsFailed Event      WFICALib._IICAClientEvents_OnDisconnectSessionsFailedEventHandler OnDisconnect...
OnICAFile                  Event      WFICALib._IICAClientEvents_OnICAFileEventHandler OnICAFile()
OnICAFileFailed            Event      WFICALib._IICAClientEvents_OnICAFileFailedEventHandler OnICAFileFailed()
OnInitializing             Event      WFICALib._IICAClientEvents_OnInitializingEventHandler OnInitializing()
OnInitialProp              Event      WFICALib._IICAClientEvents_OnInitialPropEventHandler OnInitialProp()
OnLogoffFailed             Event      WFICALib._IICAClientEvents_OnLogoffFailedEventHandler OnLogoffFailed()
OnLogoffSessions           Event      WFICALib._IICAClientEvents_OnLogoffSessionsEventHandler OnLogoffSessions(Syste...
OnLogoffSessionsFailed     Event      WFICALib._IICAClientEvents_OnLogoffSessionsFailedEventHandler OnLogoffSessions...
OnLogon                    Event      WFICALib._IICAClientEvents_OnLogonEventHandler OnLogon()
OnLogonFailed              Event      WFICALib._IICAClientEvents_OnLogonFailedEventHandler OnLogonFailed()
OnPublishedApp             Event      WFICALib._IICAClientEvents_OnPublishedAppEventHandler OnPublishedApp()
OnPublishedAppFailed       Event      WFICALib._IICAClientEvents_OnPublishedAppFailedEventHandler OnPublishedAppFail...
OnReadyStateChange         Event      WFICALib._IICAClientEvents_OnReadyStateChangeEventHandler OnReadyStateChange(S...
OnSessionAttach            Event      WFICALib._IICAClientEvents_OnSessionAttachEventHandler OnSessionAttach(System....
OnSessionDetach            Event      WFICALib._IICAClientEvents_OnSessionDetachEventHandler OnSessionDetach(System....
OnSessionEventPending      Event      WFICALib._IICAClientEvents_OnSessionEventPendingEventHandler OnSessionEventPen...
OnSessionSwitch            Event      WFICALib._IICAClientEvents_OnSessionSwitchEventHandler OnSessionSwitch(System....
OnWindowCloseRequest       Event      WFICALib._IICAClientEvents_OnWindowCloseRequestEventHandler OnWindowCloseReque...
OnWindowCreated            Event      WFICALib._IICAClientEvents_OnWindowCreatedEventHandler OnWindowCreated(System....
OnWindowDestroyed          Event      WFICALib._IICAClientEvents_OnWindowDestroyedEventHandler OnWindowDestroyed(Sys...
OnWindowDisplayed          Event      WFICALib._IICAClientEvents_OnWindowDisplayedEventHandler OnWindowDisplayed(Sys...
OnWindowDocked             Event      WFICALib._IICAClientEvents_OnWindowDockedEventHandler OnWindowDocked()
OnWindowFullscreened       Event      WFICALib._IICAClientEvents_OnWindowFullscreenedEventHandler OnWindowFullscreen...
OnWindowHidden             Event      WFICALib._IICAClientEvents_OnWindowHiddenEventHandler OnWindowHidden(System.In...
OnWindowMaximized          Event      WFICALib._IICAClientEvents_OnWindowMaximizedEventHandler OnWindowMaximized()
OnWindowMinimized          Event      WFICALib._IICAClientEvents_OnWindowMinimizedEventHandler OnWindowMinimized()
OnWindowMoved              Event      WFICALib._IICAClientEvents_OnWindowMovedEventHandler OnWindowMoved(System.Int3...
OnWindowRestored           Event      WFICALib._IICAClientEvents_OnWindowRestoredEventHandler OnWindowRestored()
OnWindowSized              Event      WFICALib._IICAClientEvents_OnWindowSizedEventHandler OnWindowSized(System.Int3...
OnWindowUndocked           Event      WFICALib._IICAClientEvents_OnWindowUndockedEventHandler OnWindowUndocked()

Like I said; a lot of available events. I will focus on the following events:

  • OnConnectFailed
  • OnLogon
  • OnLogonFailed

Attaching to an Event

To attach to an event in PowerShell, you can use the following CmdLet:

Register-ObjectEvent

It will need an input object (the ICAClientClass Object), an event to register and the action to take as parameters. For example, to register to the OnLogon event:

Register-ObjectEvent -InputObject $ICA -EventName OnLogon -Action { Write-Host "Logged on" }

This code will write “Logged on” in the PowerShell console when the OnLogon event is initiated. Pretty easy right?

So the code should look as follows if we add the events:

[System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Citrix\ICA Client\WfIcaLib.dll")
$ICA = New-Object WFICALib.ICAClientClass
$ICA.Address = "XASRV001"
$ICA.Username = "TestUser01"
$ICA.SetProp("Password","MyUsersPassword")
$ICA.Domain = "LAB"
$ICA.Application = ""
$ICA.Launch = $true
$ICA.OutputMode = [WFICALib.OutputMode]::OutputModeNormal
$ICA.DesiredHRes = 1024
$ICA.DesiredVRes = 768
$ICA.DesiredColor = [WFICALib.ICAColorDepth]::Color16bit
Register-ObjectEvent -InputObject $ICA -EventName OnConnectFailed -Action { Write-Host "Connection failed..." }
Register-ObjectEvent -InputObject $ICA -EventName OnLogonFailed -Action { Write-Host "Logon failed..." }
Register-ObjectEvent -InputObject $ICA -EventName OnLogon -Action { Write-Host "User has logged on..." }
$ICA.Connect()

You should see something like this:

PS C:\Users\Administrator> $ICA.Connect()
PS C:\Users\Administrator> User has logged on...

Note that the “User has logged on…” string can appear later, since the Connect() method is asynchronous. Hope this post was usefull for you. In my next post, I will be talking about simulation keyboard and mouse in the ICA session.

Comments
  1. 4 years ago
  2. 4 years ago
    • 2 years ago
  3. 3 years ago
    • 3 years ago
  4. 3 years ago
  5. 2 years ago
    • 2 years ago
      • 2 years ago
        • 2 years ago
  6. 1 year ago
  7. 6 months ago

Leave a Reply

Your email address will not be published. Required fields are marked *

Complete the following sum: * Time limit is exhausted. Please reload CAPTCHA.