While rebooting our servers today as part of the monthly patch cycle, one of the servers took longer than usual to shutdown and restart. I didn’t want to waste time keeping an eye out on it so I whipped up a simple script that will first keep pinging the server (until it’s down), then keep pinging the server until it’s up, and finally send me an email when the server is reachable. Pretty straight-forward script, here it is with some commenting and extra bits tacked on:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<# .SYNOPSIS Keeps sending ICMP packets to a device you specify until it's reachable, upon which an email is sent. .DESCRIPTION Keeps sending ICMP packets to a device you specify until it's reachable, upon which an email is sent to an address you specify from an address you specify. Optionally, the script can wait for the device to be not reachable, and then start sending ICMP packets. .EXAMPLE .\Monitor-Device.ps1 -Device www.rakhesh.com -To abc@example.com -From def@example.com .EXAMPLE .\Monitor-Device.ps1 -Device www.rakhesh.com -Wait -To abc@example.com -From def@example.com .PARAMETER Device The name/ IP address of the device to query. .PARAMETER Wait To wait or not to wait before querying? .PARAMETER To To address. .PARAMETER From From address. .PARAMETER SmtpServer Optional SMTP server to use. #> [CmdletBinding()] Param ( [Parameter(Mandatory=$True)] [string]$Device, [Parameter(Mandatory=$True)] [string]$To, [Parameter(Mandatory=$True)] [string]$From, [Parameter(Mandatory=$False)] [string]$SmtpServer, [switch]$Wait ) # I had a good mind to use regexps to validate the email addresses, but then I read http://www.regular-expressions.info/email.html. :) if ($Wait) { # wait until the server is unreachable while (Test-Connection -ComputerName $Device -Quiet -Count 1 -BufferSize 16) { Write-Host "[$(get-date -format u)] Waiting for $Device to shutdown" Start-Sleep -Seconds 30 } } while (!(Test-Connection -ComputerName $Device -Quiet -Count 1 -BufferSize 16)) { Write-Host "[$(get-date -format u)] Waiting for $Device to be reachable" Start-Sleep -Seconds 30 } if ($SmtpServer) { Send-MailMessage -Subject "$Device now reachable" -To $To -From $From -SmtpServer $SmtpServer } else { Send-MailMessage -Subject "$Device now reachable" -To $To -From $From} |
In a corporate environment you do not need to specify the SMTP server as it’s automatically picked up from the $PSEmailServer
variable.
If you specify an SMTP server that requires authentication though you can pass this via the -Credential
switch to Send-MailMessage
. I didn’t add that above coz I didn’t need it but while typing this post I realize that that’s something others might find of use. Especially if you are using a 3rd party SMTP server that requires authentication.
For more Send-MailMessage
switches check out this TechNet page and blog post.
Further updates to this script will be at GitHub.