Conrad_Bel
asked on
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?
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?
ASKER
will this work pinging 3 different IP's?
No, but you can just start three different instances.
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?
ASKER
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.
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
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.
ASKER
Awesome i will give that a whirl and get back to you as soon as I can. Cheers.
Any updates?
ASKER
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.
ASKER
Bob,
I'm back to normal today. I will start testing the script today. Sorry for all the delays after your hard work.
I'm back to normal today. I will start testing the script today. Sorry for all the delays after your hard work.
ASKER
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.
ASKER
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Your script works great Bob !
THanks.
THanks.
ASKER
Thanks Bob. Sorry for the delayed closing of this. Appreciate all your assist.
Open in new window