Frequently I work on issues where there is a problem connecting to certain network resources, but the symptoms aren’t necessarily conveyed in an empirical manner. “We sometimes have issues connecting to Server1” or “Once in a while I can’t get to a web page, but refresh works,” etc.
The go-to tool is frequently “ping,” as that lets you know quickly if something is accessible or not (assuming no firewall restrictions related to ICMP, etc.).
What should be done if the problem is happening infrequently or if multiple servers are involved? PING doesn’t provide a good history of when things were responding and stopped responding. That’s where wrapping some code around the PowerShell command Test-Connection comes in handy.
Copy the content below and put into a file “Ping.ps1”.
[parameter(Mandatory=$true,ValueFromPipeline=$false,HelpMessage=’HostName or IP Address’)][string]$HostName
# John Fedor – Peters & Associates
[console]::BackgroundColor = “black”
$CurrentState = Test-Connection -ComputerName $HostName -Count 1 -Quiet
if ($CurrentState -ne $LastState)
$Now = Get-Date
if ($LastTime -eq $Null)
$Delta = “”
$Delta = “: Duration was ” + ($Now-$LastTime).TotalSeconds + ” seconds”
if ($CurrentState -eq $true)
write-host “Responding ” (get-date) $Delta -ForegroundColor Green
write-host “Stopped ” (get-date) $Delta -ForegroundColor Red
$LastState = $CurrentState
$LastTime = $Now
# Give the CPU a break
Sleep -Seconds 1
while (1 -eq 1)
Using the script
The easiest way to test the script is to launch a PowerShell prompt and simply run:
.\ping.ps1 -HostName Server1
This will start the process. It will continue to ping the specified resource until you escape out (CTL-C or close the window). Whenever there is a change (starts responding or stops responding), the time/date will show up, along with how long the prior step was running.
If we look at the above sample output, we can see we initially started the ping and it was successful at 10:27:22 AM.
Then, at 10:27:28 AM, it stopped responding to pings. The duration of the success was 6.1278674 seconds.
Then, at 10:27:30 AM, it started responding to pings again. The duration of the failure was 2.0373406 seconds
By getting just the detailed timestamps of when the response status changes, you can have this run for hours/days without filing up the screen (depending on how stable the connection has been).
How does this help?
With this tool, you can now launch multiple windows with various PINGs being performed from a server. Each window can hit various resources on the network (layer 3 devices, servers, etc.) to help identify where the problem may lie, and the time/date stamps may provide correlation of problems.
You can also run this from other machines to get a different perspective of the issue. Alternatively, you can wrap it in a batch to launch periodically for testing purposes, such as:
start powershell -command C:\Scripts\ping.ps1 -HostName Server1
start powershell -command C:\Scripts\ping.ps1 -HostName Server2
start powershell -command C:\Scripts\ping.ps1 -HostName Server3
Using tools like this, we’ve narrowed issues down to a particular 1-2 devices to provide more focus on troubleshooting, see patterns in issues (and discovered scheduled reboots as a result), and other uses.
This script is a starting point. We can modify the script to test other resources so it’s not dependent on ICMP echo & echo-reply being passed through firewalls, etc. This method may show clearer issues to service availability, not just the server.
Need assistance in hunting down network issues or other assistance? Email firstname.lastname@example.org. We are happy to help.