Sunday, January 26, 2014

Listing and removing tags from Citrix Xendesktop VDIs

I recently had to use tags as a way to filter a Machine and User policy for a select group of users with Citrix XenDesktop VDIs across 2 different pools.  Adding the tags were easy from within the GUI but I quickly noticed that removing them wasn’t.  The Citrix XenDesktop Desktop Studio allows you to tag one desktop as such as such:

image image

… as well as tag multiple desktops by shift or CTRL selecting them:

image image

The challenge here is that while you can remove the tag by selecting a VDI and selecting Edit tags:

image

You cannot do the same when you select more than one desktop:

image

Seeing how there wasn’t an obvious way to do this in the GUI, I went ahead and turned to PowerShell cmdlets.  First off, the cmdlets that manipulate tagging are as follows:

Get-BrokerTag
http://support.citrix.com/static/kc/CTX127254/help/Get-BrokerTag.html

Add-BrokerTag
http://support.citrix.com/static/kc/CTX127254/help/Add-BrokerTag.html

New-BrokerTag
http://support.citrix.com/static/kc/CTX127254/help/New-BrokerTag.html

Remove-BrokerTag
http://support.citrix.com/static/kc/CTX127254/help/Remove-BrokerTag.html

Rename-BrokerTag
http://support.citrix.com/static/kc/CTX127254/help/Rename-BrokerTag.html

As I did not find a way to display the tags for a virtual desktop in the Desktop Studio, I used the following to retrieve the list of desktops with the tag Opt:

Get-BrokerDesktopGroup

The problem with simply using the cmdlet above is that you get quite a bit of information as each desktop has a lot of attributes:

image

… so to make the list a little easier to read, you can use the following cmdlet to list the desktop and a subset of the attributes associated with it:

Get-BrokerDesktopGroup -Tag Opt | FL HostedMachineName,CatalogName,DesktopGroup,Tags,Uid

… or it can be further simplified with:

Get-BrokerDesktopGroup -Tag Opt | FL HostedMachineName,Uid

image

The attribute we’re interested in is the Uid because the Remove-BrokerTag expects that value to be provided when removing the tag from the desktop.  Executing the following cmdlet will list the Uid(s) of desktops that are currently tagged with the tag Opt:

Get-BrokerDesktopGroup -Tag Opt | FL Uid

image

With the Uid(s) listed, you can use the cmdlet:

Remove-BrokerTag Opt -Desktop 296

… to remove the tag from the desktop:

image

Notice how desktop with the Uid 296 is no longer listed:

image

I’m sure many may be wondering now whether we can pipe the Get-BrokerTag cmdlet into the Remove-BrokerTag cmdlet so we don’t have to manually remove the tag one desktop at a time but unfortunately, it doesn’t look like the Remove-BrokterTag accepts bundled Uid(s) that the Get-BrokerTag cmdlet returns.  What I would suggest if you have a lot of desktops is to dump the Uid output into a spreadsheet and manipulate it from there to make this easier rather than manually typing a line for each desktop.

If anyone knows of a way to achieve the same results more efficiently, please feel free to post down in the comments.

No comments: