XenApp Reboot Script 2


As many customers are still running XenApp 6.5, probably one of the most stable/successful XenApp releases, when time comes to reboot the servers on the farm the options available are quite limited.

After looking for some scripts I ended up finding the one fellow CTP Dane Young wrote and posted on his site. The problem IMHO it is overkill for many customers (what ends up making it complex, especially for people not very familiar with PowerShell).

I took his script as a starting point and created a much simpler version that works very well and does what most administrators need:

– Reboots the farm in groups of servers.
– Prevents new logons to the servers that will be rebooted.
– Sends messages to the users 15, 10 and 5 minutes before the reboot will happen.
– Reboots the servers, not waiting for the servers to be completely drained. To be honest I prefer this approach as if you need to wait until a server has no more users you may have to wait days in certain cases.
– Every interaction is logged to the event log (disabling logons, sending messages to users, servers being rebooted, etc).

In this example, the need was to reboot the farm in two passes. One covering half of the servers and another for the remaining servers, 30 minutes later. For this particular case the farm had to be rebooted twice a week. So this is what we did, preparation wise:

– Created four worker groups named TuesdaysRebootGroup1, TuesdaysRebootGroup2, FridaysRebootGroup1 and FridaysRebootGroup2.
– Added half of the servers to TuesdaysRebootGroup1 and the remaining servers to TuesdaysRebootGroup2. Did the exact same thing for the FridaysRebootGroup1 and FridaysRebootGroup2 worker groups.
– On the ZDCs (two of them), we created two scheduled tasks on each of them. On the first ZDC the two tasks are scheduled to run on Tuesdays and Fridays at 1:00am and on the second ZDC the tasks run on Tuesdays and Fridays at 1:30am. The tasks on the first ZDC take care of rebooting the servers on Group1 and the ones on the second ZDC take care of rebooting the servers on Group2.

Here you have the script:

#### BEGINNING

#———————————————————————————–
#———————————————————————————–
# Reboot script for XenApp 6.5 Citrix Farms
# Simply disables new logons and issues warning messages every 5 minutes
# for 15 minutes in total.
# This script can be run as a schedule task from the Zone Data Collector to process
# reboots for all other application servers
# Created by Cláudio Rodrigues, Citrix CTP, Microsoft MVP, VMware vExpert
# WTSLabs Inc. Copyright 2010, 2011, 2012, 2013
# http://blog.wtslabs.com
# Loosely based on the work by Dane Young, Citrix CTP
# Check http://blog.itvce.com/?p=79 for more information
# Build 2014.11.24 Revision 6
#———————————————————————————–
#———————————————————————————–

Add-PSSnapin “Citrix.Xenapp.Commands” -ErrorAction SilentlyContinue

# Define which worker group should be processed.
# We are using one script per worker group so make copies and change the worker groups as needed.
# Can be easily modified to allow multiple worker groups (see Dane’s script)
$Global:WORKERGROUP = “TestCR”
$Global:EventLog = New-Object -type System.Diagnostics.Eventlog -argumentlist Application
$Global:EventLog.Source = “Citrix Reboot Script”
$EventLog.WriteEntry(“Starting scheduled task Citrix Reboot Script.”,”Information”,”111″) # Create test event entry to note the start time of the script

$Step1 =
{
param ([string]$server)

$Global:EventLog = New-Object -type System.Diagnostics.Eventlog -argumentlist Application
$Global:EventLog.Source = “Citrix Reboot Script”

function DisableLogons
{
# Prohibits logons until next restart for server passed as variable 0
set-XAServerLogOnMode -ServerName $args[0] -LogOnMode ProhibitNewLogOnsUntilRestart
Write-Host “Disabling Logons on $server” -foregroundcolor Blue
}

DisableLogons $server
$EventLog.WriteEntry(“Disabled logons until next reboot on ” + $server + “.”,”Information”,”411″)

$sessions = Get-XASession | ? {($_.servername -eq $server -and $_.state -eq “Active”)}
foreach ($session in $sessions)
{
$username = $session.Accountname
Write-Host “Sending message to user $username on $server” -foregroundcolor Blue
Send-XASessionMessage -servername $server -MessageTitle “Server maintenance” -Messagebody “Server will be rebooted in 15 minutes” -sessionID $session.sessionid -MessageboxIcon “error”
}
$EventLog.WriteEntry(“Fifteen (15) minutes warning on server ” + $server + “.”,”Information”,”311″)

}

$Step2 =
{
param ([string]$server)

$Global:EventLog = New-Object -type System.Diagnostics.Eventlog -argumentlist Application
$Global:EventLog.Source = “Citrix Reboot Script”

$sessions = Get-XASession | ? {($_.servername -eq $server -and $_.state -eq “Active”)}
foreach ($session in $sessions)
{
$username = $session.AccountName
Write-Host “Sending message to user $username on $server” -foregroundcolor Green
Send-XASessionMessage -servername $server -MessageTitle “Server maintenance” -Messagebody “Server will be rebooted in 10 minutes” -sessionID $session.sessionid -MessageboxIcon “error”
}
$EventLog.WriteEntry(“Ten (10) minutes warning on server ” + $server + “.”,”Information”,”311″)

}

$Step3 =
{
param ([string]$server)

$Global:EventLog = New-Object -type System.Diagnostics.Eventlog -argumentlist Application
$Global:EventLog.Source = “Citrix Reboot Script”

$sessions = Get-XASession | ? {($_.servername -eq $server -and $_.state -eq “Active”)}
foreach ($session in $sessions)
{
$username = $session.AccountName
Write-Host “Sending message to user $username on $server” -foregroundcolor Yellow
Send-XASessionMessage -servername $server -MessageTitle “Server maintenance” -Messagebody “Server will be rebooted in 5 minutes. SAVE your work” -sessionID $session.sessionid -MessageboxIcon “error”

}
$EventLog.WriteEntry(“Five (5) minutes warning on server ” + $server + “.”,”Information”,”311″)

}

$RebootServer =
{
param ([string]$server)

$Global:EventLog = New-Object -type System.Diagnostics.Eventlog -argumentlist Application
$Global:EventLog.Source = “Citrix Reboot Script”

function StartReboot
{
# Creates a variable named server from the first passed variable
$server = “$args”
# Initiates shutdown on remote server
Invoke-Expression “Shutdown.exe /m $server /r /t 0 /c “”Shutdown scheduled by Citrix Reboot Script.”””
$EventLog.WriteEntry(“Initiating reboot process on ” + $server + “.”,”Information”,”911″)
Start-Sleep -s 120
}

StartReboot $server
}
# Main Script
$workergroup = $GLOBAL:WORKERGROUP
$workergroupservers = @(get-xaworkergroupserver -workergroupname $workergroup | sort-object -property ServerName)
foreach ($workergroupserver in $workergroupservers)
{
$server = $workergroupserver.ServerName
Write-Host “Step1 on $server” -foregroundcolor Blue

$EventLog.WriteEntry(“Processing server ‘” + $server + “‘ from worker group ‘” + $workergroup + “‘.”,”Information”,”211″)
Invoke-Command -ScriptBlock $Step1 -ArgumentList $server
}

Start-Sleep -s 300

foreach ($workergroupserver in $workergroupservers)
{
$server = $workergroupserver.ServerName
Write-Host “Step2 on $server” -foregroundcolor Green

Invoke-Command -ScriptBlock $Step2 -ArgumentList $server
}

Start-Sleep -s 300

foreach ($workergroupserver in $workergroupservers)
{
$server = $workergroupserver.ServerName
Write-Host “Step3 on $server” -foregroundcolor Yellow

Invoke-Command -ScriptBlock $Step3 -ArgumentList $server
}

Start-Sleep -s 300

foreach ($workergroupserver in $workergroupservers)
{
$server = $workergroupserver.ServerName
Write-Host “Rebooting $server” -foregroundcolor Red

Invoke-Command -ScriptBlock $RebootServer -ArgumentList $server
}

#### END

Yes, I do know this could be better and smaller, not to mention improved. The bottom line is, it is a simple script that does the job very well and at the same time it is simple and easy to follow, even for people not used to PowerShell.

I am sure it will help some of you out there. Any comments and suggestions (and even criticism) feel free to reach out. I am all ears.

CR

5,842 total views, 0 views today


Leave a comment

Your email address will not be published. Required fields are marked *

2 thoughts on “XenApp Reboot Script

  • Rajat

    Thanks for the post, it looks to be a great script and is exactly what a friend of mine needs. Apparently, Reboot Behavior Citrix Policy works only with Enterprise and Platinum editions and NOT with Advanced Edition and he needs to use a script like this. Copy/Paste from the site is causing encoding issue. Can you provide a link to download the script or let me know how to make it work in Powershell? I tried to convert the encoding in Notepad++ but to no avail.