When you configure a XenDesktop environment, the primary configuration you are going to make is to create new desktop groups. Desktop Groups define the virtual desktops that you want to make available to users, who can access them, how they are assigned to users, and how many are kept active at any given time. Whenever I’m working within the Delivery Services Console, I spend most of my time looking at the desktop groups and the virtual machines contained within them.
So when you look at automating a XenDesktop environment, being able to automate the creation of a desktop group is typically one of the first things you want to try to do. This article will provide a sample PowerShell script on how to create a new VM-based desktop group for a XenDesktop farm. A VM-based desktop group just means that the virtual desktops (VDAs) are hosted on a hypervisor such as XenServer or Hyper-V. The alternative is a PC-based desktop group where the virtual desktops are hosted on PC appliances.
This is the third 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 second blog, I discussed the XenDesktop PowerShell objects and how you can use them for retrieving the XenDesktop farm properties and other configurations. For a complete list of topics that I will be covering in this blog series, see the bottom of this article.
Before we look at the PowerShell script, it’ll be good to get some information about my environment so you can understand what the script will be doing.
I’m using XenServer as my host infrastructure. My XenServer IP address is 10.10.10.31. All the virtual machines for my XenDesktop environment are hosted on this XenServer. The VM details are as follows:
|Role||OS||VM Name||AD Machine Name||IP Address|
|Domain Controller||Windows Server 2003||W2K3_DC1||dc1||10.10.10.50|
|Desktop Delivery Controller||Windows Server 2003||W2K3_XD4_DDC||ddc||10.10.10.56|
|Virtual Desktops||Windows XP (all)||WINXP_XD4_VDA1
The virtual desktop machines already have the virtual desktop agent software installed, they just need to be added into a desktop group on the Desktop Deliver Controller. I want to create a new desktop group with this configuration:
|Desktop Group Name||Windows XP|
|Desktop Group Description||New desktop group created by PowerShell|
|Assigned Users/Groups||CITRIXLAB\Domain Users|
|Assigned virtual desktops||WINXP_XD4_VDA1
|Peak Idle Count||5 virtual desktops|
|Business Hours Idle Count||3 virtual desktops|
|Out of hours Idle Count||1 virtual desktop|
|Peak Hours Start||8am|
|Peak Hours End||10am|
|Business Hours End||8pm|
|Time Zone||Central Standard Time|
|Business Days||Every day of the week, including Saturday and Sunday|
The sample script below demonstrates how to create a new VM-based desktop group within XenDesktop using the configurations I outlined in the previous section.
After you run the script, check out the Delivery Services Console to make sure the desktop group was added.
Also check out the idle pool settings for the desktop group to make sure those settings were applied properly:
The New-XdDesktopGroup cmdlet is used to create the desktop group within XenDesktop. Typically the way that you want to write a script such as this is to look at what parameters are required by New-XdDesktopGroup and then work backwards to ensure you have all the required parameters covered.
The New-XdDesktopGroup cmdlet needs the following parameters to execute. It supports more parameters than this, but this is what I’m supplying in the script above.
|-Publish||Name to assign to the desktop group|
|-Description||Description of the desktop group|
|-Desktops||The virtual desktops to assign to this desktop group|
|-User||The Active Directory users/groups to assign to this desktop group|
|-HostingSettings||The idle pool settings to use for this desktop group|
|-AssignmentBehavior||How desktops within the group are assigned to users (e.g. “Pooled”)|
|-AdminAddress||The address of the Desktop Delivery Controller to connect to for making this update|
The top part of the PowerShell script is used gather and store the above items into variables so we can pass them into the New-XdDesktopGroup cmdlet. Several of these parameters are just strings and are pretty easy to figure out (-Publish, -Description, -AssignmentBehavior, -AdminAddress). The other parameters are actually looking for particular XenDesktop objects (-Desktops, -User, -HostingSettings).
The -Desktops parameter is looking for a XdHostedMachine object. We are using the Get-XdHostedMachine cmdlet to create that object. The -User parameter is looking for a XdUser object. We are using the New-XdUser cmdlet to create that object. The -HostingSettings parameter is looking for a XdGroupHostingSettings object. We are using the New-XdGroupHostingSettings cmdlet to create that object.
In order to use the Get-XdHostedMachine cmdlet, this cmdlet requires a connection to the XenServer itself. The New-XdHostingServer cmdlet is used to create a connection to XenServer using a supplied XenServer username and password. This “xenserver” object is then passed into the Get-XdHostedMachine cmdlet.
This article explains how to automate the creation of a new desktop group in XenDesktop via PowerShell. As you test this script, I would recommend executing the statements line-by-line to understand what the script is doing. Don’t forget to echo out the value of the variables to see what data they contain. When you create the XenDesktop objects using New-XdUser, Get-XdHostedMachine, New-XdHostingServer, etc., I recommend using get-member to undestand all of the properties and methods that object supports. For example:
If you have any questions or comments about this script, or if you found new ways of using it, feel free to post a comment below. In the next article, I will discuss how to enable and disable a desktop group via PowerShell. Stay tuned!
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!
- Getting Started
- Retrieving the XenDesktop farm properties
- Creating a new desktop group (this one)
- Enabling/disabling a desktop group
- Updating the idle pool settings of a desktop group
- Adding virtual desktops to a desktop group
- Adding AD users and groups to a desktop group
- Disconnecting and stopping virtual desktop sessions
- Restarting virtual desktops and series wrap-up
Ask-the-Architect Site: http://community.citrix.com/p/product-automation#home
Follow Me on twitter: http://twitter.com/citrixedy