The last task that we’ll discuss in this blog series is how to automate the restarting (rebooting) of virtual desktops within XenDesktop. I’ve provided two different scripts in this article – one for restarting a single virtual desktop, the second for restarting multiple virtual desktops at the same time. As you’ll find in the scripts, they look almost identical, making this one of the more easier actions to perform.

This is the 9th and final blog in a series on how to use the XenDesktop 4 PowerShell SDK. In the first blog, I provided info on how to set up your XenDesktop PowerShell environment so that you could run these scripts. If you haven’t done that yet, please visit that article first. In the last blog, I discussed how you can disconnect and stop virtual desktop sessions using PowerShell. For a complete list of topics that I have covered in this blog series, see the bottom of this article.

PowerShell script for restarting a single virtual desktop
The sample script below demonstrates how to restart a single virtual desktop in the XenDesktop farm. Please note the two options provided here for retrieving a reference to the virtual desktop.

#************************************************************
#Restart a single virtual desktop
#************************************************************

#Add the XenDesktop snap-in to the current PowerShell session
Add-PSSnapin <span class="code-quote">"XdCommands"</span>

#Set up variables <span class="code-keyword">for</span> the script
$strDDCAddress = <span class="code-quote">"10.10.10.56"</span>
$strDesktopADName = <span class="code-quote">"xpvda1"</span>            #Active Directory machine name
$strDesktopVMName = <span class="code-quote">"WINXP_XD4_VDA1"</span>    #Virtual Machine name

#Option 1 - retrieve a particular virtual desktop by Active Directory machine name
$desktop = Get-XdVirtualDesktop -AdminAddress $strDDCAddress -HostingDetails | where { $_.Name -match $strDesktopADName }

#Option 2 - retrieve a particular virtual desktop by Virtual Machine name
$desktop = Get-XdVirtualDesktop -AdminAddress $strDDCAddress -HostingDetails | where { $_.HostingName -match $strDesktopVMName }

#Restart the virtual desktop
Restart-XdVirtualDesktop -force -desktop $desktop

PowerShell script for restarting all virtual desktops from a desktop group
The sample script below demonstrates how to restart all virtual desktops for a desktop group at the same time.

#************************************************************
#Restart all virtual desktops in a desktop group
#************************************************************

#Add the XenDesktop snap-in to the current PowerShell session
Add-PSSnapin <span class="code-quote">"XdCommands"</span>

#Set up variables <span class="code-keyword">for</span> the script
$strDDCAddress = <span class="code-quote">"10.10.10.56"</span>
$strDesktopGroupName = <span class="code-quote">"Windows XP"</span>

#Retrieve all virtual desktops within a desktop group
$desktops = Get-XdVirtualDesktop -AdminAddress $strDDCAddress -Group $strDesktopGroupName -HostingDetails

#Restart all virtual desktops within the group that are currently powered on
Restart-XdVirtualDesktop -force -desktop $desktops

Analyzing the PowerShell Scripts
Both scripts above are almost identical so we’ll discuss them at the same time. The Get-XdVirtualDesktop cmdlet is used to get a reference to one or more virtual desktops. If you view the help on this cmdlet, you’ll find that it provides various switches (-Registered, -Unregistered, -Group) for performing filtering on the virtual desktops that are returned.

In the first script above, I provided two different ways for getting a reference to a single virtual desktop. The first approach looks for the virtual desktop based on the Active Directory machine name. The second approach looks for the virtual desktop based on the Virtual Machine name. You don’t need both of those statements in the script – I just provided both as it could help understand the cmdlet statement better. Feel free to experiment with them to find which works best for you.

The Get-XdVirtualDesktop cmdlet returns a XdVirtualDesktop object (or an array of them if there is more than one returned). You pass this object as a parameter to the Restart-XdVirtualDesktop cmdlet and that’s it! All the specified virtual desktops will be restarted (if they were already running).

Wrap-up
The blog series is now done! If you found the info within this series valuable or if would like to see some particular things on the XenDesktop PowerShell SDK in the future, feel free leave a comment. Be sure to also sign up for the TechTalk we are delivering on Tuesday, August 24 where you can see several of these PowerShell scripts in action!

Upcoming TechTalk
I will be leading a TechTalk with Mike Bogobowicz on Essentials for using Windows PowerShell with XenApp and XenDesktop on Tuesday, August 24 from 2pm to 3pm EST. If you interesting in learning more about these SDKs first hand and want to see the demos in action, you can sign up here. Feel free to also check out Mike’s blog on XenApp 6 PowerShell scripting here. We hope to see you at the TechTalk!

Blogs in this series

Ed York – Senior Architect – Worldwide Technical Readiness
Ask-the-Architect Site: http://community.citrix.com/p/product-automation#home
Follow Me on twitter: http://twitter.com/citrixedy