Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1048
  • Last Modified:

PowerShell to check XenApp offline servers and send only incremental records

We are using XenApp 5.0 environment and we are in the process of automating the XenApp environment using PowerShell.

Our existing scenario:

The PowerShell script checks for offline servers and stores the record in a log file. The script is scheduled to run on every 1 hour. It overwrites the existing log file and then send the log file by email.

Requirement:

The script will check for existing record in the log file and then send only newly added records (delta changes) by email.

Here is the PowerShell script that we are using to detect and store offline servers in a log file.
 
Add-PSSnapin citrix.XenApp.Commands

$Log_File="C:\OfflineServers.log"
Clear-Content $Log_File

##############################################################################
# Executing the command to check for Offline Server and stores in a log file
##############################################################################

QFARM /OFFLINE > $Log_File

#########################################
# Sending mail with the log attachment
#########################################

$smtpServer = "<SMTP Server Name>"
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($log_file)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "<From Email Address>"
$msg.To.Add("<To Email Address>")
$msg.Subject = "Offline Servers"
$msg.Body = "List of Offline Servers"
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()

Open in new window

0
hchabria
Asked:
hchabria
  • 3
1 Solution
 
hchabriaAuthor Commented:
Thanks a lot. Just waiting for solution.
0
 
Chris DentPowerShell DeveloperCommented:
Compare-Object is potentially the way to go.

And, if you're using PowerShell 2, it has Send-MailMessage, much neater than the raw .NET way :)

HTH

Chris
Add-PSSnapin citrix.XenApp.Commands

$Log_File="C:\OfflineServers.log"
$DeltaFile = "C:\OfflineServers-Changed.log"

##############################################################################
# Executing the command to check for Offline Server and stores in a log file
##############################################################################

$ExistingList = Get-Content $Log_File
$NewList = QFARM /OFFLINE

# This defines newly added entries, ignoring removed entries
$DeltaList = Compare-Object $ExistingList $NewList | 
  Where-Object { $_.SideIndicator -eq '=>' } |
  Select-Object -ExpandProperty InputObject

# Write that to a temp file for the attachment
$DeltaList | Out-File $DeltaFile -Encoding ASCII

# Save the complete list
$NewList | Out-File $log_file -Encoding ASCII

$smtpServer = "<SMTP Server Name>"
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($DeltaFile)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "<From Email Address>"
$msg.To.Add("<To Email Address>")
$msg.Subject = "Offline Servers"
$msg.Body = "List of Offline Servers"
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()

Open in new window

0
 
hchabriaAuthor Commented:
Thank you very much for the script. I will try it and let you know the result.
0
 
hchabriaAuthor Commented:
Excellent. That is the script I wanted. Thank you so much for your kind help.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now