How to Identify Obsolete Machines in Citrix XenDesktop?

  • 0

How to Identify Obsolete Machines in Citrix XenDesktop?

With this post I share a small PowerShell function, Get-ObsoleteBrokerDesktop, that identifies and returns obsolete desktops with in a Citrix XenDesktop Desktop Group.

What are obsolete desktops? Typically, XenDesktop brokers sessions to virtual machines with Microsoft Windows 7 and an according Computer Account in Active Directory. Over time, due to whatever reason, an Administrator may remove VMs and AD Computer Accounts. XenDesktop isn’t aware of this meaning that XenDesktop simply classifies the according machines as powered off, which is only half the story: these machines are good for nothing.

XenDesktop maintains for each virtual machine an appropriate object in its database. A database object representing a virtual machine contains comprehensive information about a the machine such like the VDA’s version, the associated user name(s), if power managed or not, the Desktop Group it belongs to, and – last but not least – Hypervisor- and Active Directory-related details about the brokered machine’s identity on the Hypervisor/Hosting platform (for example the HostedMachineID) as well as in Active Directory (SID).

From a high level perspective the function Get-ObsoleteBrokerMachine performs the following steps:

  • Get the Id of each VM within the specified Hypervisor path. (The VM-Id corresponds to the HostedMachinId property of a desktop object in XenDesktop)
  • Identify all Machines within the specified Desktop Group that have no matching HostedMachineId in the list of VM-Ids

Please keep in mind that this approach will class physical desktops as obsolete too!

Here we go!

Disclaimer: I hope that the information in this post is valuable to you. Your use of the information contained in this post, however, is at your sole risk. All information on this post is provided “as is”, without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by me. Further, I shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.


  • 3

How to Move Machines Within a Citrix XenDesktop Site?

Recently, I used Citrix XenDesktop 5’s PowerShell interface at a mid-size business in order to transfer machines to a new Machine Catalog and Desktop Group. With this article I want to share a generalized version of that script, MoveXDMachines.ps1, in order to help Admins and Consultants who are stuck in a similar situation. (Please note the disclaimer at the end of this article.)

The initial situation was a typical PoC setup meaning that there were XenDesktop Controller, Web Interface, and SQL Express Database installed on a single Windows Server. There was only one Machine Catalog for physical machines configured. Among the members of this catalog I found only 5 to 10 physical machines and round about 300 VMware vSphere based VMs! With that said it seems quite logical that there was no Connection to the Hosting Infrastructure configured. Thus, referred to this case, it was my task to 1) manually configure the connection to VMware vSphere, 2) manually create a new Machine Catalog for the existing VMs, 3) manually create a new Desktop Group for the power managed VMs, and 4) automatically move the VM-hosted machines from the existing Machine Catalog/Desktop Group to the new Machine Catalog/Desktop Group while keeping existing User-to-VM associations.

From a high level perspective the script MoveMachines.ps1 performs the following steps:

  • Preparation:
    • Load Citrix commands – this is straightforward
    • Get the UID of the target Machine Catalog (with Get-BrokerCatalog)
    • Get the UID of the Hypervisor Connection (with Get-BrokerHypervisorConnection)
    • List all VMs within the specified Hypervisor path (with Get-ChildItem and the PSDrive “XDHyp:” created by Get-BrokerHypervisorConnection)
    • List all Machines within the source Desktop Group (with Get-BrokerDesktop)
  • For each Machine:
    • Get the ID from the list of VMs (HostedMachineId)
    • Enable Maintenance Mode (with Set-BrokerPrivateDesktop)
    • Remove the machine from the current Desktop Group (with Remove-BrokerMachine)
    • Remove the machine from the current Machine Catalog (with Remove-BrokerMachine too)
    • Add the machine to the target Machine Catalog (with New-BrokerMachine)
    • Associate the user account to the machine (with Add-BrokerUser)
    • Add the machine to the target Desktop Group (with Add-BrokerMachine)

The script writes to a separate log file for each (known) error or condition making it easier distinguish between the different cases when the script has finished its work.

So, finally here we go:

Disclaimer: I hope that the information in this post is valuable to you. Your use of the information contained in this post, however, is at your sole risk. All information on this post is provided “as is”, without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by me. Further, I shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.