The scenario
You have multiple delivery groups in your XenDeskop/XenApp 7.12+, and you would like to use Powershell to Schedule Tag based Reboots for a Delivery Group
A tag is simply a mark or a label. Assuming you have a delivery group (A) with 20 machines assigned to it. You can label 10 of those machines with a tag “Z” for instance, and the other 10 with tag “X” then you can configure your weekly reboots to reboot the “X”s on Staurdays and the “Z”s on Sunday for instance.
In this blog, I am going to be tagging the members (machines) of my Delivery Group based on the last 2 numbers of the server name; the script will check the name and tag it with “Odd” tag if the server name ends with and odd number e.g. compuer110 and the same logic applies to even numbers.
The Tagging Script
The script below will tag members of DGs as evens and odds based on the last 2 digits in the name. You can tweak the script to accomplish tagging based on other criteria if you like.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
############################### ## Script to tag machines for weekly reboot based on odd/even numbers # 22/03/2017 # Amir Joseph Sayes # ninaronline.co.uk ############################### # Editable Variables param ( [string]$DGName = "YOUR_DELIVERY_GROUP_NAME" #The name of the delivery group that we need to tag servers/desktops in. ) #add Citrix snapin Add-PSSnapin citrix.* #configure the names of the tags to be used $oddtag = "odd_server_os_machines" $eventag = "even_server_os_machines" #check if the tags are already created in your site, and if not, create them $checkoddtag = get-brokertag -Name $oddtag -ErrorAction SilentlyContinue $checkeventag = get-brokertag -Name $eventag -ErrorAction SilentlyContinue If (!$checkoddtag) { New-BrokerTag -Name $oddtag} else {Write-Host $oddtag is already created in this site} If (!$checkeventag) { New-BrokerTag -Name $eventag} else {Write-Host $eventag is already created in this site} ############################## #Functions# ############################## Function TagMachinesOddEven() { #This function accept the machinename, parse it and tag the machine with either even or odd tag param ( [string]$Machinename, [string]$Machinefullname ) try { $tNumber = "$Machinename" -replace ‘\D+0+’ [uint64]$tNumber = $tNumber $tStringNumber = $tNumber / 2 | Out-String if($tStringNumber -match "\.") { #Write-Host "I am odd" Add-BrokerTag -name $oddtag -machine $Machinefullname Remove-BrokerTag -name $eventag -machine $Machinefullname } else { #Write-Host "I am Even" Add-BrokerTag -name $eventag -machine $Machinefullname Remove-BrokerTag -name $oddtag -machine $Machinefullname } } catch { Write-Host Error tagging $Machinename } } ############################## #END OF Functions# ############################## ####################################### # Main Script ####################################### If (!$DGName) { Write-Error "DGName variable value is null" break; } else { $result = Get-BrokerMachine -MaxRecordCount 1000 | Where-Object {$_.DesktopGroupName -eq $DGName} | Select-Object hostedmachinename ,machinename, desktopuid #loop inside your delivery group machines and tag servers accordignly foreach ($UID in $result) { TagMachinesOddEven -machinename $uid.HostedMachineName -machinefullname $UID.MachineName Get-BrokerMachine -desktopuid $UID.desktopuid | Select hostedmachinename, {$_.tags} } } |
The output would be something like this:
The Scheduled Reboots Script
New-BrokerRebootScheduleV2 command requires the switch –desktopgroupid; to get this value for your Delivery Group, run:
1 2 3 |
Get-BrokerDesktopGroup | select name,uid |
Now that you have your DG UID, you will need to decide on the frequency, day and the time, etc…
To map your scheduled reboot to a certain tag, use the switch –RestrictToTag
Below, I am creating a scheduled reboot for each tag, one for evens on Saturdays and another for odds machines on Sunday:
1 2 3 |
New-BrokerRebootScheduleV2 -DesktopGroupUid 1 -Frequency Weekly -day Saturday -Name THE_NAME_OF_THE_SCHDULED_REBOOT__EVENS -RestrictToTag even_Server_OS_Machines -StartTime 02:00:00 -RebootDuration 60 -WarningDuration 15 -WarningMessage “Please save your work, logoff and logon again, as this machine will reboot in 15 minutes" |
The output would be something like this:
To verify what has been configured use:
1 2 3 |
Get-BrokerRebootschedulev2 |
To remove a scheduled reboot use:
1 2 3 |
Remove-BrokerRebootScheduleV2 -Name THE_NAME_OF_THE_SCHDULED_REBOOT_ODDS |
Happy scripting!
References: Citrix Blogs