Custom Condition Using PowerShell to Check if an ICA Session is a Published Destkop

Recently I had a specific need to identify if a Citrix XenApp session was a published desktop using AppSense Environment Manager. AppSense Environment Manager has built-in conditions to apply to a particular published application, however I needed to know if the session was just a published desktop so this wasn’t going to work. To do this I created the following PowerShell script to be used as a custom condition. I hope this helps anyone else in a similar position.

$sessionid = query session | Select-String -Pattern ">"
$sessionid = $sessionid.ToString()
$sessionid = $sessionid.Substring(9,9)
$sessionid = $sessionid.trim()
$publishedapp = (Get-ItemProperty  HKLM:\SOFTWARE\Citrix\Ica\Session\$sessionid\connection).InitialProgram
If ($publishedapp) {exit (0)} else {exit(1)}

The AppSense User Virtualization service will start then immediately stops

The AppSense User Virtualization service will start then immediately stops with the error message from windows reading as follows:

“The AppSense User Virtualization Service on the Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service.”

All other AppSense services remain running but as a result no AppSense Environment policies are being applied.

The application event log only shows the following message:
Source: AppSense EmCoreService
Event ID:8001
Description: AppSense Environment Manager: Service stopped

After sometime with AppSense technical support the issue was caused by a missing registry key. The key HKLM\Software\Microsoft\Windows NT\CurrentVersion\AeDebug must be present for the AppSense User Virtualisation service to start. As it happens the previously upgraded AppSense Policy Configuration was deleting that key to disable Dr. Watson debugging as per the Microsoft KB article KB188296. To resolve the issue I simply removed the action and started the service.