PowerShell script to detect if you are running in Citrix XenApp Environment

PowerShell script to detect if you are running in Citrix XenApp Environment

It is very usual that you find enterprises supporting Physical and Virtual environments. When you have to setup an automation or an application customization across such an enterprise, you often require to determine the environment where the user is logged in and apply appropriate settings.

Say you want to set a user environment variable ONLY when an application is launched on the local physical machines but not when launched in Citrix environment.  If you have to achieve this with a script that runs automatically for all users, you need to know “if you are running in Citrix XenApp Environment”.  This would come handy in application integration environments using AppSense, etc.,.  Here are couple of options that I have for validating if a user is running in Citrix XenApp environment or not.

Determining if you are in Citrix XenApp Environment using %sessionname% environment variable:

As we know that Citrix XenApp uses ICA protocol that is dependent on the underlying Windows RDP.  When connecting remotely with Remote Desktop Connection, the environment variables CLIENTNAME and SESSIONNAME are added to each process that is started.  The SESSIONNAME variable reports whether the logged on session is running on the physical system locally or via RDP or via Citrix XenApp.  Depending on the environment it returns respective values as shown below:

In Console (on my local system) login: It returns “Console”

image

In RDP (Remote Desktop or Terminal Services session) login: It returns “RDP-TCp#<SessionID>”

image

In Citrix XenApp (ICA session) login: It returns “ICA-TCp#”

image

The Command prompt and PowerShell commands:

[code language=”powershell”]
C:\>echo %sessionname%
Console
C:\>

PS C:\> $env:SESSIONNAME
Console
PS C:\>
[/code]

As these environment variables are added by Windows for each Process/Shell during and after the session creation, it gives a better reliability to build automation around.  Here is a quick PowerShell code  that reads the SESSIONNAME variable and tell where you are running in:

[code language=”powershell”]
switch ($env:SESSIONNAME.ToUpper().Substring(0,3)) {
"CON" { Write-output "You are running in Console environment" }
"RDP" { Write-output "You are running in Remote Desktop (RDP) environment" }
"ICA" { Write-output "You are running in Citrix XenApp environment" }
}

[/code]

Output:

[code language=”powershell”]
PS C:\> powershell C:\Test\MySessionEnvironment.ps1
You are running in Console environment
PS C:\>
[/code]

However, there is also one caveat when relying on SESSIONNAME variable. That is, this environment variable is not inherited by child Explorer.exe Windows and thus their sub-processes.  Please be watchful if you are launching a new Explorer Window in Citrix before running letting your SESSIONNAME dependent automation has run.  Refer to this caveat at MS article:  Clientname and Sessionname enviroment variable may be missing

Hope this comes handy should you are dealing with the similar case to find where your script/application is running to automate other depended tasks.

Other Useful references:

Applying Citrix Policies through AppSense Environment Manager on XenApp 6.x

Leave a Reply

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