Thursday, January 13, 2011

Citrix Daily Server testing script useing Powershell 2.0 and MFCOM objects.

N.B : The script is applied on CPS 4.0 onwards only

Please note that before executeing the script make sure you have powershell 2.0 and Powershell SDK for Xenapp is installed on the server.


Add-PSSnapin citrix.XenApp.Commands
#log file used to stored logs
$log_file="w:\logs\servertesting.log"
Clear-Content $log_file
$tool="qfarm.exe"
$cmdline="/offline"
$date = Get-Date
$servers=Get-XAServer
$ping = new-object System.Net.NetworkInformation.Ping #creates a ping object
$dns=$server.ServerName
$ip=$server.IPAddresses
echo "Checking Server's Health..."
echo "REPORT OVERVIEW:" >> "$log_file"
echo "This script uses the Citrix Farm API to check all Citrix servers in the farm that it knows about." >>$log_file
echo "It then runs various checks against each server and only logs the results of any failures." >>$log_file
echo "This script runs every day at 1am Eastern Time.  If there are any questions on this report, please contact Kevin Bianchi" >> $log_file
echo "                                           " >> $log_file
echo "REPORT DETAILS:" >> $log_file
echo "Starting Server Health script check at Date: $Date" >> "$log_file"
echo "########################################" >> "$log_file"
echo "Starting Ping Failure checks" >> "$log_file"
foreach($server in $servers){
 $ping = new-object System.Net.NetworkInformation.Ping #creates a ping object
 $dns=$server.ServerName
 $ip=$server.IPAddresses
    echo "`n**** $server ****"
    $result=$ping.send($dns,500).Status.ToString()
    echo "Ping $server by hostname: $result"
    $result=$ping.send($ip,500).Status.ToString()
    echo "Ping $server by IP: $result"
 if ($result -ne "Success"){
    echo "Pinging $server by IP: $result" | Out-File $log_file -width 240 -Append #logs the result
   }
  
  if($result -eq "Success"){
        try {
            $socket = new-object System.Net.Sockets.TcpClient($ip, $server.IcaPortNumber) #creates a socket connection to see if the port is open
        } catch {
            $socket = $null
        }

  if($socket -ne $null) {
            echo "Socket Connection Successful."
           
            $stream = $socket.GetStream() #gets the output of the response
                  
            $buffer = new-object System.Byte[] 1024
            $encoding = new-object System.Text.AsciiEncoding
            Start-Sleep -Milliseconds 500 #records data for half a second
            while($stream.DataAvailable)
            {
                $read = $stream.Read($buffer, 0, 1024) 
                $response=$encoding.GetString($buffer, 0, $read)
                if($response -like '*ICA*'){
                    Write-Host "ICA protocol  found." -ForegroundColor Green
                    #echo "ICA protocol  found." | Out-File $log_file -width 240 -Append #logs the
$result
                } else {
                    echo "Something else responded."
     echo "ICA Socket Connection Not Successful on $server." | Out-File $log_file -width 240 -Append #logs the result
the $result
            }
            }
          
        } else {
            echo "Socket connection failed."
            echo "Ping connection failed on $server." | Out-File $log_file -width 240 -Append #logs the result
        }

}
}
echo "Finished Ping Failure checks" >> "$log_file"
echo "########################################" >> "$log_file"
echo "Starting Logons Disabled checks" >> "$log_file"
foreach($server in $servers)
 {
    echo "`n**** $server ****"
if($server.LogOnsEnabled -eq $false)
  {
        Write-Host "Logons are disabled on this server!" -ForegroundColor Red
  echo "Logons are disabled on $server! " | Out-File $log_file -width 240 -Append #logs the result
     }
 }
echo "Finished Logons Disabled checks" >> "$log_file"
##########################################
###        Gather Disk Information     ###
##########################################
Write-Host “Servers Low on Disk Space"
echo "########################################" >> "$log_file"
echo "Starting Low Disk Space checks" >> "$log_file"
$percentWarning = 10
foreach($server in $servers)
{
 # Get fixed drive info
 $disks = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk -Filter "DriveType = 3";

 foreach($disk in $disks)
 {
  $deviceID = $disk.DeviceID;
  [float]$size = $disk.Size;
  [float]$freespace = $disk.FreeSpace;

  $percentFree = [Math]::Round(($freespace / $size) * 100, 2);
  $sizeGB = [Math]::Round($size / 1073741824, 2);
  $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);

  $colour = "Green";
  if($percentFree -lt $percentWarning)
  {
   $colour = "Red";
   Write-Host -ForegroundColor $colour "$server $deviceID percentage free space = $percentFree" >>"$log_file";
   echo "$server $deviceID % free space = $percentFree" >>"$log_file";
   echo "running delprof on $server">>$log_file

  delprof /q /i /c:$server

  $disks = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk -Filter "DriveType = 3"

  foreach($disk in $disks)
  {
  $deviceID = $disk.DeviceID;
  [float]$size = $disk.Size;
  [float]$freespace = $disk.FreeSpace;

  $percentFree = [Math]::Round(($freespace / $size) * 100, 2);
  $sizeGB = [Math]::Round($size / 1073741824, 2);
  $percentFree = [Math]::Round(($freespace / $size) * 100, 2);
    
   echo "$server $deviceID drive now has $percentFree % Free Space">>$log_file
  }}
  else
   {
   Write-Host -ForegroundColor $colour "$server $deviceID percentage free space = $percentFree";
   }

 }
}
echo "Finished Low Disk Space checks" >> "$log_file"
echo "########################################" >> "$log_file"
echo "Starting Offline Server checks" >> "$log_file"
invoke-expression "$tool $cmdLine" >> "$log_file"
echo "Finished Offline Server checks" >> "$log_file"
echo "########################################" >> "$log_file"
echo "Starting 24 Hours Rebooted Server checks" >> "$log_file"
foreach($Server in $Servers)

{

$wmi=Get-WmiObject -class Win32_OperatingSystem -computer $server

$LBTime=$wmi.ConvertToDateTime($wmi.Lastbootuptime)

[TimeSpan]$uptime=New-TimeSpan $LBTime $(get-date)
[int]$a=$uptime.Days
[int]$b=$uptime.Hours
[int]$c=$uptime.Minutes
[int]$d=$uptime.Seconds
[int]$e=$a*24+$b+$c/60+$d/3600
if ($e -le 24)
{
echo "`n$server :It was rebooted $e hours before. At :$LBTime" >> "$log_file"
}
else
{
echo "No Server found." >> "$log_files"
}
}
echo "########################################" >> "$log_file"
echo "########################################" >> "$log_file"
echo "Starting XenApp Custom Advanced LoadEvaluator checks" >> "$log_file"
foreach ($server in $servers)
{
$load=Get-XALoadEvaluator -ServerName $server | where-object {$_.LoadEvaluatorName -ne "XenApp Custom Advanced"}
if ($load -ne $null)
{
Write-Host "Found"
echo "`n$server has $load loadevaluator " >> "$log_file"
}
}
echo "Finished XenApp Custom Advanced LoadEvaluator checks" >> "$log_file"
echo "########################################" >> "$log_file"
$Date2 = Get-Date
echo "Finished Server Health script at Date: $Date2" >> "$log_file"
echo "Sending email"
$smtpServer = "SMTP server"
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($log_file)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "from mail address"
$msg.To.Add("to mail address")
$msg.Subject = "Citrix Server Testing"
$msg.Body = "Citrix Server Testing"
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()



Download

No comments:

Post a Comment