Self Service Tasks: Remove all disconnected Citrix Sessions with Orchestrator

If you are running Citrix or Microsoft RDS you know that occasionally, or sometimes frequently, user sessions get stuck and need an admin to kick them off. Or you have a ton of “disconnected” users taking up licenses preventing new sessions from starting, this generally requires an admin to login to the Citrix console, whether installed locally or on the server and find the disconnected users and disconnect them. Or they can run a PowerShell script to disconnect the users, which again either requires the admin to be logged in on the server or have all the necessary components installed on their local machine to run it remotely. Either way this is a manual task and requires someone with enough permissions to resolve the issue.

So what I have done is added a Self Service Request Offering in the Service Manager Self-Service Portal that kicks off an Orchestrator Runbook that perform the action for us. This allows even our Tier 1 helpdesk techs to kick off all disconnected users and also creates a service request which we can use for reporting on how many times we have to kick users off.

Before we dig into the meat and potatoes we need the Citrix SDK for our version of Citrix.

For Xenapp/Xendesktop 7+
http://www.citrix.com/go/citrix-developer/xenapp-xendesktop-developer-community/power-shell-xenapp7.html

For Xenapp 6.5
http://www.citrix.com/downloads/xenapp/sdks/powershell-sdk.html

Now install whichever version you have on your Orchestrator Runbook server(s). Note, it must be installed on all runbook servers if you have more than one.

Once that is installed you can run the following commands in PowerShell to verify installation:

add-pssnapin citrix*
get-pssnapin citrix*


For testing, you could enter a remote PowerShell session to your Citrix server or farm and run the commands and they will work, and then be surprised when they don’t work in Orchestrator. Let me save you the trouble. Since Orchestrator is running in PowerShell 2.0 not all the commands work the same as if you were running them in PowerShell on your server or desktop. I found that you have to use invoke-command with -scriptblock in Orchestrator Run .Net Script, when trying to run Citrix commands from the Run .Net Activity.

invoke-command -session $session -scriptblock {
script here
}

To start a remote session, in Orchestrator, with our Citrix server or farm we will do the following in the Run .Net Activity:
$RunAsAccount = “subscribed domainusername”

$RunAsPass = “subscribed, encrypted password”

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

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

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

Then we will invoke the command and run our script:

invoke-command -session $session -scriptblock {

add-pssnapin citrix*

Get-xasession | where { ($_.state -eq “disconnected”)} | stop-xasession

}

remove-pssession $session
 
This is what it looks like in the Run .Net Activity.

This command will get all sessions where the state equals disconnected and remove the sessions.

This is what my runbook looks like.

Once that was created I went into Service Manager and created a request offering and put it under a Service Offering that only our internal IT members can access.
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.
You can download the runbook here