Restart Unregistered XenDesktop VMs with Powershell

January 09, 2013  •  6 Comments

Having been running a XenDesktop/XenServer based VDI environment for a bit over three years now many growing pains and problematic issues have been worked through.  One which has irritatingly stuck around is sporadically desktops will power on, but never register with the Desktop Delivery Controller leaving then in an unusable state wasting resources.  After much procrastination during which unregistered desktops were manually dealt with (when someone remembered to check...), I finally took the time to put together a Powershell script to automate (with Task Scheduler) the clean-up process.

Tested against XenDesktop 5.6 with XenServer 6.0.2.

#########################################################################################
##                                                                                      #
## Based upon sample code from the following Citrix blog articles                       #
## http://blogs.citrix.com/2012/12/05/xendesktop-powershell-sdk-script-examples-part-1/ #
## http://blogs.citrix.com/2012/10/27/xendesktop-monitoring-desktop-availability/       #
##                                                                                      #
## This script will connect to your XenDesktop environment and provide a list of        #
## desktop(s) that are powered on, but unregistered or it will perform a forced restart #
## on the VM(s).  To modify this behavior just uncomment the appropriate line.          #
##                                                                                      #
## Finally the script will send an email with a listing of all restarted desktops.      #
##                                                                                      #
## Pre-reqs: Powershell, Citrix DDC SDK                                                 #
##                                                                                      #
## Use of this script is at your own risk, not responsible for data loss/corruption or  #
## dragons invading your data center.                                                   #
##                                                                                      #
## Michael Davis - 01/09/13                                                             #
##                                                                                      #
#########################################################################################

##Load Citrix Modules
asnp Citrix.*

#Variables for email
[string[]]$recipients = "recipient@domain.com"
$fromEmail = "sender@domain.com"
$server = "emailserver.domain.com"
$date= Get-Date

##Check for VMs on and unregistered
$unregisteredVMs = (Get-BrokerDesktop -MaxRecordCount 5000 | ? {($_.PowerState -eq 'On') -and ($_.RegistrationState -eq 'Unregistered')} | select MachineName)
[string]$emailVMs = (Get-BrokerDesktop -MaxRecordCount 5000 | ? {($_.PowerState -eq 'On') -and ($_.RegistrationState -eq 'Unregistered')} | select HostedMachineName | ft -wrap -autosize | Out-String)

IF (!$unregisteredVMs) {
##Send all clear email
[string]$emailBody = "There were no powered on desktops in an unregistered state."
send-mailmessage -from $fromEmail -to $recipients -subject " XenDesktop Daily Check - $date" -body $emailBody -priority High -smtpServer $server
}
Else {
##If powered-on and unregistered, perform a forceful restart
foreach ($unregisteredVM in $unregisteredVMs)
{
 New-BrokerHostingPowerAction -MachineName $unregisteredVM.MachineName -Action Reset
 #Write-Host "Hello, I am unregistered: $unregisteredVM"
}
 
#Send an email report of VMs to be restarted due to being in an unregistered state
[string]$emailBody = "The following desktops were forcefully restarted due to not registering with the DDCs in a timely manner. `n $emailVMs"
send-mailmessage -from $fromEmail -to $recipients -subject " XenDesktop Daily Check - $date" -body $emailBody -priority High -smtpServer $server
}


Comments

Andrew(non-registered)
Very nice...I've been procrastinating just the same and sat down today intent on writing a script to do just this. Thanks a million!
Peter G(non-registered)
Thanks, script works a treat for me. To those asking questions about disconnection sessions, you can deal with those with a GPO, just set them to log off after disconnected for more than a set period of time, works best for me that way.
Michael Davis
You can install and run the DDC SDK from any system that can communicate with the DDC services. I just found it easiest to install on the DDC so that I wasn't having to rely upon a separate system to run the automated task.

Hope it works out for you.

Michael
Chad(non-registered)
Michael, thanks for the info...

I'll do some testing and let you know how it turns out. I think this is extremely helpful.

Is it necessary / best to install and run the PS DDC SDK from the DDC itself? That's where I'm installing and testing, but I did wonder if I could just as easily run these tools from a workstation.

Thanks again,
Chad
Michael Davis
You would need to modify it a bit to restart VMs which are disconnected.

Modifying the lines under "Check for VMs on and unregistered" to read *should* do the trick, but I haven't tested to confirm.

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

I've got a folder called 'scripts' on my DDC that I store this and other scripts in. If the Desktop Delivery Controller SDK is not already installed, you can download and install it from - http://community.citrix.com/cdn/xd/sdks/. Once it's installed you should be good to go for running this script (though I would recommend testing it with the restart functionality disabled initially).
No comments posted.
Loading...

Archive
January (5) February March (1) April May June July August September October November (1) December (1)
January February March (1) April May June July August September October (1) November (4) December
January (1) February (1) March (2) April (1) May June July August September October November December
January February March April May June July August September October November December
January February March April May June July August September October November December