Citrix Xen Desktop 7.X custom rolling powershell reboot script.

The standard reboot behaviour that you can configure in XenDesktop gui is not flexible/dynamic enough. I’ve got some inspiration from this Citrix blog. I’ve created a Powershell reboot script to restart Xen Desktop machines for a customer that is using PVS Shared Hosted Servers.

1. First define groups. In XenDesktop Studio Add a tag to the machines that you want to join the reboot schedule. Like group1.

2. After that change the parameters in the script.

The script will do the following:

1. Set the servers in maintanance mode per group.

2. Send a message to the users on the servers that will be rebooted

3. Reboot the servers in the group

4. Set maintanance mode off. Servers accepts new sessions.

I would like to share it, so here it is:

# Simple but effective reboot script. Sets machine with corresponding tag in maintance mode, wait for xxx seconds and sends xxx seconds before restart a message

# Change Variables if you wish.

# Define the name of the tags you want in the reboot script. You could define multiple to prevent reboot storms @(“Groep1″,”Groep2”)
$NameOfTags = @(“Groep1”)
# Time between maintance mode and message send to user on the server that is getting a reboot. default 35100
$SecondsBetweenMaintanceModeAndMessage = 35100
# Time between message send to user and the reboot of the server. default 900
$SecondsBetweenMessageAndReboot = 900
# Time between reboot and server ready for load. default 1800
$SecondsBetweenRebootAndReady = 1800
# Title of the message send to the user.
$TitleOfMessage = “our TEXT”
# Text of the message send to the user.
$TextofMessage = “Your TEXT”

#——————————-
# Don’t change things here. © Made by Mark Arnoldus KPN Consulting

Add-PSSnapin citrix*

Function F_GoSleep($Seconds) {Start-Sleep -s $Seconds}

Function F_SetMaintenanceModeIsTrue($Group)
{$GetBrokerMachine = Get-BrokerMachine | where {($_.Tags -contains “$Group”)}
Set-brokermachine $GetBrokerMachine -InMaintenanceMode $true}

Function F_SetMaintenanceModeIsFalse($Group)
{$GetBrokerMachine = Get-BrokerMachine | where {($_.PowerState -eq “on”) -and ($_.Tags -contains “$Group”) -and ($_.InMaintenanceMode -contains “True”) -and $_.RegistrationState -contains “Registered”}
Set-brokermachine $GetBrokerMachine -InMaintenanceMode $false}

Function F_RebootGroup($Group)
{Get-BrokerMachine | where {($_.PowerState -eq “on”) -and ($_.Tags -contains “$Group”) -and ($_.InMaintenanceMode -contains “True”)} | ForEach-Object {
new-brokerhostingpoweraction -Action Restart -MachineName $_.MachineName}}

Function F_GetSessionOfGroup($Group)
{$GetBrokermachineOfGroups = Get-BrokerMachine | where {($_.PowerState -eq “on”) -and ($_.Tags -contains “$Group”)} | foreach-object {$_.HostedMachineName}
$ArrayGetBrokermachineOfGroups = @($GetBrokermachineOfGroups)
ForEach ($GetBrokermachineOfGroup In $ArrayGetBrokermachineOfGroups) {
Get-BrokerSession | where {($_.HostedMachineName -contains “$GetBrokermachineOfGroup”)}}}

Function F_SendMessage($InputGroup)
{$GetBrokerSession = F_GetSessionOfGroup $InputGroup
Send-BrokerSessionMessage $GetBrokerSession -MessageStyle Information -Title $TitleOfMessage -Text $TextofMessage}

# Call Functions
ForEach ($NameOfTag In $NameofTags) {
F_SetMaintenanceModeIsTrue $NameOfTag
}

F_GoSleep $SecondsBetweenMaintanceModeAndMessage

ForEach ($NameOfTag In $NameofTags) {
F_SendMessage $NameOfTag
F_GoSleep $SecondsBetweenMessageAndReboot
F_RebootGroup $NameOfTag
F_GoSleep $SecondsBetweenRebootAndReady
}

ForEach ($NameOfTag In $NameofTags) {
F_SetMaintenanceModeIsFalse $NameOfTag
}

Update: For non pvs machines use the following script see the following code. Thanks for sharing Bas

# Simple but effective reboot script. Sets machine with corresponding tag in maintance mode, wait for xxx seconds and sends xxx seconds before restart a message

# Change Variables if you wish.

# Script start @ 24:00 and sets $group in maint. After 6 hours-> message to user, 15 min later -> reboot, 30 min later->Server ready @ 06:45

# Define the name of the tags you want in the reboot script. You could define multiple to prevent reboot storms @(“Groep1″,”Groep2”)

$NameOfTags = @(“Even”)

# Time between maintance mode and message send to user on the server that is getting a reboot. default 35100

$SecondsBetweenMaintanceModeAndMessage = 21600

# Time between message send to user and the reboot of the server. default 900

$SecondsBetweenMessageAndReboot = 900

# Time between reboot and server ready for load. default 1800

$SecondsBetweenRebootAndReady = 1800

# Title of the message send to the user.

$TitleOfMessage = “Reboot Citrix server”

# Text of the message send to the user.

$TextofMessage = “Dear User, this specific server that hosts your Citrix application is going for a reboot in 15 minutes. Please close your application and start it again to log on to a different server.”

# Text to log file

$LogText = ” Script heeft gelopen.”

# File location of log

$LogFileLocation = “D:\Management\Logs\ScheduledTask_Servers Reboot.log”

#——————————-

# Don’t change things here. © Made by Mark Arnoldus KPN Consulting, changed for CustomerX by Bas Stapelbroek KPN Consulting.

Add-PSSnapin citrix*

Function F_GoSleep($Seconds) {Start-Sleep -s $Seconds}

Function F_SetMaintenanceModeIsTrue($Group)

{$GetBrokerMachine = Get-BrokerMachine | where {($_.Tags -contains “$Group”)}

Set-brokermachine $GetBrokerMachine -InMaintenanceMode $true}

Function F_SetMaintenanceModeIsFalse($Group)

{$GetBrokerMachine = Get-BrokerMachine | where {($_.Tags -contains “$Group”) -and ($_.InMaintenanceMode -contains “True”) -and $_.RegistrationState -contains “Registered”}

Set-brokermachine $GetBrokerMachine -InMaintenanceMode $false}

Function F_RebootGroup($Group)

{Get-BrokerMachine | where {($_.Tags -contains “$Group”) -and ($_.InMaintenanceMode -contains “True”)} | ForEach-Object {

Restart-Computer $_.DNSName -Force}}

Function F_GetSessionOfGroup($Group)

{$GetBrokermachineOfGroups = Get-BrokerMachine | where {($_.Tags -contains “$Group”)} | foreach-object {$_.MachineName}

$ArrayGetBrokermachineOfGroups = @($GetBrokermachineOfGroups)

ForEach ($GetBrokermachineOfGroup In $ArrayGetBrokermachineOfGroups) {

Get-BrokerSession | where {($_.MachineName -contains “$GetBrokermachineOfGroup”)}}}

Function F_SendMessage($InputGroup)

{$GetBrokerSession = F_GetSessionOfGroup $InputGroup

Send-BrokerSessionMessage $GetBrokerSession -MessageStyle Information -Title $TitleOfMessage -Text $TextofMessage}

Function F_LogRunningOfScript($InputGroup)

{$filedate = get-date -format “M-d-yyyy hh:mm:ss”

$filedate + $LogText | Out-File -Append $LogFileLocation}

# Call Functions

ForEach ($NameOfTag In $NameofTags) {

F_SetMaintenanceModeIsTrue $NameOfTag

}

F_GoSleep $SecondsBetweenMaintanceModeAndMessage

ForEach ($NameOfTag In $NameofTags) {

F_SendMessage $NameOfTag

F_GoSleep $SecondsBetweenMessageAndReboot

F_RebootGroup $NameOfTag

F_GoSleep $SecondsBetweenRebootAndReady

}

ForEach ($NameOfTag In $NameofTags) {

F_SetMaintenanceModeIsFalse $NameOfTag

}

F_LogRunningOfScript

 

Advertisements

5 thoughts on “Citrix Xen Desktop 7.X custom rolling powershell reboot script.

  1. Check: Works out of the box for PVS (and MCS?).. Physical machines do not have powerstate, hostedmachine and cannot reboot servers by new-brokerhostingpoweraction -Action Restart -MachineName $_.MachineName

  2. Wow !! Very exciting CITRIX XEN DESKTOP 7.X CUSTOM ROLLING POWERSHELL REBOOT SCRIPT. Database server and networking workers definitely love this Topic. You have said this post within a single page But which described multiple page information. Fine effort !! I am expecting like this post. Thanks
    threadrollingmachine.org

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s