|
2 | 2 | .SYNOPSIS
|
3 | 3 | Checks the ping latency
|
4 | 4 | .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. |
6 | 6 | .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) |
8 | 8 | .EXAMPLE
|
9 | 9 | PS> ./check-ping
|
10 |
| - ✅ Ping latency is 13ms...109ms with 25ms average. |
| 10 | + ✅ Ping latency is 13ms...109ms with 25ms average |
11 | 11 | .LINK
|
12 | 12 | https://github.com/fleschutz/PowerShell
|
13 | 13 | .NOTES
|
14 | 14 | Author: Markus Fleschutz | License: CC0
|
15 | 15 | #>
|
16 | 16 |
|
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") |
18 | 18 |
|
19 | 19 | try {
|
20 |
| - Write-Progress "⏳ Sending pings to 9 popular hosts..." |
| 20 | + Write-Host "✅ Ping latency is" -noNewline |
21 | 21 | $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 |
41 | 22 |
|
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 |
46 | 32 | if ($Latency -lt $Min) { $Min = $Latency }
|
47 | 33 | if ($Latency -gt $Max) { $Max = $Latency }
|
48 | 34 | $Avg += $Latency
|
| 35 | + $Count++ |
49 | 36 | }
|
50 |
| - $Avg /= $Pings.count |
51 | 37 | }
|
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" |
55 | 40 | exit 0 # success
|
56 | 41 | } catch {
|
57 | 42 | "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
0 commit comments