Skip to content

Commit 140f470

Browse files
committed
Update check-ping.ps1
1 parent 9ef8b1f commit 140f470

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

Scripts/check-ping.ps1

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,43 @@
1717
param([string]$hosts = "amazon.com,bing.com,cnn.com,dropbox.com,facebook.com,google.com,live.com,twitter.com,youtube.com")
1818

1919
try {
20-
Write-Progress "⏳ Sending a ping to 9 popular hosts..."
20+
Write-Progress "⏳ Sending pings to 9 popular hosts..."
2121
$HostsArray = $hosts.Split(",")
22-
$Pings = Test-Connection -computerName $HostsArray -count 1
22+
if ($IsLinux) {
23+
$t = $HostsArray | foreach {
24+
(New-Object Net.NetworkInformation.Ping).SendPingAsync($_, 250)
25+
}
26+
[Threading.Tasks.Task]::WaitAll($t)
27+
[int]$Min = 9999999
28+
[int]$Max = [int]$Avg = [int]$Count = 0
29+
foreach($ping in $t.Result) {
30+
if ($ping.Status -eq "Success") {
31+
[int]$Latency = $ping.RoundtripTime
32+
if ($Latency -lt $Min) { $Min = $Latency }
33+
if ($Latency -gt $Max) { $Max = $Latency }
34+
$Avg += $Latency
35+
$Count++
36+
}
37+
}
38+
$Avg /= $Count
39+
} else {
40+
$Pings = Test-Connection -computerName $HostsArray -count 1
2341

24-
[int]$Min = 9999999
25-
[int]$Max = [int]$Avg = 0
26-
foreach($Ping in $Pings) {
27-
if ($IsLinux) { [int]$Latency = $Ping.latency } else { [int]$Latency = $Ping.ResponseTime }
28-
if ($Latency -lt $Min) { $Min = $Latency }
29-
if ($Latency -gt $Max) { $Max = $Latency }
30-
$Avg += $Latency
42+
[int]$Min = 9999999
43+
[int]$Max = [int]$Avg = 0
44+
foreach($Ping in $Pings) {
45+
if ($IsLinux) { [int]$Latency = $Ping.latency } else { [int]$Latency = $Ping.ResponseTime }
46+
if ($Latency -lt $Min) { $Min = $Latency }
47+
if ($Latency -gt $Max) { $Max = $Latency }
48+
$Avg += $Latency
49+
}
50+
$Avg /= $Pings.count
3151
}
32-
$Avg /= $Pings.count
3352

3453
Write-Progress -Completed "."
3554
Write-Host "✅ Ping latency is $($Min)ms...$($Max)ms with $($Avg)ms average"
3655
exit 0 # success
3756
} catch {
3857
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
3958
exit 1
40-
}
59+
}

0 commit comments

Comments
 (0)