Advertisement
henrydenhengst

Reboot Script XenDektop/XenApp 7.x

May 26th, 2015
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #---------------------------------------------------------------------
  2. # Script: RebootXenApp7VDA.ps1
  3. # Reboot Script for two groups of XenApp 7.x VDAs (for 24x7 environments)
  4. # Creator: Wilco van Bragt
  5. # Creation Date: 27-11-2014
  6. #---------------------------------------------------------------------
  7. # Version: 0.1
  8. # By: Wilco van Bragt
  9. # Date: 27-11-2014
  10. # Changes: Initial Release
  11. #---------------------------------------------------------------------
  12. # Import Modules
  13. #---------------------------------
  14. add-pssnapin Citrix.* -erroraction silentlycontinue
  15. # Define Infrastructure Dependent Variables
  16. #---------------------------------
  17. $DeliveryGroup="<<DELIVERYGROUPNAME>>"
  18. $EnableMaintenanceTime=21600
  19. $Tempdir="C:\temp"
  20. $Logdir="C:\temp"
  21. $EvenDays="Sunday","Tuesday","Thursday"
  22. $OddDays="Monday","Wednesday","Friday"
  23. $UserWarningMessagePart1="Please save your work and log-off. This machine will be restarted in"
  24. $UserWarningMessagePart2="minutes."
  25. # Define Script Variables
  26. #---------------------------------
  27. $exe="C:\Windows\System32\msg.exe"
  28. $ScriptStart=Get-Date
  29. $Date=Get-Date -format M.d.yyyy
  30. $DayofWeek=(get-date).dayofweek
  31. $Logfile=$Logdir+"\RebootLog"+$Date+".log"
  32. $ServerstoReboot=$TempDir+"\ServerstoReboot.txt"
  33. # Functions
  34. #---------------------------------
  35. Function LogWrite
  36. {
  37.    Param ([string]$logstring)
  38.    Add-content $Logfile -value $logstring
  39. }
  40. # Start RebootScript
  41. #---------------------------------
  42. LogWrite "Script started at $ScriptStart"
  43. # Determine based on the Day of the week even or odd servers are booted
  44. #---------------------------------
  45. If ($DayofWeek -eq "Saturday")
  46. {LogWrite "Today is Saturday, no servers will be rebooted"
  47.    $EndScriptDate=Get-Date
  48.    LogWrite "Script ended on $EndScript"
  49.    exit
  50. }
  51. If ($EvenDays -contains $DayofWeek)
  52. {$Reboot="Even"
  53.    LogWrite "Day of the week is $DayofWeek, even servers need to be rebooted"
  54.    }
  55. If ($OddDays -contains $DayofWeek)
  56.    {$Reboot="Odd"
  57.    LogWrite "Day of the week is $DayofWeek, odd servers need to be rebooted"
  58.    }
  59.  
  60. # Catch the VDAs in the Delivery Group which are not in maintenance mode
  61. #---------------------------------
  62. LogWrite "Determine which VDAs in the Delivery Group which are not in maintenance and have the state registered"
  63. Get-BrokerMachine -DesktopGroupName "$DeliveryGroup" | Where-Object {($_.InMaintenanceMode -ne "True") -And ($_.RegistrationState -eq "Registered")} | select MachineName | Add-Content $TempDir"\VDAs.txt"
  64. if (!(test-path $TempDir"\VDAs.txt"))
  65.                 {LogWrite "No machines are found. Script will quit."
  66.                 $EndScriptDate=Get-Date
  67.    LogWrite "Script ended on $EndScript"
  68.                 exit
  69.                 }
  70. $VDAs=Get-Content $TempDir"\VDAs.txt"
  71. LogWrite "The following servers are available in the Delivery Group: $VDAs"
  72. LogWrite "Next step is to determine which servers will be rebooted this day"
  73. #Trim the VDA Name and determine if the server should be rebooted
  74. #---------------------------------
  75. foreach ($line in $VDAs)
  76.                 { $DeviceName=$line.split('=')[1]
  77.                     $DeviceName=$DeviceName.Trim("}")
  78.                 $DeviceLastNumber = [int]"$(($DeviceName)[-1])"
  79.      If([bool]!($DeviceLastNumber%2))
  80.        {$DeviceNumber="Even"}
  81.      else
  82.        {$DeviceNumber="Odd"}
  83.      If ($Reboot -eq "Even" -And $DeviceNumber -eq "Even")
  84.          {$DeviceName | Add-Content $ServerstoReboot
  85.            LogWrite "$DeviceName is added to the list of servers to reboot"
  86.          }                  
  87.      If ($Reboot -eq "Odd" -And $DeviceNumber -eq "Odd")
  88.          {$DeviceName | Add-Content $ServerstoReboot
  89.            LogWrite "$DeviceName is added to the list of servers to reboot"
  90.          }    
  91.        $DeviceName=$null
  92.    }
  93. if (!(test-path $ServerstoReboot))
  94.                 {LogWrite "No machines are added to list to reboot. Script will quit."
  95.                 $EndScriptDate=Get-Date
  96.    LogWrite "Script ended on $EndScript"
  97.                 exit
  98.                 }            
  99.                
  100. #Set Maintenance Mode for servers who will reboot
  101. #---------------------------------
  102. $RebootServers=Get-Content $ServerstoReboot
  103. foreach ($line in $RebootServers)
  104.      {$DeviceName=$line
  105.      Set-BrokerMachineMaintenanceMode -InputObject $DeviceName -MaintenanceMode $True
  106.      LogWrite "$DeviceName is set in maintenance mode"
  107.      $DeviceName=$null
  108.      }
  109.      LogWrite   "All machines are set in Maintenance. Script will wait for $EnableMaintenanceTime seconds to continue."
  110.      start-sleep -s $EnableMaintenanceTime
  111. #Send Messages to possible active users
  112. #---------------------------------
  113. foreach ($line in $RebootServers)
  114.        {$DeviceName=$line
  115.        $DeviceName=$DeviceName.split('\')[1]
  116.        & $exe * /Server:$DeviceName /time:120 $UserWarningMessagePart1 "60" $UserWarningMessagePart2
  117.        LogWrite "Warning message 60 minutes is send to possible active users on machine $DeviceName"
  118.          }
  119. start-sleep 1800
  120. foreach ($line in $RebootServers)
  121.        {$DeviceName=$line
  122.        $DeviceName=$DeviceName.split('\')[1]
  123.        & $exe * /Server:$DeviceName /time:120 $UserWarningMessagePart1 "30" $UserWarningMessagePart2
  124.        LogWrite "Warning message 30 minutes is send to possible active users on machine $DeviceName"
  125.        }
  126. start-sleep 1200
  127. foreach ($line in $RebootServers)
  128.        {$DeviceName=$line
  129.        $DeviceName=$DeviceName.split('\')[1]
  130.        & $exe * /Server:$DeviceName /time:120 $UserWarningMessagePart1 "10" $UserWarningMessagePart2
  131.        LogWrite "Warning message 10 minutes is send to possible active users on machine $DeviceName"
  132.        }
  133. start-sleep 300
  134. foreach ($line in $RebootServers)
  135.        {$DeviceName=$line
  136.        $DeviceName=$DeviceName.split('\')[1]
  137.        & $exe * /Server:$DeviceName /time:120 $UserWarningMessagePart1 "5" $UserWarningMessagePart2
  138.        LogWrite "Warning message 5 minutes is send to possible active users on machine $DeviceName"
  139.        }
  140. start-sleep 300
  141. #Execute Reboot and check if servers are back on-line
  142. #---------------------------------
  143. LogWrite "Actual reboot process will start now"
  144. foreach ($line in $RebootServers)
  145.        {$CTXDeviceName=$line
  146.          $DeviceName=$line
  147.          $DeviceName=$DeviceName.split('\')[1]
  148.          LogWrite "Restart command is send to machine $DeviceName"
  149.          Restart-Computer -Computername $DeviceName -Force -Wait -For PowerShell
  150.          LogWrite "Start-up process of $DeviceName is being checked."
  151.          $count=$null
  152.          Do
  153.            {   start-Sleep 30
  154.                  $ServerStatus=Get-BrokerMachine -MachineName $CTXDeviceName
  155.                $count=$count+1
  156.            }
  157.          Until (($ServerStatus.RegistrationState -eq "Registered") -or ($count -eq "4"))
  158.          
  159.          IF ($ServerStatus.RegistrationState -eq "Registered")
  160.          {LogWrite "Machine $CTXDeviceName has successfully registered. MaintenanceMode will be turned off."
  161.                                Set-BrokerMachineMaintenanceMode -InputObject $CTXDeviceName -MaintenanceMode $False
  162.                                }
  163.          ELSE
  164.          {LogWrite "Machine $CTXDeviceName is not successfully registered. Please check this machine."}
  165.          }
  166. #Clean-up temporary files        
  167. #---------------------------------
  168. Remove-Item $ServerstoReboot
  169. Remove-Item   $TempDir"\VDAs.txt"  
  170.                              
  171. $EndScriptDate=Get-Date
  172. LogWrite "Script finished at $EndScriptDate"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement