Self Service: Remove frozen or hung Citrix Sessions with Orchestrator

There is quite possibly nothing more annoying than having your Citrix session freeze. You’re working doing well, and then out of nowhere you can’t do anything or you leave for a break and come back and cant do anything. You then have to put in a helpdesk request which could take who knows how long to get routed to the right person, or call the helpdesk and hope you get someone that can help you.

From an IT perspective not everyone may have access to reset your Citrix session. If you get someone that can, they either have to have the Citrix console installed locally or have to log on to the server to open the console, find you and then reset your session. This all takes time, even more so if no one is available when you put in your ticket, which slows your workers down and stalls productivity. I am here to say there is an easier way, enter System Center Orchestrator, Service Manager and a little PowerShell too.

Unfortunately there are no free Orchestrator Intergration Packs for Citrix. But if you have read my previous post about removing all disconnected session from Citrix, all we need is the Xenapp PowerShell SDK.

For Xenapp/Xendesktop 7+

For Xenapp 6.5

Remember, if you have more than one runbook server, you’ll need to install it on all of them.

First here is the overview of our Runbook.

As always when doing Request Offerings from Service Manager into Orchestrator, we need a variable that is linked into the Request Template in Service Manager so that Orchestrator can get all the related information from the Service Manager Service Request. In my case I almost always just call it RBAID.

 First I want to get the Related User to the Runbook, for the Object GUID we use the RBAID.

Then we get the user in Service Manager by subscribing to the Related Object GUID from the get Related User Activity.

Then we want to get the Runbook Automation Activity so we can get the Service Request.

Next we get the Service Request via the relationship to the Runbook Automation Activity, we do this by subscribing to the Object GUID from Get Runbook Activity.

And now we get the actual Service Request by subscribing to the Related Object GUID from Get Related Service Request.

Now, we have everything we need to properly complete this request. Lets drag a Run .Net Activity into the designer and set it to PowerShell.

Below is the PowerShell script we use to perform the task.

As you recall from my previous Citrix post, I use subscribed variables for the username and password.

$RunAsAccount = “subscribed variable citrixaccount”

$RunAsPass = “subscribed variable citrixaccountPW”

We then convert the encrypted password to a string.

$pass = $RunAsPass | ConvertTo-SecureString -AsPlainText -Force

Now we create our credentials for the remote session we are about to enter.

$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $RunAsAccount, $pass

We then create our session to our Citrix Server

$session = New-PSSession -computername citrixserver -Credential $credentials

And now our command

invoke-command -session $session -scriptblock {

add-pssnapin citrix*

Get-xasession | where { ($_.accountname -eq “subscribed.Domainsubscribed.UserName”)} | stop-xasession


remove-pssession $session

After some cleanup we update the Service Request depending on success or failure. I will say the failure is limited because we are doing remoting, in this case the only failure is if Orchestrator could not successfully run the command against the server. It does not account for if it was unable to find the correct user.

This runbook is provided as an example and is not production ready, please test in your own environment.  The runbook is provided as is and without warranty.

The runbook can be downloaded from here