Citrix XenApp Powershell Report with Days Since Last Reboot and Citrix Servers with Logins Disabled

Previously I put together two separate how-to articles that show you how to use powershell to setup emailed reports for Citrix servers with disabled logins (http://24x7itconnection.com/2014/02/25/powershell-report-that-will-check-for-citrix-xenapp-servers-with-logins-disabled/) and how to determine how many days it has been since each Citrix server in your farm has rebooted (http://24x7itconnection.com/2014/01/13/use-powershell-to-get-an-e-mailed-report-about-xenapp-6-citrix-server-uptime/?relatedposts_exclude=248).

This how-to combines the two separate reports into one report into one.

For this Powershell script to work properly you will need to have the following in place:

  • Create the following directories on the Citrix server where the powershell will run from c:\healthreport and c:\powershellscripts\healthreport\reports
  • Modify the #Email the report to your Citrix Administrators section
  • Create a service account that has access to Citrix
  • Install the appropriate Citrix XenApp SDK for Powershell
  • Save the following code as a .ps1 file
  • See the following article and go to the section on how to set this up as a scheduled task in order to routinely automate this script http://24x7itconnection.com/2014/02/25/powershell-report-that-will-check-for-citrix-xenapp-servers-with-logins-disabled/
#Load Powershell Snap-in’s
Add-PSSnapin “Citrix.XenApp.Commands” -ErrorAction SilentlyContinue#Get a list of Citrix servers with Logins Disabled and Add to the html file
[string]$path = “C:\HealthReport\CitrixServerHealth.html”
$logins = Get-XAServer | where {$_.LogonsEnabled -eq $false} | select ServerName, LogonsEnabled | Sort-object -Property LogonsEnabled | ConvertTo-HTML -As Table -body “
<h1>Citrix Servers with Disabled Logins</h1>
The following report was run on $(get-date)” >> $path
#Create html file and get XenApp Servers sorted alphabetically
[string]$path = “C:\HealthReport\CitrixServerHealth.html”
$servers = Get-XAServer | Sort-Object -Property ServerName#Get uptime for XenAppServers in the Array
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))).TotalDays}}}

#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
$emailserver = “smtp.domain.com”
$msgfrom = “address@domain.com”
$msgto = “address@domain.com”
$msgsubject = “Citrix Environment Health Report”
$body = get-content “C:\HealthReport\CitrixServerHealth.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)
#Move File with a new Name for reporting
$today = get-date -Displayhint DateTime
Copy-Item C:\HealthReport\CitrixServerHealth.html C:\PowershellScripts\HealthReport\Reports\
rename-item -path C:\HealthReport\Reports\CitrixServerHealth.html -newname Report_$((Get-Date).ToString(‘MM-dd-yyyy_hh-mm-ss’)).html
#Delete the html file
Remove-Item C:\HealthReport\CitrixServerHealth.html -recurse

Powershell Report that will check for Citrix XenApp Servers with Logins Disabled

Sometimes Citrix application servers that are automatically rebooted through Citrix Server Policy can become hung during the process and marked with logins disabled. When this happens the users were weaned from the server, but the server didn’t actually reboot. As an administrator you will likely want to get this server back into production ASAP. The following script will look through your XenApp Farm Servers for servers with disabled logins and when configured appropriately will email your Citrix administrators the servers with disabled logins.

This should be run after your reboot scripts are expected to be complete and can be automated via scheduled task. Let’s get started!

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 DLogins on the XenApp server this script will run from

3) Modify the #Email the Report to your Citrix Administrators 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 = “address@domain.com”
  • $msgto = “address@domain.com”

4) Save the following code as a .ps1 file to any directory on the Citrix XenApp server you are running the script from.

#Load the Citrix XenApp Powershell Commands

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

#Create html file and get XenApp Servers sorted alphabetically
[string]$path = “C:\DLogins\logonsd.html”
$servers = Get-XAServer | where {$_.LogonsEnabled -eq $false} | select ServerName, LogonsEnabled | Sort-object -Property LogonsEnabled | ConvertTo-HTML -As Table -body “
<h1>Citrix Servers with Disabled Logins</h1>
The following report was run on $(get-date)” >> $path

#Email the Report to your Citrix Administrators
$emailserver = “smtp.domain.com”
$msgfrom = “address@domain.com”
$msgto = “address@domain.com”
$msgsubject = “Citrix Servers with Disabled Logins”
$body = get-content “C:\DLogins\logonsd.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:\DLogins\logonsd.html -recurse

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”.

  • 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 reboot and should be run after reboots are expected to be completed.

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:\directorynamechosenabove\filenamechosenabove.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 are stuck with their logins disabled and have not rebooted.

Use PowerShell to Get an E-mailed Report about XenApp 6 Citrix Server Uptime

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.

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 = “address@domain.com”
$msgto = “address@domain.com”

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 = “address@domain.com”
$msgto = “address@domain.com”
$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”.

  • 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.

Ringing in the New Year 2014 (Citrix, Exchange and Active Directory)

This past year brought about many new exciting things in technology. On the Citrix frontier virtual desktops are the hot topic. There are many great options available to organizations such as Citrix XenApp Hosted Desktops, VMWare VDI and Citrix XenDesktop VDI. Discovery of these topics includes performance, costs and reliability. What really makes sense for your organization?

As for Microsoft Exchange and Active Directory we have been strongly looking at Office 365 and security options for BYOD. Organizations moving their email and other office related products to the cloud and looking for new ways to secure corporate data on personal devices.

It is fun to watch strategies in both of these areas unfold. Below please find some of my featured articles that discuss these topics and more.

Wishing each of you a Happy and Safe New Year!

Theresa Miller

Citrix

Are Citrix XenApp Hosted Desktops the Right VDI Option for You?

http://24x7itconnection.com/2013/12/18/are-citrix-xenapp-hosted-desktops-the-right-vdi-option-for-you/

Why Use Citrix Profile Manager for User Profiles?

http://24x7itconnection.com/2013/12/12/why-use-citrix-user-profile-manager-for-citrix-applications/

Use PowerShell to Create a Load Evaluator for Citrix Server Maintenance

http://24x7itconnection.com/2013/12/06/use-powershell-to-create-a-load-evaluator-for-citrix-server-maintenance/

Use PowerShell to Replicate Printer Drivers to Citrix Servers

http://24x7itconnection.com/2013/10/18/xenapp-6-0-replicate-printer-drivers-to-citrix-servers/

Exchange

Office 365, Is it a Good Fit for My Organization?

http://blog.enowsoftware.com/solutions-engine/bid/155613/Office-365-is-it-a-Good-Fit-for-my-Organization

Random Thoughts on BYOD and Email

http://blog.enowsoftware.com/solutions-engine/bid/154874/Random-Thoughts-on-BYOD-and-Email

Under the Hood with ExFolders

http://blog.enowsoftware.com/solutions-engine/bid/154390/Under-the-Hood-with-ExFolders-and-Microsoft-Exchange

My Users Can Create Organizationally Visible Distribution Lists?

http://blog.enowsoftware.com/solutions-engine/bid/154697/My-Users-Can-Create-Distribution-Lists

Getting Started with Exchange 2010 Relay Connectors

http://blog.enowsoftware.com/solutions-engine/bid/155079/Getting-Started-with-Exchange-2010-Relay-Connectors

How to Tackle Exchange 2010 Backup and Recovery

http://blog.enowsoftware.com/solutions-engine/bid/155451/How-to-Tackle-Exchange-2010-Backup-and-Recovery

Add an Exchange Custom Attribute Using Exchange Management Shell

http://24x7itconnection.com/2013/10/18/how-to-add-an-exchange-custom-attribute-using-powershell/

Use PowerShell to Create a Load Evaluator for Citrix Server Maintenance

In Citrix you can apply artificial load to your Application Servers to prevent new users from attaching to the server. By doing this you allow existing connections to gracefully wean from the Citrix server you would like to do maintenance on instead of abruptly kicking them off. This can be done through your Citrix XenApp PowerShell SDK. Download and install the SDK for your version of XenApp at http://community.citrix.com/display/xa/Download+SDKS.

Create a Citrix Server Maintenance Mode Load Evaluator

1) Logon to a Citrix XenApp Application Server
2) From the Start Menu launch the Citrix XenApp Server SDK (x86)
3) Type Set-ExecutionPolicy RemoteSigned
4) Type Y and Press Enter
5) Type New-XALoadEvaluator ServerMaintenance -description “Remove Citrix Servers from Production for Maintenance” -ContextSwitches 0,1 press enter
Note: This will create the new load evaluator and provide you with a summary as shown below.

citrixmaintenance

6) Type Get-XALoadEvaluator ServerMaintenance and press enter to validate the load evaluator exists
7) Type Set-XAServerLoadEvaluator -ServerName ServerName -LoadEvaluatorName “ServerMaintenance” and press enter to apply the new load evaluator
8) Type Get-XAServerLoad and press enter to verify that load is at 10000 which will prevent new connections without interrupting the current users

Note: Be sure to monitor user connections until there are not anymore users attached to the server and then complete your Citrix server maintenance.

Replicate Printer Drivers to Citrix servers through Powershell

In the older versions on Citrix such as XenApp 4.5 it was possible to open the XenApp Advanced Configuration and within Printer Management you could allow Citrix to install printer drivers on your Citrix servers. This was especially useful when bringing a new Citrix server into the environment. In XenApp 6.0 we no longer use the XenApp Advanced Configuration, so there is a way to replicate your printer drivers is through Powershell. Also, in order for this to work you must have the Citrix XenApp Server SDK installed on your Citrix Servers.http://community.citrix.com/display/xa/Download+SDKS

Here are the steps:

  • Logon to a Citrix server that has the printer drivers you want replicated to another server
  • Open Windows Powershell with Citrix Xenapp Server SDX (X86)
  • Type set-executionpolicy remotesigned enter, choose Y
  • Type Start-XAPrinterDriverReplication -DriverName “HP Universal Printing PS (v5.4)” -SourceServerName FillInSourceServer -TargetServerName FillInTargetServer

Monitor installation progress through the server application log. This is a secondary process on the server so it may take up to 15 minutes for this to work.

Note: It is possible to push drivers to multiple target servers. Separate target servers with a comma and a space (ex. XEN1, XEN2)