mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-02-01 10:29:47 +01:00
Updated ping-host.ps1
This commit is contained in:
parent
0e47423514
commit
7e774853fc
@ -4,33 +4,33 @@
|
|||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
This PowerShell script pings the given host.
|
This PowerShell script pings the given host.
|
||||||
.PARAMETER hostname
|
.PARAMETER hostname
|
||||||
Specifies the hostname or IP address to ping (windows.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
|
||||||
✅ x.com is up and running (11ms latency).
|
✅ Host 'x.com' is up with 23ms ping latency.
|
||||||
.LINK
|
.LINK
|
||||||
https://github.com/fleschutz/PowerShell
|
https://github.com/fleschutz/PowerShell
|
||||||
.NOTES
|
.NOTES
|
||||||
Author: Markus Fleschutz | License: CC0
|
Author: Markus Fleschutz | License: CC0
|
||||||
#>
|
#>
|
||||||
|
|
||||||
param([string]$hostname = "windows.com")
|
param([string]$hostname = "x.com")
|
||||||
|
|
||||||
function GetPingLatency([string]$hostname) {
|
function GetPingLatency([string]$hostname) {
|
||||||
$hostsArray = $hostname.Split(",")
|
$hostsArray = $hostname.Split(",")
|
||||||
$tasks = $hostsArray | foreach { (New-Object Net.NetworkInformation.Ping).SendPingAsync($_,1500) }
|
$tasks = $hostsArray | foreach { (New-Object Net.NetworkInformation.Ping).SendPingAsync($_,3000) }
|
||||||
[Threading.Tasks.Task]::WaitAll($tasks)
|
[Threading.Tasks.Task]::WaitAll($tasks)
|
||||||
foreach($ping in $tasks.Result) { if ($ping.Status -eq "Success") { return $ping.RoundtripTime } }
|
foreach($ping in $tasks.Result) { if ($ping.Status -eq "Success") { return $ping.RoundtripTime } }
|
||||||
return 1500
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
[int]$latency = GetPingLatency($hostname)
|
[int]$latency = GetPingLatency($hostname)
|
||||||
if ($latency -eq 1500) {
|
if ($latency -lt 0) {
|
||||||
Write-Host "⚠️ Host '$hostname' doesn't respond - check the connection or maybe the host is down."
|
Write-Host "⚠️ Host '$hostname' doesn't respond - check the connection or maybe the host is down."
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
Write-Host "✅ $hostname is up and running ($($latency)ms latency)."
|
Write-Host "✅ Host '$hostname' is up with $($latency)ms ping latency."
|
||||||
exit 0 # success
|
exit 0 # success
|
||||||
} catch {
|
} catch {
|
||||||
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||||
|
Loading…
Reference in New Issue
Block a user