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

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?
Conrad_BelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
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

Conrad_BelAuthor Commented:
will this work pinging 3 different IP's?
oBdACommented:
No, but you can just start three different instances.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Bob McCoyCommented:
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?
Conrad_BelAuthor 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.
Bob McCoyCommented:
How often do you want it to run?  Once a minute?  Every five seconds?
Bob McCoyCommented:
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.
Conrad_BelAuthor Commented:
Awesome   i will give that a whirl and get back to you as soon as I can.  Cheers.
Bob McCoyCommented:
Any updates?
Conrad_BelAuthor 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.
Conrad_BelAuthor Commented:
Bob,
I'm back to normal today.  I will start testing the script today.  Sorry for all the delays after your hard work.
Conrad_BelAuthor Commented:
Were should i input the $VerbosePreference=2 option in the script if i did want to watch it interactively?
Bob McCoyCommented:
You can put it at the top of the script.  Or you can enter it in the console window before running the script.
Conrad_BelAuthor 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?
Bob McCoyCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Senior IT System EngineerSenior Systems EngineerCommented:
Your script works great Bob !
THanks.
Conrad_BelAuthor Commented:
Thanks Bob.  Sorry for the delayed closing of this.  Appreciate all your assist.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.