Using Powershell to Schedule Tag-based Reboots for a Delivery Controller in XenDesktop 7.12

The scenario:
You have multiple delivery groups in your XenDeskop/XenApp 7.12+, and you would like to schedule two or more different reboot schedules for machines that belong to the same delivery group based on their tag.
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.


###############################
## 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:

2017-08-02 16_28_19-PWGSXAGSEMDC103 - Remote Desktop Connection Manager v2.7

The Scheduled Reboots Script:
New-BrokerRebootScheduleV2 command requires  the switch –desktopgroupid; to get this value for your Delivery Group, run:


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:


#the odds reboot schedule
New-BrokerRebootScheduleV2  -DesktopGroupUid 1 -Frequency Weekly -day Sunday -Name THE_NAME_OF_THE_SCHDULED_REBOOT_ODDS -RestrictToTag Odd_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 even reboot schedule
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:

2017-08-02 16_40_04-PWGSXAGSEMDC103 - Remote Desktop Connection Manager v2.7

To verify what has been configured use:


Get-BrokerRebootschedulev2

To remove a scheduled reboot use:


Remove-BrokerRebootScheduleV2 -Name THE_NAME_OF_THE_SCHDULED_REBOOT_ODDS 

Happy scripting!

References: Citrix Blogs

 

 

 


Leave a Reply