PowerShell/Scripts/check-ping.ps1

50 lines
1.6 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-08-21 16:53:43 +02:00
This PowerShell script measures the ping roundtrip times from the local computer to other computers (10 Internet servers by default).
2021-10-16 16:50:10 +02:00
.PARAMETER hosts
2023-08-21 16:53:43 +02:00
Specifies the hosts to check, seperated by commata (default is: amazon.com,bing.com,cnn.com,dropbox.com,github.com,google.com,live.com,meta.com,x.com,youtube.com)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./check-ping.ps1
2023-08-21 16:53:43 +02:00
Ping latency is 29ms average (13ms...109ms, 0/10 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-08-21 16:53:43 +02:00
param([string]$hosts = "bing.com,cnn.com,dropbox.com,github.com,google.com,ibm.com,live.com,meta.com,x.com,youtube.com")
2021-09-21 20:19:39 +02:00
2021-03-30 09:06:30 +02:00
try {
2023-06-03 08:51:05 +02:00
$hostsArray = $hosts.Split(",")
2023-08-21 16:53:43 +02:00
$parallelTasks = $hostsArray | foreach {
2023-08-18 18:27:29 +02:00
(New-Object Net.NetworkInformation.Ping).SendPingAsync($_, 500)
2023-06-02 12:41:34 +02:00
}
2023-06-03 08:51:05 +02:00
[int]$min = 9999999
[int]$max = [int]$avg = [int]$successCount = [int]$lossCount = 0
2023-08-21 16:53:43 +02:00
[int]$totalCount = $hostsArray.Count
[Threading.Tasks.Task]::WaitAll($parallelTasks)
foreach($ping in $parallelTasks.Result) {
2023-06-02 12:41:34 +02:00
if ($ping.Status -eq "Success") {
2023-06-03 08:51:05 +02:00
[int]$latency = $ping.RoundtripTime
2023-08-21 16:53:43 +02:00
if ($latency -lt $min) { $min = $latency }
if ($latency -gt $max) { $max = $latency }
2023-06-03 08:51:05 +02:00
$avg += $latency
$successCount++
2023-06-02 13:09:21 +02:00
} else {
2023-06-03 08:51:05 +02:00
$lossCount++
2023-06-02 12:29:49 +02:00
}
2021-03-30 09:06:30 +02:00
}
2023-08-18 18:27:29 +02:00
if ($successCount -eq 0) {
2023-08-21 16:53:43 +02:00
Write-Host "⚠️ Offline ($lossCount/$totalCount loss)"
2023-08-18 18:27:29 +02:00
} else {
$avg /= $successCount
2023-08-21 16:53:43 +02:00
Write-Host "✅ Ping latency is $($avg)ms average ($($min)ms...$($max)ms, $lossCount/$totalCount loss)"
2023-08-18 18:27:29 +02:00
}
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
}