PowerShell/Scripts/check-ping.ps1

47 lines
1.4 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2022-10-28 12:39:58 +02:00
Checks the ping latency
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2023-06-02 12:41:34 +02:00
This PowerShell script checks the ping latency from the local computer to 10 popular hosts.
2021-10-16 16:50:10 +02:00
.PARAMETER hosts
2023-06-02 12:41:34 +02:00
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)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-09-24 17:19:49 +02:00
PS> ./check-ping
2023-06-02 13:09:21 +02:00
Ping latency is 13ms...109ms with 29ms average (0 loss)
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-03-30 09:06:30 +02:00
#>
2023-06-02 12:41:34 +02:00
param([string]$hosts = "amazon.com,bing.com,cnn.com,dropbox.com,facebook.com,github.com,google.com,live.com,twitter.com,youtube.com")
2021-09-21 20:19:39 +02:00
2021-03-30 09:06:30 +02:00
try {
2023-06-02 12:41:34 +02:00
Write-Host "✅ Ping latency is" -noNewline
2021-09-21 20:19:39 +02:00
$HostsArray = $hosts.Split(",")
2021-03-30 09:06:30 +02:00
2023-06-02 12:41:34 +02:00
$t = $HostsArray | foreach {
(New-Object Net.NetworkInformation.Ping).SendPingAsync($_, 250)
}
[Threading.Tasks.Task]::WaitAll($t)
[int]$Min = 9999999
2023-06-02 13:09:21 +02:00
[int]$Max = [int]$Avg = [int]$SuccessCount = [int]$LossCount = 0
2023-06-02 12:41:34 +02:00
foreach($ping in $t.Result) {
if ($ping.Status -eq "Success") {
[int]$Latency = $ping.RoundtripTime
2023-06-02 12:29:49 +02:00
if ($Latency -lt $Min) { $Min = $Latency }
if ($Latency -gt $Max) { $Max = $Latency }
$Avg += $Latency
2023-06-02 13:09:21 +02:00
$SuccessCount++
} else {
$LossCount++
2023-06-02 12:29:49 +02:00
}
2021-03-30 09:06:30 +02:00
}
2023-06-02 13:09:21 +02:00
$Avg /= $SuccessCount
Write-Host " $($Min)ms...$($Max)ms with $($Avg)ms average ($LossCount loss)"
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-03-30 09:06:30 +02:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-03-30 09:06:30 +02:00
exit 1
2023-06-02 12:29:49 +02:00
}