Search code examples
powershellcitrixxenapp

Local Powershell script to trigger VBS script inside Citrix instance


I am trying to get a local powershell script to trigger a VBS script inside a citrix instance. The events should be this:

Citrix Instance opening Windows Explorer ----> Network Path of script typed into the windows explorer session

I'm using the WfIcaLib.dll (ICOSDK) that came with the Citrix receiver install. Documentation PDF for the Citrix ICOSDK is available here

So this is the code I'm using, which works PERFECTLY in Powershell command line, but when I use the 32-bit ISE, it does nothing other than telling me the DLL has been loaded. I get no errors, but the Citrix Client never actually opens like it does when I run the same exact commands through Powershell command line.

#load Citrix ICA Client SDK
[System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Citrix\ICA Client\WfIcaLib.dll")

$ICA = New-Object WFICALib.ICAClientClass 
$ICA.Address = "***.***.***.***:****" 
$ICA.Application = "Windows ExplorerFED6" 
$ICA.Username = "******"
$ICA.Domain = "**" 
$ICA.Launch = $true
$ICA.Outputmode = [WfIcaLib.OutputMode]::OutputModeNormal 
$ICA.SetProp("Password", "*********")
$ICA.TWIMode=$true 
$ICA.Connect()

Any ideas?

EDIT: SOLVED - after reopening under 32-bit ISE and getting code to work, I could not run the .ps1 file since it kept defaulting to 64-bit (even if using Open With on 32-bit powershell version). Running the script via command prompt or 32-bit powershell console both worked.

Using any method suggested by Mike Garuccio worked just fine. I will most likely end up using a Task Scheduler to run the script.


Solution

  • It looks like the problem is a versioning one, which you can deal with using start-job (was originally going to do this with a runspace but that's a lot more code for no real benefit). This will start a job to run your code under 32-bit powershell, it should still place any GUI elements or popups on screen but if you need to get any data out from the script later you'll need to receive the job. code would look something like below.

    $SB = {
    [System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Citrix\ICA Client\WfIcaLib.dll")
    
      $ICA = New-Object WFICALib.ICAClientClass 
      $ICA.Address = "***.***.***.***:****" 
      $ICA.Application = "Windows ExplorerFED6" 
      $ICA.Username = "******"
      $ICA.Domain = "**" 
      $ICA.Launch = $true
      $ICA.Outputmode = [WfIcaLib.OutputMode]::OutputModeNormal 
      $ICA.SetProp("Password", "*********")
      $ICA.TWIMode=$true 
      $ICA.Connect()
    }
    
    Start-Job -ScriptBlock $SB -RunAs32
    get-job | Receive-Job
    

    Alternatively, you could also use a simple .bat file as a launcher with something like C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -file yourscript.ps1

    or if you need everything to be wrapped in powershell and the job does not work you can use the solution Here that basically does the same thing but from within powershell (drop the if statement they use tho, just use the stuff contained inside it like below, with any changes you need to make wrt the profile and interactive settings.)

    &"$env:windir\syswow64\windowspowershell\v1.0\powershell.exe" -noninteractive -noprofile -file "C:\Path o\script.ps1" -executionpolicy bypass

    hope one of those works for you!