Skip to content

Instantly share code, notes, and snippets.

@jonsagara
Created May 26, 2011 06:02
Show Gist options
  • Save jonsagara/992625 to your computer and use it in GitHub Desktop.
Save jonsagara/992625 to your computer and use it in GitHub Desktop.
Pinger - Repeatedly pings the specified server until the ping is successful.
# -------------------------------------------------------------------------------------------------
# Pinger.ps1
#
# Repeatedly pings the specified server until the ping is successful.
# -------------------------------------------------------------------------------------------------
# Script params
param([String]$server)
# Script param validation
if ([String]::IsNullOrEmpty($server)) {
throw "You must specify a `$server to ping"
}
$pingOutput = @()
while ($pingOutput.Length -eq 0) {
Write-Host "Pinging $server..."
$pingOutput = @((ping $server -n 2) | where { $_ -match "TTL=[\d]{1,}" })
}
Write-Host "$server pinged successfully."
# Send an email notifying that the server is back online
$smtpClient = New-Object Net.Mail.SmtpClient
$smtpClient.Host = "smtp.example.com"
$currentUser = $env:UserName
$from = "[email protected]"
$to = "[email protected]"
$title = "$server was pinged successfully, and appears to be back up"
$body = "$server was pinged successfully, and appears to be back up"
$smtpClient.Send($from, $to, $title, $body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment