mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-08-21 01:14:26 +02:00
Updated the manuals
This commit is contained in:
@@ -6,14 +6,14 @@ This PowerShell script pings the given host.
|
||||
Parameters
|
||||
----------
|
||||
```powershell
|
||||
/home/markus/Repos/PowerShell/scripts/ping-host.ps1 [[-hostname] <String>] [<CommonParameters>]
|
||||
/Repos/PowerShell/scripts/ping-host.ps1 [[-hostname] <String>] [<CommonParameters>]
|
||||
|
||||
-hostname <String>
|
||||
Specifies the hostname or IP address to ping (windows.com by default)
|
||||
Specifies the hostname or IP address to ping (x.com by default)
|
||||
|
||||
Required? false
|
||||
Position? 1
|
||||
Default value windows.com
|
||||
Default value x.com
|
||||
Accept pipeline input? false
|
||||
Accept wildcard characters? false
|
||||
|
||||
@@ -26,7 +26,7 @@ Example
|
||||
-------
|
||||
```powershell
|
||||
PS> ./ping-host.ps1 x.com
|
||||
✅ x.com is up and running (11ms latency).
|
||||
✅ Host 'x.com' with 20ms latency at IP 104.244.42.1 is up 👍
|
||||
|
||||
```
|
||||
|
||||
@@ -47,38 +47,37 @@ Script Content
|
||||
.DESCRIPTION
|
||||
This PowerShell script pings the given host.
|
||||
.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
|
||||
PS> ./ping-host.ps1 x.com
|
||||
✅ x.com is up and running (11ms latency).
|
||||
✅ Host 'x.com' with 20ms latency at IP 104.244.42.1 is up 👍
|
||||
.LINK
|
||||
https://github.com/fleschutz/PowerShell
|
||||
.NOTES
|
||||
Author: Markus Fleschutz | License: CC0
|
||||
#>
|
||||
|
||||
param([string]$hostname = "windows.com")
|
||||
|
||||
function GetPingLatency([string]$hostname) {
|
||||
$hostsArray = $hostname.Split(",")
|
||||
$tasks = $hostsArray | foreach { (New-Object Net.NetworkInformation.Ping).SendPingAsync($_,1500) }
|
||||
[Threading.Tasks.Task]::WaitAll($tasks)
|
||||
foreach($ping in $tasks.Result) { if ($ping.Status -eq "Success") { return $ping.RoundtripTime } }
|
||||
return 1500
|
||||
}
|
||||
param([string]$hostname = "x.com")
|
||||
|
||||
try {
|
||||
[int]$latency = GetPingLatency($hostname)
|
||||
if ($latency -eq 1500) {
|
||||
Write-Host "⚠️ Host '$hostname' doesn't respond - check the connection or maybe the host is down."
|
||||
exit 1
|
||||
}
|
||||
Write-Host "✅ $hostname is up and running ($($latency)ms latency)."
|
||||
exit 0 # success
|
||||
$remoteHosts = $hostname.Split(",")
|
||||
$tasks = $remoteHosts | foreach { (New-Object Net.NetworkInformation.Ping).SendPingAsync($_,5000) }
|
||||
[Threading.Tasks.Task]::WaitAll($tasks)
|
||||
foreach($ping in $tasks.Result) {
|
||||
if ($ping.Status -eq "Success") {
|
||||
Write-Output "✅ Host '$hostname' with $($ping.RoundtripTime)ms latency at IP $($ping.Address) is up 👍"
|
||||
exit 0 # success
|
||||
} else {
|
||||
Write-Output "⚠️ No reply from '$hostname' (IP $($ping.Address)) - check the connection or maybe the host is down."
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
Write-Output "⚠️ No reply from host '$hostname' - check the connection or maybe the host is down."
|
||||
exit 1
|
||||
} catch {
|
||||
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||
exit 1
|
||||
}
|
||||
```
|
||||
|
||||
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:59)*
|
||||
*(page generated by convert-ps2md.ps1 as of 01/17/2025 08:30:56)*
|
||||
|
Reference in New Issue
Block a user