PowerShell – Restart Client Citrix ICA session & Print Spooler – Self Service

Our IT Helpdesk often receives calls from users who for a multitude of reasons have issues with their Citrix connection,  more often due to the fact they haven’t turned on their printer before connecting and cant see their local printer etc.

To resolve this we deployed the following self-service script that undertake the following processes to ensure that the user’s Citrix session is re-started in a clean state. In the script you will see I have listed a “YourApplication.lnk” as we have a common application that all users of Citrix use so it calls this by default.

The process is as follows:

1. Notify the user that their Citrix session will be closed.
2. Log out of Citrix session gracefully.
3. Terminate the Citrix ICA client.
4. Restart the Print Spooler service (to resolve any printing related issues).
5. Start the Citrix ICA client.
6. Check if the Citrix client is running and open the default business application.

If the process is unsuccessful or the user does not have the Citrix client installed an email is generated to the IT Helpdesk using their local Outlook client.

The script has been tested with both the legacy Citrix Online Plugin and Citrix Receiver 3.4 Enterprise.


$Prompt = new-object -comobject wscript.shell

# Variables for Citrix
$CitrixPNAPath = Get-Process -Name PNAMain -ErrorAction 'SilentlyContinue' | ForEach-Object { Write-Output $($_.Path) }
$CitrixAgentPath = ($CitrixPNAPath | Split-Path) + "\pnagent.exe"
$CitrixInstalled = Test-Path -Path $CitrixPNAPath
$CitrixShortCut = "$ENV:APPDATA" + "\Microsoft\Windows\Start Menu\Programs\Citrix Applications\Claims Applications\YourApplication.lnk"

# Message body for auto failure message
$MailBody = @"
I am having issues with my Citrix connection at present. I tried running the self service script but it was unable to resolve the problem. Can you please take a look.
- This is an automatically generated email on behalf of the user.

# Send email function
Function Notify-Helpdesk
Start-Process Outlook
$Outlook = New-Object -com Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.importance = 2
$Mail.subject = "Citrix Problems"
$Mail.body = $MailBody
$Mail.To = 'it.helpdesk@yourdomain.domain'

# Detect & Repair Citrix
If ($CitrixInstalled -eq $true)
$PromptWindow = $Prompt.popup('You will now be logged out of Citrix', 0, 'IT Self-Service')
Start-Process -FilePath $CitrixAgentPath -ArgumentList " /LogOff"
sleep 5
Get-Process | Where-Object {
$_.ProcessName -like "*WFICA*"
} | Stop-Process -Force
sleep 10
Restart-Service -Name Spooler -Force
Start-Process -FilePath $CitrixPNAPath
sleep 2
If ((Get-Process -Name PNAMain).responding -eq 'True')
$PromptWindow = $Prompt.popup('Citrix ICA client has been restarted successfully. YourApplication will now launch automatically.', 0, 'IT Self-Service')
invoke-item $CitrixShortCut
$PromptWindow = $Prompt.popup('Citrix ICA client failed to restart successfully. Outlook will now open and attempt to notify IT of your issue via email.', 0, 'IT Self-Service')
$PromptWindow = $Prompt.popup('Citrix ICA client not found. Outlook will now open and attempt to notify IT of your issue via email.', 0, 'IT Self-Service')

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s