Quick Server Health Check
Posted: March 15, 2012 Filed under: PowerShell 3 CommentsThe following is a quick script I generated that allows me to do a simple health check of systems. The idea behind it was that I wanted a script that I could execute after patching systems to make sure that the systems where available and all services where running. After creating the script though I found it was a great way for me to do an early morning health check of systems. I do this by executing the script via a windows schedule task. The result of the script is them sent to me via email.
#=====================================================
$smtpServer = ‘mail.example.com’
$emailFrom = ‘monitoring@example.com’
$emailTo = ‘youremail@example.com’
$subject = ‘Server Health Check’
#=====================================================
$Servers = Get-Content “C:\PowerShell\configs\ServerList.txt”
$ServerExists = $False
$ConnException = “”
$EmailBody = ‘;;;Security Software Review;;’
$EmailBody = $EmailBody + ‘;
table {
font-family : Verdana, sans-serif;
font-size: 11px;
}
th {
background-color: #6699CC;
color: #FFFFFF;
font-family : Verdana, sans-serif;
font-size: 11px;
}
td {
font-family : Verdana, sans-serif;
font-size: 11px;
}
;’
$EmailBody = $EmailBody + ‘;’
ForEach($Server in $Servers)
{
$ServerExists = $True
#————————————————————————————————–
#Connection Test Exception
#http://blog.powershell.no/2011/10/23/test-connection-error-handling-gotcha-in-powershell-2-0/
#————————————————————————————————–
try
{
$ConnTestInfo = Test-Connection -ComputerName $Server -Count 1 -ErrorAction stop
}
catch [System.Management.Automation.ActionPreferenceStopException]
{
try
{
throw $_.exception
}
catch [System.Net.NetworkInformation.PingException]
{
$ConnException =”Catched PingException”
$ServerExists = $False
}
catch
{
$ConnException =”General catch”
$ServerExists = $False
}
}
#————————————————————————————————–
if($ServerExists)
{
try
{
$Services = Get-WmiObject -computer $Server -Class Win32_Service `
| Where-Object { $_.StartMode -eq ‘Auto’ -and ($_.State -ne ‘Running’ -or $_.Status -ne ‘OK’) -and !($_.Name -eq ‘ShellHWDetection’ -or $_.Name -eq ‘sppsvc’ -or $_.Name -eq ‘SmcService’ -or $_.Name -eq ‘SysmonLog’ -or $_.Name -like ‘clr_optimization*’)} `
| Select Name, StartMode, State, Status | Sort Name
}
catch
{
$EmailBody = $EmailBody + ‘;Unable to review services for ‘ + $Server + ‘.;’
}
if ($Services.Count -gt 1)
{
$EmailBody = $EmailBody + ‘;’ + $Server + ‘ ‘ + $ConnTestInfo.ipv4address.ipaddressToString + ‘ service check:’ + ‘;’
$EmailBody = $EmailBody + ‘;’
$EmailBody = $EmailBody + ‘;;Name;;StartMode;;State;;Status;;’
ForEach($Service in $Services)
{
$EmailBody = $EmailBody + ‘;;’ + $Service.Name + ‘;;’ + $Service.StartMode + ‘;;’ + $Service.State + ‘;;’ + $Service.Status + ‘;;’
}
$EmailBody = $EmailBody + ‘;’
}
}
else
{
$EmailBody = $EmailBody + “;$Server Does not Exists. $ConnException;”
}
}
$email = New-Object System.Net.Mail.MailMessage
$email.From = $emailFrom
$email.IsBodyHtml =$True
$email.To.Add($emailTo)
$email.Subject = $subject
$email.Body = $EmailBody
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($email)
I cant get to work the Colors Converted : background-color: #6699CC;
color: #FFFFFF;
font-family : Verdana, sans-serif;
font-size: 11px;
The formatting is not working for me….receiving the following error: The term ‘th’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that th
e path is correct and try again.
I’m thinking its a format issue from copying it from the blog post. I will have to check that. I have example scripts includes from my last SQL Saturday session. You can get to the slide deck and example scripts here. http://sqlsaturday.com/viewsession.aspx?sat=209&sessionid=12819