Background information
Recently I was at a customer operates an Citrix Virtual Apps and Desktops farm that connects to multiple hypervisors (VMware vSphere and Nutanix AHV) to be used with Machine Creation Services. With migrations and new hardware, the site now has multiple hypervisor connections to the same hypervisor and even more hypervisor resources (hosting units) in each connection. To perform a clean up we wanted to create an overview for each resource/hosting unit to see which machine catalogs (provisioning schemes) use these resources, with the goal of removing the ones that are not used (anymore).
To get this overview we have created a PowerShell script that does exactly that. This short blogpost will describe the script and how you use it.
Why is there a connection with the hypervisor?
When you want to use Machine Creation Services in your Citrix Virtual Apps and Desktops site, you need a connection to your hypervisor (VMware vSphere, Nutanix AHV, Citrix XenServer, etc.) with the appropriate permissions (e.g.: VMware vSphere permissions) to create your session hosts and to be able to manage these machines.
You can find your hosting connections in Citrix Studio under Configuration -> Hosting. Each connection can have multiple resources. Resources specify which storage is used and which networks are available/configured. On the Citrix backend these resources are called hosting units.
For each machine catalog that you create that uses Machine Creation Services a provisioning scheme is created. This provisioning scheme directly relates to a hosting unit.
How to use the script
With this information we created a PowerShell script (complete script below) that lists all configured hosting units for your Citrix VAD site and which provisioning schemes use that exact hosting unit.
If it isn’t being used you should be able to remove it.
You can run this script on your delivery controller/broker without any parameters. If you run it on a different machine you need to use the -DeliveryControllers parameter and specify one or more of your delivery controllers by FQDN, Hostname or IP (divided by a comma).
The script requires the Citrix VAD PowerShell SDK, so make sure this is available (just make sure Citrix Studio is installed).
The actual script
<# .SYNOPSIS Show Citrix VAD hosting unit usage. .DESCRIPTION List all Citrix VAD hosting units and show which provisioning schemes/machine catalogs use this hosting unit. .EXAMPLE.ps1 -DeliveryControllers "CTXDC1.domain.local,CTXDC2.domain.local" .LINK https://github.com/ChrisJeucken/PowerShellScripts/tree/main/22-Citrix-Get-hosting-unit-usage .NOTES Version: 1.0 Author: Chris Jeucken Creation Date: 14 November 2024 Website: https://chrisjeucken.com .PARAMETER DeliveryControllers Specify Fully Qualified Domain Name, hostname or IP address of the Citrix VAD Delivery Controllers. Divide multiple DC's with a comma or don't specify this parameter if this script is run on a Delivery Controller. E.g.: CTXDC1.domain.local,CTXDC2.domain.local #> # PARAMETERS -------------------- param( [Parameter()] [string]$DeliveryControllers ) # PREREQUISITES ----------------- # Import Citrix PowerShell Snapins Add-PSSnapin -Name "Citrix*" # ------------------------------- # SCRIPT ------------------------ # Determine working Citrix VAD Delivery Controller if ($DeliveryControllers) { $DeliveryControllers = $DeliveryControllers.Split(",") foreach ($DeliveryController in $DeliveryControllers) { if (Test-Connection -ComputerName $DeliveryController -Count 1 -ErrorAction SilentlyContinue) { $TargetDC = $DeliveryController Break } } } else { $TargetDC = "localhost" } if (!($TargetDC)) { Write-Error "No working Citrix VAD Delivery Controller found. Stopping script." Return } # Verify availability of Citrix cmdlets if (!(Get-Command -Name Get-BrokerHypervisorConnection -Erroraction SilentlyContinue)) { Write-Error "Citrix cmdlets not found. Is Citrix Studio installed on this machine?" Return } # Get hosting units if (!(Get-PSDrive -Name "XDHyp")) { Write-Error "XDHyp PSDrive does not exist. Have the Citrix PowerShell snapins loaded correctly?" Return } else { $HostingUnits = Get-ChildItem "XDHyp:\HostingUnits\" -AdminAddress $TargetDC } # Get provisioning schemes $ProvSchemes = Get-ProvScheme -AdminAddress $TargetDC # Check which hosting units are not specified in any provisioning scheme foreach ($HostingUnit in $HostingUnits) { Write-Host "--- Hosting unit:" $HostingUnit.HostingUnitName "---" -ForegroundColor Green if ($ProvSchemes.HostingUnitUid -notcontains $HostingUnit.HostingUnitUid) { Write-Host "No provisioning schemes found for this hosting unit." } else { (($ProvSchemes | Where-Object {$_.HostingUnitUid -eq $HostingUnit.HostingUnitUid}) | Select-Object -Property ProvisioningSchemeName).ProvisioningSchemeName } } # -------------------------------
DISCLAIMER: Once again: I’m in no way an expert PowerShell scripter, so it might not be the most efficient code, but it gets the job done. And, of course, feel free to use it/alter it/publish it as your own.
You can also find this script on my GitHub page (script 22):
https://github.com/ChrisJeucken/PowerShellScripts