Skip to content

Commit fa3865a

Browse files
author
Markus Fleschutz
committed
Update check-ping.ps1
1 parent 140f470 commit fa3865a

File tree

1 file changed

+17
-32
lines changed

1 file changed

+17
-32
lines changed

Scripts/check-ping.ps1

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,41 @@
22
.SYNOPSIS
33
Checks the ping latency
44
.DESCRIPTION
5-
This PowerShell script checks the ping latency from the local computer to 9 popular hosts.
5+
This PowerShell script checks the ping latency from the local computer to 10 popular hosts.
66
.PARAMETER hosts
7-
Specifies the hosts to check, seperated by commata (default is: amazon.com,bing.com,cnn.com,dropbox.com,facebook.com,google.com,live.com,twitter.com,youtube.com)
7+
Specifies the hosts to check, seperated by commata (default is: amazon.com,bing.com,cnn.com,dropbox.com,facebook.com,github.com,google.com,live.com,twitter.com,youtube.com)
88
.EXAMPLE
99
PS> ./check-ping
10-
✅ Ping latency is 13ms...109ms with 25ms average.
10+
✅ Ping latency is 13ms...109ms with 25ms average
1111
.LINK
1212
https://github.com/fleschutz/PowerShell
1313
.NOTES
1414
Author: Markus Fleschutz | License: CC0
1515
#>
1616

17-
param([string]$hosts = "amazon.com,bing.com,cnn.com,dropbox.com,facebook.com,google.com,live.com,twitter.com,youtube.com")
17+
param([string]$hosts = "amazon.com,bing.com,cnn.com,dropbox.com,facebook.com,github.com,google.com,live.com,twitter.com,youtube.com")
1818

1919
try {
20-
Write-Progress "⏳ Sending pings to 9 popular hosts..."
20+
Write-Host "✅ Ping latency is" -noNewline
2121
$HostsArray = $hosts.Split(",")
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
4122

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 }
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
4632
if ($Latency -lt $Min) { $Min = $Latency }
4733
if ($Latency -gt $Max) { $Max = $Latency }
4834
$Avg += $Latency
35+
$Count++
4936
}
50-
$Avg /= $Pings.count
5137
}
52-
53-
Write-Progress -Completed "."
54-
Write-Host "✅ Ping latency is $($Min)ms...$($Max)ms with $($Avg)ms average"
38+
$Avg /= $Count
39+
Write-Host " $($Min)ms...$($Max)ms with $($Avg)ms average"
5540
exit 0 # success
5641
} catch {
5742
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"

0 commit comments

Comments
 (0)