Monitoring is key in any environment.  IMHO, it is probably the most important part of managing and supporting an environment.  It allows us to react when things go wrong, and ideally, it also allows us to be proactive and address red flags before they become issues.

Typically, when we think about monitoring, we think about network, hardware, performance, resource utilization, capacity, event logs, windows services, licensing and availability of key infrastructure components.  In a XenDesktop environment we also have to worry about desktop availability.

As part of daily operations we should check Desktop Studio or Desktop Director to see number of available desktops, desktops in maintenance mode, unregistered desktops, etc.  This, however, is a very reactive approach, which I’m not a fan of.  Troubleshooting business impacting issues really stinks, so if there is a way to proactively look for potential issues and have an opportunity to address them before they become critical, I’m all for it.  So I put together a PowerShell script that checks the XenDesktop site for potential desktop availability issues and emails me a report of the findings.  I set this report as a scheduled task in Windows so it runs automatically every day.  Now every morning I can check this report in my phone when I wake up and see if I need to log on immediately or I can go about my business as usual.

The script does four things:

  1. Look for pooled VMs that failed to reboot after user logoff and have the “WillShutdownAfterUse” flag set.  If any, reset them.
  2. Look for VMs that are powered on and unregistered.
  3. Look for VMs that are in maintenance mode.
  4. Email summary of unregistered VMs and VMs in maintenance mode.

Here is the script:


# Load Citrix PowerShell modules
Asnp Citrix.*

#Check VMs that failed to reboot
$badVMs = Get-BrokerDesktop -PowerActionPending $false -PowerState On -SummaryState Available -WillShutdownAfterUse $true -MaxRecordCount 5000
If ($badVMs)
   foreach($vm in $badVMs)
      New-BrokerHostingPowerAction -MachineName $vm.HostedMachineName -Action 'Reset'

#Check for VMs in maint mode and unregistered, and send email report
[string[]]$recipients = "",""
$fromEmail = ""
$server = ""
$time = Get-Date –format d

[string]$unregisteredVMs = (Get-BrokerDesktop -MaxRecordCount 5000 | ? {($_.RegistrationState -eq 'Unregistered') -and ($_.PowerState -eq 'On')} | select HostedMachineName,DesktopGroupName,LastDeregistrationReason | ft -wrap -autosize | Out-String)

[string]$maintenanceModeVMs = (Get-BrokerDesktop -MaxRecordCount 5000 | ? {$_.InMaintenanceMode -eq 'True'} | select HostedMachineName,DesktopGroupName,LastDeregistrationReason | ft -wrap -autosize | Out-String)

[string]$emailBody = "UNREGISTERED MACHINES `n`n`n $unregisteredVMs" + "`n`n`n`n" + "MACHINES IN MAINTENANCE MODE `n`n`n $maintenanceModeVMs"

send-mailmessage -from $fromEmail -to $recipients -subject " XenDesktop Daily Check $currentTime" -body $emailBody -priority High -smtpServer $server


One important thing to note…  If you look at the queries, they all have the “MaxRecordCount” option set to 5000.  If you don’t specify this, the query will only return the first 250 results.  Make sure you adjust as needed to cover all desktops in your site.

This should be a pretty good starting point for everyone.  You can modify this script to suit your specific needs, change the format of the report, etc.   For more information on PowerShell in XenDesktop you can check out Getting Started with PowerShell in XenDesktop 5.

For more information on monitoring your Citrix environment, check out Operations Guide: Monitoring.

Happy scripting!


P.S.:  The script can be downloaded here.