Karla News

PowerShell Ping Program – a Free Alternative to Plot Pings

If you’ve ever worked with networked computers and needed to test a system’s connectivity via the Ping command, you’ve likely run into the same challenges as I. There are lots of ping utility products out there such as SolarWinds, PingPlotter, PingAssist, FREEping, or just the plain ol’ command prompt. Some are free, or built into Windows, but I needed something simple for day-to-day operations.

Here’s the scenario I encounter often as a System Administrator: A user calls or puts in a work order telling me that their LAN/WAN connected computer seems to be running slow at bringing up their email, Internet or mapped drives. My next step is usually to bring up a Windows Command Prompt (On Windows 7 you’d click, Start and type in Cmd.exe” and then type in: Ping ComputerName -t This causes the computer to send a Ping request over the network to the destination computer. The “-t” option at the end tells the Ping to keep going and going and going, filling the screen and then scrolling off. By watching the Ping responses I can see whether the monitored system loses connectivity and thus begin my troubleshooting of why it’s “dropping Pings”.

But what if you miss a drop? What if you get distracted?

It’s not usually a question of “if” I’m distracted, it’s “when”. A command window can only hold so many pings before it scrolls beyond the capacity of the program’s memory buffer. There’s also no “historical” type of view, as once again the data scrolls off the screen into the data bit bucket in the sky.

I needed a simple visual solution for pinging a computer or IP address that could be launched from a Command Prompt or directly by a PowerShell script. By harnessing the power of PowerShell I came up with a solution that I utilize nearly every day in my network troubleshooting. I could have called it “Powershell Ping” or “Ping Powershell”, but, hey, why not give it a fancy name, right? And so, “PowerPing” was born!

See also  How to Delete a Skype Account

PowerPing Features:

Displays IP address (if available) after Maximum Pings have been reached
Adjustable Foreground and Background Colors
Adjustable Delay between Pings
Adjustable Maximum number of Pings (I’ve tested with 99,999,999 – but admittedly, it’s never reached the end!)
Configurable Column Width (65 works best in most cases)
Running time stamp displayed along right hand column
Top banner showing hostname, Ping Delay, and configured Ping attempts

Platforms Tested / Requirements

This has been successfully tested on Windows XP and Windows 7 32-bit using PowerShell version 2.0. In order for this to launch and run, Powershell’s ExecutionPolicy will need to be configured. I have mine set as “RemoteSigned”. (You can view and change Security Policy from within PowerShell with Get-ExecutionPolicy and Set-ExecutionPolicy.)

Getting it all Running

I generally launch my Powershell scripts from a Windows Command Prompt using the following format: “PP HostName”. In order to pull this off, you’ll need two files, both located within Windows’ path. (I keep my scripts in a specific folder and then add this folder to the system environmental variable ‘Path=’ , but you could also put the two files into the Windows or WindowsSystem directories.)

Creating the PP.Bat file:

Copy the code between the “Begin” and “End” scripting blocks, and save it as “PP.bat” (without the quotes, of course)

|| Begin PP.Bat Script

:: PowerPing Batch File – PP.BAT

@echo off

echo PowerPing.ps1 %* | powershell -Command –

|| End PP.Bat Script

Creating the PowerPing.ps1 file:

Copy the code between the “Begin” and “End” scripting blocks, and save it as “PowerPing.ps1”

PowerPing.ps1

See also  Cool Sites to Download Free Music!

|| Begin PowerPing.ps1 Script

# PowerPing by Ron Masters — http://contributor.yahoo.com/user/626225/ron_masters.html

# Color values are “Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta,

# DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White”.

$MaxPings = 3000

$PingDelaySeconds = 1

$ColumnWidth = 65

$BannerBackground = “Blue”

$ColorPingBackground = “Black”

$ColorPingForeground = “DarkGreen”

$BackgroundColorNoPing = “Black”

$ForegroundColorNoPing = “Red”

$TimeStampBackground = “Black”

$TimeStampForeground = “Gray”

$ExtraSpace = ” ”

$Name=($args[0])

$b=1

$g=1

$ResetV=1

$datetime=Get-Date -format g

$TimeNow=Get-Date -format T

$IPAddress=get-wmiobject win32_pingstatus -Filter “Address=’$Name'” | Select-Object IPV4Address | Format-list

Write-Host `r

Write-Host PowerPing “-” $name $ExtraSpace $ExtraSpace $ExtraSpace $ExtraSpace “PingDelay=”$PingDelaySeconds “Secs MaxPings=” $MaxPings $ExtraSpace -background $BannerBackground -foreground “white”

for($i=1; $i -le $MaxPings; $i++)

{$ALive=get-wmiobject win32_pingstatus -Filter “Address=’$Name'” | Select-Object statuscode

$b++

start-sleep $PingDelaySeconds

if($ALive.statuscode -eq 0)

{

# —– IP/Hostname Pinging ——————–

$TimeNow=Get-Date -format t

$ResetV++

if($ResetV -gt 1){write-host “_” -nonewline -background $ColorPingBackground -foreground $ColorPingForeground}

}

else

{

# —– IP/Hostname Not Pinging —————

$TimeNow=Get-Date -format t

#$DownTime=Get-Date -format t

#$TimeNow=$DownTime

$g++

$ResetV++

if($g -lt 3){write-host “+-” -nonewline -background $BackgroundColorNoPing -foreground “RED”}

if($g -gt 3){write-host “-” -nonewline -background $BackgroundColorNoPing -foreground “RED”}

}

if((($b -gt $ColumnWidth) -and ($ResetV -gt 2))){write-host ” ” $TimeNow -background $TimeStampBackground -foreground $TimeStampForeground `r}

if($b -gt $ColumnWidth){$b=1}

}

Write-Host `r

$IPAddress

|| End Script

Running PowerPing

From a command prompt, type PP and then the name of the system or IP address (for example: pp 192.168.1.1; or, pp www.yahoo.com; or, pp hostname) that you wish to ping. The PP.bat file will pass the hostname variable onto the PowerPing.ps1 script. First to show up should be the top banner and then – if the destination system is pingable – a series of green dashes will begin to move horizontally across the screen. When the end of the line is reached, a time stamp is displayed, and the next line begins to draw. If a system is unpingable, first a red “+” will display (I wanted something that would catch my eye), followed by more red dashes. If the system becomes reachable again, the trace line will turn green. Take a look through the photos 1 thru 5 accompanying this article for examples of different kinds of ping responses. Once the script has cycled through the maximum configured value, the IP address of the host is displayed and the script ends.

See also  LG Phoenix Review

Colors can be configured for most displayed items. I’ve included a remark statement at the beginning of the PowerPing.ps1 file that includes the possible color choices that can be used. (You’ll know they’re Remark statements when the line starts with a “#” ) Try some different color combinations. Maybe you’ll find a color combo that matches your style.

To adjust the “Ping Delay”, “Column Width” and “Maximum Pings” just change the number next to the appropriate variable, save and re-run the script.

Final Thoughts

Ever heard of the phrase, it’s better to work smarter rather than harder? I’ve explored that before in my Powershell Tips and Tricks and Listing Printers with Powershell articles. Now with PowerPing, I’ve managed to utilize the power of Powershell to give me a quick visual reference of how consistent or inconsistent a connection is. This do-it-yourself script is now an essential troubleshooting utility that will be in my system admin toolkit for a long time to come.

Happy scripting… and happy pinging!