mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-04-23 18:38:19 +02:00
Updated ping-host.ps1
This commit is contained in:
parent
15a764c5a5
commit
28c533d906
@ -7,7 +7,7 @@
|
|||||||
Specifies the hostname or IP address to ping (x.com by default)
|
Specifies the hostname or IP address to ping (x.com by default)
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
PS> ./ping-host.ps1 x.com
|
PS> ./ping-host.ps1 x.com
|
||||||
✅ Host 'x.com' is up with 23ms ping latency.
|
✅ Host 'x.com' is UP (20ms latency to IP 104.244.42.65).
|
||||||
.LINK
|
.LINK
|
||||||
https://github.com/fleschutz/PowerShell
|
https://github.com/fleschutz/PowerShell
|
||||||
.NOTES
|
.NOTES
|
||||||
@ -16,22 +16,18 @@
|
|||||||
|
|
||||||
param([string]$hostname = "x.com")
|
param([string]$hostname = "x.com")
|
||||||
|
|
||||||
function GetPingLatency([string]$hostname) {
|
|
||||||
$hostsArray = $hostname.Split(",")
|
|
||||||
$tasks = $hostsArray | foreach { (New-Object Net.NetworkInformation.Ping).SendPingAsync($_,3000) }
|
|
||||||
[Threading.Tasks.Task]::WaitAll($tasks)
|
|
||||||
foreach($ping in $tasks.Result) { if ($ping.Status -eq "Success") { return $ping.RoundtripTime } }
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
[int]$latency = GetPingLatency($hostname)
|
$remoteHosts = $hostname.Split(",")
|
||||||
if ($latency -lt 0) {
|
$tasks = $remoteHosts | foreach { (New-Object Net.NetworkInformation.Ping).SendPingAsync($_,3000) }
|
||||||
Write-Host "⚠️ Host '$hostname' doesn't respond - check the connection or maybe the host is down."
|
[Threading.Tasks.Task]::WaitAll($tasks)
|
||||||
exit 1
|
foreach($ping in $tasks.Result) {
|
||||||
}
|
if ($ping.Status -eq "Success") {
|
||||||
Write-Host "✅ Host '$hostname' is up with $($latency)ms ping latency."
|
Write-Host "✅ Host '$hostname' is UP ($($ping.RoundtripTime)ms latency to IP $($ping.Address))."
|
||||||
exit 0 # success
|
exit 0 # success
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Write-Host "⚠️ Host '$hostname' doesn't respond - check the connection or maybe the host is down (yet)."
|
||||||
|
exit 1
|
||||||
} catch {
|
} catch {
|
||||||
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||||
exit 1
|
exit 1
|
||||||
|
Loading…
Reference in New Issue
Block a user