Use PowerShell to ping a device and email you when it’s up

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:

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.