mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-04-29 21:24:45 +02:00
Add ping-hosts.ps1
This commit is contained in:
parent
54cc55b103
commit
6daf7a7928
50
scripts/ping-hosts.ps1
Normal file
50
scripts/ping-hosts.ps1
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<#
|
||||||
|
.SYNOPSIS
|
||||||
|
Pings the local hosts
|
||||||
|
.DESCRIPTION
|
||||||
|
This PowerShell script pings the hosts in the local network and lists which one up.
|
||||||
|
.EXAMPLE
|
||||||
|
PS> ./check-hosts.ps1
|
||||||
|
.LINK
|
||||||
|
https://github.com/fleschutz/PowerShell
|
||||||
|
.NOTES
|
||||||
|
Author: Markus Fleschutz | License: CC0
|
||||||
|
#>
|
||||||
|
|
||||||
|
try {
|
||||||
|
[string]$hosts = "Boston,Castor,Cisco,Fireball,Firewall,fritz.box,Gateway,Hippo,Io,Jenkins01,Jenkins02,Jupiter,Mars,Mercury,Miami,NY,Paris,Pluto,Proxy,Rocket,Router,Server,Sunnyboy,Ubuntu,Vega,Venus,Zeus" # sorted alphabetically
|
||||||
|
$hostsArray = $hosts.Split(",")
|
||||||
|
$count = $hostsArray.Count
|
||||||
|
|
||||||
|
Write-Progress "Sending pings to $count local hosts..."
|
||||||
|
$queue = [System.Collections.Queue]::new()
|
||||||
|
foreach($hostname in $hostsArray) {
|
||||||
|
$ping = [System.Net.Networkinformation.Ping]::new()
|
||||||
|
$object = @{ Host = $hostname; Ping = $ping; Async = $ping.SendPingAsync($hostname, 400) }
|
||||||
|
$queue.Enqueue($object)
|
||||||
|
}
|
||||||
|
|
||||||
|
[string]$result = ""
|
||||||
|
while ($queue.Count -gt 0) {
|
||||||
|
$object = $queue.Dequeue()
|
||||||
|
try {
|
||||||
|
if ($object.Async.Wait(400) -eq $true) {
|
||||||
|
if ($object.Async.Result.Status -ne "TimedOut") {
|
||||||
|
$result += "$($object.Host) "
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
if ($object.Async.IsCompleted -eq $true) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$queue.Enqueue($object)
|
||||||
|
}
|
||||||
|
Write-Progress -completed "Done."
|
||||||
|
Write-Host "✅ Up are: $result"
|
||||||
|
exit 0 # success
|
||||||
|
} catch {
|
||||||
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||||
|
exit 1
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user