PowerShell/scripts/ping-remote-hosts.ps1

49 lines
1.6 KiB
PowerShell
Raw Normal View History

2024-10-01 15:11:03 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
Pings remote hosts to measure the latency
2021-10-04 21:29:23 +02:00
.DESCRIPTION
This PowerShell script measures the ping roundtrip times from the local computer to remote ones (10 Internet servers by default).
2021-10-16 16:50:10 +02:00
.PARAMETER hosts
2024-01-03 11:30:45 +01:00
Specifies the hosts to ping, seperated by commata (10 Internet servers by default)
2021-07-13 21:10:02 +02:00
.EXAMPLE
PS> ./ping-remote-hosts.ps1
2024-08-21 17:43:49 +02:00
Online with 11...40ms latency - 18ms average
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(",")
2024-09-03 18:06:07 +02:00
$tasks = $hostsArray | foreach { (New-Object Net.NetworkInformation.Ping).SendPingAsync($_,1000) }
2023-06-03 08:51:05 +02:00
[int]$min = 9999999
2023-09-25 21:08:20 +02:00
[int]$max = [int]$avg = [int]$success = 0
[int]$total = $hostsArray.Count
2024-05-07 10:01:06 +02:00
[Threading.Tasks.Task]::WaitAll($tasks)
foreach($ping in $tasks.Result) {
2023-09-25 21:08:20 +02:00
if ($ping.Status -ne "Success") { continue }
$success++
[int]$latency = $ping.RoundtripTime
$avg += $latency
if ($latency -lt $min) { $min = $latency }
if ($latency -gt $max) { $max = $latency }
2021-03-30 09:06:30 +02:00
}
2023-09-25 21:08:20 +02:00
[int]$loss = $total - $success
2024-08-21 17:43:49 +02:00
if ($success -eq 0) {
2024-10-01 17:04:39 +02:00
Write-Host "⚠️ Internet offline (100% ping loss)"
2024-08-21 17:43:49 +02:00
} elseif ($loss -eq 0) {
2023-09-25 21:08:20 +02:00
$avg /= $success
2024-10-01 17:04:39 +02:00
Write-Host "✅ Internet latency $($min)...$($max)ms, average is $($avg)ms"
2023-09-12 22:07:26 +02:00
} else {
2024-08-21 17:43:49 +02:00
$avg /= $success
Write-Host "✅ Online with $loss/$total ping loss and $($min)...$($max)ms latency - $($avg)ms average"
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
}