Powershell or Batch script to ping IP's for a certain lenght of time

Conrad_Bel
Conrad_Bel used Ask the Experts™
on
Does anyone have a batch or powershell script to ping an ip for a time period (i.e. 8 hours) and output to a file.

I'm trying to ping heart beat IP's throughout the day to troubleshoot possible network issues.  

Is there a switch for the ping -t command that will run for say 8 hours then stop?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Here's a batch script; it doesn't stop after 8 hours, but then again, it will only log status changes, so the log shouldn't be too big. Just double-click it, it will ask for a host to ping and then create a log file in the script's folder.
@echo off
setlocal
mode con cols=40 lines=25
if "%~1"=="" (
	set /p IP=Name or IP: 
) else (
	set IP=%~1
)
title Ping %IP%
set LogFile=%~dpn0-%IP%.log
set Lost=X
:loop
ping.exe -n 1 %IP% | find /i "TTL" >NUL
if errorlevel 1 (
	if not %Lost%==1 (
		color 4F
		echo - Down: %Date% %Time: =0%
		>>"%LogFile%" echo - %Date% %Time%
		set Lost=1
	) 
) else (
	if not %Lost%==0 (
		color 2F
		echo + Up: %Date% %Time: =0%
		>>"%LogFile%" echo + %Date% %Time%
		set Lost=0
	)
	ping.exe localhost -n 2 >NUL
)
goto :loop

Open in new window

Author

Commented:
will this work pinging 3 different IP's?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
No, but you can just start three different instances.
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

First of all I would use Test-Connection in PowerShell, not ping.
Is this just for one server, or multiple?
Is it always going to be a fixed time to stop?  That is, can it be coded in the script or do you want to provide that at start time?
What culture, e.g., US?
What do you want the output to be?

Author

Commented:
Bob,
I am pinging 3 different IP's (no DNS name so has to be IP) and would want to ping or test connection from 07:00am EST to 5:00pm EST (US).  I would ideally want a start and stop time.

The output could be whatever is easiest to tell me if the test or ping failed and when or what OBDA stated as to log only failures if that's possible.
How often do you want it to run?  Once a minute?  Every five seconds?
OK, here's what I came up with.
$serverList = "192.168.50.154","192.168.50.239","192.168.55.254"
#$stopTime = Get-Date -Hour 17 -Minute 0 -Second 0
$stopTime = (Get-Date).AddSeconds(60)
$pass = 0

$results = do {
    $now = Get-Date
    $pass++
    foreach ($server in $serverList)
    {
        $responseTime = ((Test-Connection $server -ErrorAction SilentlyContinue).ResponseTime | Measure-Object -Average).Average
        Write-Verbose "$("[{0:D5}] {1,16} {2,7}" -f $pass, $server, $responseTime)"
        [PSCustomObject]@{
            Computer = $server
            TimeStamp = $now.ToShortTimeString()
            Time = $responseTime
        }
    }
    Start-Sleep -Seconds 4
} until ($now -gt $stopTime)

# Sample outputs - pick one or more, your choice
$results
$results | Format-Table -AutoSize
$results | Out-GridView
$results | Export-Csv -Path .\foo.csv -NoTypeInformation -Encoding ASCII
$results | Out-File -FilePath .\foo.txt -Encoding ASCII
$results | Export-Clixml -Path .\foo.xml -Encoding ASCII

Open in new window

Change the first line to put in your IP addresses.
The script doesn't include a start time.  It starts whenever you start it.  That makes it easier to kick off as a scheduled task.
I let Test-Connection run it's standard 4 pings and then I average the results.
Computers that fail to respond will have a blank in the Time column rather than a number.
That will take about 12 seconds for three computers (1 second per ping per computer).
At the bottom of the loop I set the script to sleep for 4 seconds before starting over again.
There is NO intermediate output file.  It is all captured in the $results object and output at the end of the run.
I have included several different output possibilities at the bottom of the script.  Pick whichever one(s) appeal to you.
If you want to watch it interactively while it's running, set $VerbosePreference=2.
The verbose listing includes the pass number in brackets.
Line 2 is commented out.  That controls when the script will stop.  For testing purposes line 3 sets the stop time.  Remove or comment out that line when you are satisfied the script does what you want.  At the same time, uncomment line 2.

Author

Commented:
Awesome   i will give that a whirl and get back to you as soon as I can.  Cheers.
Any updates?

Author

Commented:
Not yet.  Been off work and my home lab is out of comish right now.  I will try this as soon as i get back to work.  Thanks very much for following up.  I will keep you posted.

Author

Commented:
Bob,
I'm back to normal today.  I will start testing the script today.  Sorry for all the delays after your hard work.

Author

Commented:
Were should i input the $VerbosePreference=2 option in the script if i did want to watch it interactively?
You can put it at the top of the script.  Or you can enter it in the console window before running the script.

Author

Commented:
So i am testing and have 2 questions:

1. i noticed in the results it doesn't have the ping time <1ms etc...  Is there a way for that to show up in results?

2.  When i change the time in line 2 for 1 or 2 (which i thought where hours) it runs once and stops.  How does that line work?  How can i set it to run for 23 hours?  Also, when i did set the time in line 2 to 23 hours and manually stopped the script, it didn't write the log.  

Any ideas?
I included 11 comments after the script.  Please go back and read them.

The last bullet points out that line 2 is commented out.  You will want to uncomment this line to run it in production and remove or comment out line 3.  Line 3 currently allows the script to run for 60 seconds for demo/testing purposes.

Commented:
Your script works great Bob !
THanks.

Author

Commented:
Thanks Bob.  Sorry for the delayed closing of this.  Appreciate all your assist.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial