Feb 05

Delete a DesktopGroup

# —————————————————————————————————
#PowerShell sample script to delete a desktop group and related objects from a XenDesktop 5.x site
#Note: One or more required values must be specified below before executing the script.
#Version 1.0

#Provide the following values before executing:

#The FQDN for the DDC and XML port, if not the default of 80
#Eg: ‘ddc01.domain.com:80’
$ddcAddress = ”

#The name of the desktop group to be removed
$desktopGroupName = ”

#End of variables
asnp Citrix.*

#Get a collection of the desktops in the desktop group
$desktops = Get-BrokerDesktop -DesktopGroupName $desktopGroupName -AdminAddress $ddcAddress

#Get the machine UIDs for each desktop
$machineUids = $desktops | ForEach {$_.MachineUid}

#Use the collection of machine UIDs to remove them from the desktop group
Remove-BrokerMachine -InputObject $machineUids -DesktopGroup $desktopGroupName -AdminAddress $ddcAddress

#Get the access policy rules with the desktop group as an included desktop group
$aprs = Get-BrokerAccessPolicyRule -IncludedDesktopGroup $desktopGroupName -AdminAddress $ddcAddress

#Loop through the access policy rules
ForEach ($apr in $aprs) {

#Determine if the desktop group is the only referenced by the rule
If ($apr.IncludedDesktopGroups.Length -eq 1) {

#The desktop group is the only one referencing this rule; remove the rule
Remove-BrokerAccessPolicyRule -Name $apr.Name -AdminAddress $ddcAddress
} ElseIf ($apr.IncludedDesktopGroups.Length -gt 1) {

#This rule is being applied to more than one desktop group
#Get a list of the desktop groups with the (target) desktop group removed
$dgs = $apr.IncludedDesktopGroups | Where-Object { $_.Name -ne $desktopGroupName}

#Set the access policy rule to use the updated desktop groups list (sans target desktop group)
Set-BrokerAccessPolicyRule -Name $apr.Name -IncludedDesktopGroups $dgs -AdminAddress $ddcAddress

#Get the desktop group object for the desktop group
$desktopGroup = Get-BrokerDesktopGroup -Name $desktopGroupName -AdminAddress $ddcAddress

#Get the entitlement policy rules applied to this desktop group
$eprs = Get-BrokerEntitlementPolicyRule -DesktopGroupUid $desktopGroup.Uid -AdminAddress $ddcAddress

#Remove each entitlement policy rule
ForEach ($epr in $eprs) {
Remove-BrokerEntitlementPolicyRule -Name $epr.Name -AdminAddress $ddcAddress

#Remove the desktop group
Remove-BrokerDesktopGroup -Name $desktopGroupName -AdminAddress $ddcAddress

Leave a Reply

Your email address will not be published.