2024-10-01 15:11:03 +02:00
|
|
|
|
<#
|
2024-03-23 11:57:48 +01:00
|
|
|
|
.SYNOPSIS
|
2024-11-04 15:08:49 +01:00
|
|
|
|
Pings a host
|
2024-03-23 11:57:48 +01:00
|
|
|
|
.DESCRIPTION
|
2024-11-04 15:08:49 +01:00
|
|
|
|
This PowerShell script pings the given host.
|
2024-03-23 11:57:48 +01:00
|
|
|
|
.PARAMETER hostname
|
2024-11-04 15:08:49 +01:00
|
|
|
|
Specifies the hostname or IP address to ping (windows.com by default)
|
2024-03-23 11:57:48 +01:00
|
|
|
|
.EXAMPLE
|
2024-11-04 15:08:49 +01:00
|
|
|
|
PS> ./ping-host.ps1 x.com
|
2024-11-04 15:25:02 +01:00
|
|
|
|
✅ x.com is up and running (11ms latency).
|
2024-03-23 11:57:48 +01:00
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
.NOTES
|
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
|
#>
|
|
|
|
|
|
2024-11-04 15:08:49 +01:00
|
|
|
|
param([string]$hostname = "windows.com")
|
2024-03-23 11:57:48 +01:00
|
|
|
|
|
|
|
|
|
function GetPingLatency([string]$hostname) {
|
|
|
|
|
$hostsArray = $hostname.Split(",")
|
2024-11-04 15:25:02 +01:00
|
|
|
|
$tasks = $hostsArray | foreach { (New-Object Net.NetworkInformation.Ping).SendPingAsync($_,1500) }
|
2024-03-24 12:05:31 +01:00
|
|
|
|
[Threading.Tasks.Task]::WaitAll($tasks)
|
2024-11-04 15:25:02 +01:00
|
|
|
|
foreach($ping in $tasks.Result) { if ($ping.Status -eq "Success") { return $ping.RoundtripTime } }
|
2024-11-04 15:08:49 +01:00
|
|
|
|
return 1500
|
2024-03-23 11:57:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2024-11-04 15:08:49 +01:00
|
|
|
|
[int]$latency = GetPingLatency($hostname)
|
|
|
|
|
if ($latency -eq 1500) {
|
2024-11-04 20:34:49 +01:00
|
|
|
|
Write-Host "⚠️ Host '$hostname' doesn't respond - check the connection or maybe the host is down."
|
2024-11-04 15:25:02 +01:00
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
Write-Host "✅ $hostname is up and running ($($latency)ms latency)."
|
2024-03-23 11:57:48 +01:00
|
|
|
|
exit 0 # success
|
|
|
|
|
} catch {
|
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
|
exit 1
|
|
|
|
|
}
|