If your Citrix environment is similar to most you may be rebooting your servers on an interval.  In my case we have Citrix policies and a reboot script that will reboot all of our Citrix servers every week.  Just because there are policies in place doesn’t mean that the reboots are happening.  A couple examples of situations that can prevent a server reboot are sessions can become hung or the server may have logins disabled, but a hung process prevented the reboot.

[wp_ad_camp_1]

To gain better insight of whether or not your servers rebooted the following PowerShell script can be used to check your servers for Uptime based upon last reboot time and email yourself a report.

1)      Create an account that can be used to run this as a scheduled task.  This account must have access to your Citrix XenApp Farm.

2)      Create a folder called reports on the XenApp server this script will run from

3)      Modify the “#Email the Report to your Citrix Adminstrators” section of the script to reflect the settings and email addresses for your environment.  The most important variables to change are shown here.

$smtpserver = “smtp.domain.com”
$msgfrom = “[email protected]
$msgto = “[email protected]

4)      Save the following code as a .ps1 file into the reports directory you created

#Add Citrix XenApp Powershell Commands
Add-PSSnapin “Citrix.XenApp.Commands” -ErrorAction SilentlyContinue

#Create html file and get XenApp Servers sorted alphabetically
[string]$path = “c:\reports\xauptime.html”
$servers = Get-XAServer | Sort-Object -Property ServerName

#Get uptime for XenAppServers in the Array via Scripting Guys blog post.
Function Get-UpTime
{ Param ([string[]]$servers)
Foreach ($s in $servers)
{
$os = Get-WmiObject -class win32_OperatingSystem -cn $s
New-Object psobject -Property @{computer=$s;
uptime = (get-date) – $os.converttodatetime($os.lastbootuptime)}}}

#Add Server data and Uptime information to html file
Get-UpTime -servers $servers |
ConvertTo-Html -As Table -body ”
<h1>Citrix Server Uptime Report</h1>
The following report was run on $(get-date)” >> $path

#Email the Report to your Citrix Administrators
$smtpserver = “smtp.domain.com”
$msgfrom = “[email protected]
$msgto = “[email protected]
$msgsubject = “Citrix Server Uptime”
$body = get-content “c:\reports\xauptime.html”

$message = New-Object System.Net.Mail.MailMessage $msgfrom, $msgto
$message.subject = $msgsubject
$message.IsBodyHTML = $true

$message.Body = $body

$smtp = New-Object Net.Mail.SmtpClient($emailserver)
$smtp.Send($message)

#Delete the html file
Remove-Item c:\reports\xauptime.html -recurse
Now that we have our PowerShell script we will want to create a Windows scheduled task to run the script on the interval you choose.  For example, you may choose to do this every evening.

Create a scheduled Task to execute the newly created .ps1 file

  • Request or create a service account to run the scheduled task.  This account will need administrator access to Citrix and should not be used for anything else.
  • Sign into a server that has Citrix XenApp and the Citrix XenApp SDK installed
  • Open Task Scheduler through the server Control Panel.
  • In the Task Scheduler console expand the tree on the left hand side until you see Microsoft.  From there, right-click on Microsoft and choose “Create Task”.

task1

  • Give your scheduled task a name and then check the radio button marked to “Run whether user is logged on or not”.task2
  • On the Triggers Tab, in the bottom left corner click the “New” button and create an appropriate schedule for your task.

Note:  This frequency should be determined by how often you expect your Citrix servers to reboot.

task3

  • Click on the Actions Tab and then in the bottom left hand corner click the “New” button.

task4

  • Fill in the Program/Script and Add Arguments Fields.  See below for the syntax for each field and adjust accordingly for your environment
  • Program/Script:  C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • Add Arguments (Optional): -ExecutionPolicy Bypass -File “C:\ reports\CitrixRebootReport.ps1”
    • Skip the conditions Tab.
    • Click on the settings tab and adjust to your preferences; however, please note that the default settings are typically ok.

task5

  • Click Ok.
  • You will be prompted to enter the password for the account used for the scheduled task.

Upon completion of this series of steps this script will run on the scheduled interval of your choice and receive a report that will let you know when your Citrix Servers have last rebooted.

error

Enjoy this blog? Please spread the word :)