Reputation: 599
I have the following code:
$Servers = "8.8.8.8"
$TimeStart = Get-Date
$TimeEnd = $TimeStart.AddMinutes(1)
Do {
Foreach($s in $Servers) {
$Timeout = 1
$Ping = New-Object System.Net.NetworkInformation.Ping
$Response = $Ping.Send($s,$Timeout)
$Response.Status
$TimeNow = Get-Date
}
}
Until ($TimeNow -ge $TimeEnd)
But it seems to run too often. Ideally I would like a ping to take place for every second, for a minute, and if the ping takes longer than 5 milliseconds, log 'TimedOut' and anything below 5 milliseconds to log 'Success'. Also, outputting the date would be useful too.
How would I manipulate this code to achieve this?
Upvotes: 1
Views: 305
Reputation: 8432
You can use Test-Connection
to do this. Here's one way, with slight modifications to get the additional info you want:
Test-Connection -ComputerName $servers -Delay 1 -Count 60 |
Add-Member -MemberType NoteProperty -Name Date -Value (Get-Date) -PassThru |
Add-Member -MemberType NoteProperty -Name Status -Value $(if($_.ResponseTime -gt 5){"Failed"}else{"Succeeded"}) -PassThru
This will ping each server in $servers
once per second for 60 seconds, and output the standard objects, with two new properties: Date
(datetime of the ping) and Status
(did it succeed or fail?)
You can capture these, display them onscreen or send to file. For example to log the data by saving to CSV, you could just append the following:
| Export-Csv .\ping.csv
The CSV will contain more information than you need, so you can either be selective in what you export, or simply ignore what you don't want and use the parts you do.
EDIT: Displaying the custom properties
To see the custom properties in the console, append the following:
| Format-Table PsComputerName, Date, ResponseTime, Status
Upvotes: 4
Reputation: 6860
This one uses Workflow to allow for parallel pings. It also takes into account that that System.Net.NetworkInformation.Ping timeout isnt exact when it comes to low millisecond use. I suggest working BoxDogs Answer since its the most inline with powershell standards. But I wanted to join in the fun and learning.
workflow AdvPing([string[]]$Servers, [timespan]$RunTimeLength, [timespan]$Freqency, [timespan]$PingTimeout){
foreach -parallel ($Server in $Servers)
{
$Runtime = (get-date).Add($RunTimeLength)
$CurrentDate = get-date
$Timeout = $PingTimeout
$Ping = New-Object System.Net.NetworkInformation.Ping
while($CurrentDate -le $RunTime){
$Response = ""
$ResponseMessage = ""
$CurrentFreqency = (Get-Date).add($Freqency)
try{
$Response = $Ping.Send($Server,$Timeout.TotalMilliseconds)
if(($Response.RoundtripTime -gt $PingTimeout.Milliseconds) -and ($Response.Status -like "Success")){
$ResponseMessage = "TimeOut"
}else{
$ResponseMessage = $Response.Status
}
$Reply = New-Object psobject -Property @{
Server = $Server
Response = $ResponseMessage
Date = $(get-date).DateTime
Time = $Response.RoundtripTime
Exception = $null
}
}catch{
$Reply = New-Object psobject -Property @{
Server = $Server
Response = "No Host"
Date = $(get-date).DateTime
Time = $null
Exception = $_
}
}
$Reply
$CurrentDate = Get-Date
[timespan]$Wait = $CurrentFreqency - $CurrentDate
try{
sleep -Milliseconds $Wait.TotalMilliseconds
}catch{}
}
}
}
$Servers = @("8.8.8.8", "8.8.4.4", "StackOverflow.com","243.42.432.44")
advping -Servers $Servers -RunTimeLength ([TimeSpan]::FromSeconds(60)) -Freqency ([TimeSpan]::FromSeconds(1)) -PingTimeout ([TimeSpan]::FromMilliseconds(5)) | format-table Date,Server,Response,Time
The output will look like
Date Server Response Time
---- ------ -------- ----
Friday, July 13, 2018 2:53:37 PM 243.42.432.44 No Host
Friday, July 13, 2018 2:53:37 PM StackOverflow.com TimeOut 15
Friday, July 13, 2018 2:53:37 PM 8.8.4.4 Success 2
Friday, July 13, 2018 2:53:37 PM 8.8.8.8 Success 2
Friday, July 13, 2018 2:53:38 PM 243.42.432.44 No Host
Friday, July 13, 2018 2:53:38 PM StackOverflow.com TimeOut 15
Friday, July 13, 2018 2:53:38 PM 8.8.4.4 Success 2
Friday, July 13, 2018 2:53:38 PM 8.8.8.8 Success 2
Upvotes: 0
Reputation:
The following script uses Test-Connection with -Count 1
and configurable Delay between tests and also a treshold to define Success/Timeout.
## Q:\Test\2018\07\13\SO_51327101.ps1
$Servers = "8.8.8.8","8.8.4.4","62.220.18.8","89.246.64.8"
$TimeStart = Get-Date
$TimeEnd = $TimeStart.AddMinutes(2)
$Treshold = 12
$DelayMS = 1500
Do {
Foreach($Server in $Servers) {
$Response = Test-Connection $Server -Count 1
$Status = 'Success '
If ($Response.ResponseTime -gt $Treshold){$Status = 'TimedOut'}
"[{0}] {1} Server: {2,15} Responsetime: {3,3} ms" -f `
(Get-Date -f yyyyMMddHHmmss),
$Status,
$Server,
$Response.ResponseTime
}
Start-Sleep -Milliseconds $DelayMS
} Until ((Get-Date) -ge $TimeEnd)
Sample output
[20180713172651] TimedOut Server: 89.246.64.8 Responsetime: 13 ms
[20180713172653] Success Server: 8.8.8.8 Responsetime: 12 ms
[20180713172653] Success Server: 8.8.4.4 Responsetime: 12 ms
[20180713172653] Success Server: 62.220.18.8 Responsetime: 7 ms
[20180713172653] TimedOut Server: 89.246.64.8 Responsetime: 13 ms
Upvotes: 1