Ping Checking with PowerShell [Updated 9/25/07]

I wanted a better way to watch pings while doing stuff on another monitor. I was tired of looking back and scrolling up. So, Check-Pings.ps1 was born. This script simply pings the first given variable in a loop and outputs the last successful ping time. However, if an error occurs, the time and date is printed out for logging purposes. This is a lot better than ping -t because it doesn't spam your screen too much and you'll know 100% if a ping drops. So, here it is.

Check-Pings Picture

This script uses spaces, which don't agree with my blog too much. Click the PowerShell icon to download the .ps1 file that will include the spaces needed. Oh, what the hay, you can just click here too.
Download Check-Pings.ps1

Check-Pings.ps1

$target = $args

if ((($target).length) -lt "1") {
Write-Warning "No Target IP Specified."
Write-Warning "EXAMPLE: Check-Pings.ps1 www.google.com"
break
}

$didnt = 0
$worked = 0
$datetime = (get-date)

write-host "`nLoading..." -nonewline -foregroundcolor yellow
$ping = new-object System.Net.NetworkInformation.Ping
write-host "`rStarted ping to $target $datetime`n"

while (1) {

$go = $ping.send("$target")
$result = $go.status
$ms = $go.roundtriptime
$ip = ($ping.send("$target").address).ipaddresstostring

if ($result -match "Success") {
$time = (get-date)
$worked = ($worked + 1)
if ($didnt -gt "0") {
$failrate = ( ($didnt / $worked ) * 100).tostring(".0")
write-host `r"Ping Successful ($ip) - $target - $time - $failrate% Loss - $ms ms " -nonewline -foregroundcolor green
write-host " " -nonewline
}
else {
write-host `r"Ping Successful ($ip) - $target - $time - 0% Loss - $ms ms " -nonewline -foregroundcolor green
write-host " " -nonewline
}
}
Else {
$time = (get-date)
$didnt = ($didnt + 1)
if ($worked -gt "0") {
$failrate = ( ($didnt / $worked ) * 100).tostring(".0")
write-host `r"Ping Failed ($ip) - $target - $time - $failrate% Loss ($didnt lost) "`n -nonewline -foregroundcolor red
write-host " " -nonewline
}
else {
$didnt = ($didnt - 1)
write-host `r"$target ($ip) - $time - Host not sucessfully pinged yet. " -nonewline -foregroundcolor red
}
}
sleep -milliseconds 500
}

Example Useage:

Check-Pings.ps1 google.com

Edit: Yeah, my ISP is Comcast
Edit: Updated to work with the .net ping object thanks to Shay (http://scriptolog.blogspot.com)