Maintenance is always a big part of every XenDesktop environment. There may be times throughout the course of the year where you need to “take-down” a desktop group in order to prevent users from connecting to certain virtual desktops. You then perform maintenance on the virtual desktops and bring the desktop group back online to allow access again. All of this can be accomplished by enabling and disabling the desktop group within the Desktop Group properties of the Delivery Services Console.

This blog will focus on how to automate the enabling and disabling of a desktop group within a PowerShell script. The more of the maintenance process we can automate, the less time the virtual desktops are inaccessible. The sample scripts I present here build on a lot of the concepts from the earlier blogs in this series. So if you are new to PowerShell and want some extra information, feel free to check out the earlier blogs as well.

This is the fourth 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 automate the creation of a new desktop group via PowerShell. For a complete list of topics that I will be covering in this blog series, see the bottom of this article.

PowerShell script for disabling a desktop group
The sample script below demonstrates how to disable a desktop group within XenDesktop.

#*************************************************************************************
#Disable 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>

#Get a particular desktop group
$xdgroup = Get-XdDesktopGroup -Name $strDesktopGroupName -AdminAddress $strDDCAddress -HostingDetails

#Disable desktop group
$xdgroup.Enabled = $<span class="code-keyword">false</span>

#Apply the change to the DDC
Set-XdDesktopGroup $xdgroup

#Verify the update
echo $xdgroup.Enabled

After executing the script, you can open the Delivery Services Console and verify that the desktop group was disabled as shown below:

PowerShell script for enabling a desktop group
The sample script below demonstrates how to enable a desktop group within XenDesktop.

#*************************************************************************************
#Enable 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>

#Get a particular desktop group
$xdgroup = Get-XdDesktopGroup -Name $strDesktopGroupName -AdminAddress $strDDCAddress -HostingDetails

#Enable desktop group
$xdgroup.Enabled = $<span class="code-keyword">true</span>

#Apply the change to the DDC
Set-XdDesktopGroup $xdgroup

#Verify the update
echo $xdgroup.Enabled

After executing the script, you can open the Delivery Services Console and verify that the desktop group was enabled as shown below:

Analyzing the PowerShell Scripts
Both of the scripts are pretty much identical except for the one line that either enables or disables the desktop group. So we’ll talk about both scripts at the same time.

The Get-XdDesktopGroup cmdlet is used to get a reference to an existing desktop group. This cmdlet returns a XdDesktopGroup object which we are storing into a variable called $xdgroup. We are passing two parameters to the Get-XdDesktopGroup cmdlet (the name of the desktop group we want to get and the DDC address so the script knows where to look for it). We are also passing an additional flag called -HostingDetails. The hosting details flag allows you to retrieve the Virtual Machine name for each of the virtual desktops in the desktop group as part of the returned object. With this flag included, you get both the Active Directory machine name and Virtual Machine name for each virtual desktop in the group. Without this flag, the Get-XdDesktopGroup cmdlet would just return the Active Directory machine name for each virtual desktop within the desktop group.

If you are learning this script, I would recommend that you display the current value of the $xdgroup variable after executing the Get-XdDesktopGroup cmdlet to make sure that it retrieved the desktop group properly. To do this, execute the command below. One of the properties that is displayed is the Enabled setting that indicates if the desktop group is currently enabled or disabled.

echo $xdgroup

Enabling or disabling the desktop group is pretty straightforward. You just set the Enabled property of the desktop group to true or false. To indicate a boolean in PowerShell (True or False), you just use the variables $true or $false.

Finally, to commit the change back to the Desktop Delivery Controller, you use the Set-XdDesktopGroup cmdlet.

Wrap-up
This article explains how to enable and disable a desktop group within XenDesktop via a PowerShell script. This script is one of the simpler scripts that I will present in the blog series, but it shows a couple great examples on how to use the XenDesktop PowerShell objects for setting properties and committing them back to the farm.

In the next blog, we will look at updating the idle pool settings for a desktop group as part of a PowerShell script – stay tuned!

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. We hope to see you there!

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