mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-04-13 21:48:23 +02:00
Updated ping-local-hosts.ps1
This commit is contained in:
parent
3ff1d8718c
commit
36aa290e9c
@ -12,35 +12,36 @@
|
|||||||
Author: Markus Fleschutz | License: CC0
|
Author: Markus Fleschutz | License: CC0
|
||||||
#>
|
#>
|
||||||
|
|
||||||
$hostsArray = @('amnesiac','archlinux','berlin','boston','brother','canon','castor','cisco','echodot','epson','fedora','fireball','firewall','fritz.box','fritz!repeater','gassensor','gateway','hippo','heizung','homemanager','io','iphone','jarvis','jenkins','la','laptop','jupiter','mars','mercury','miami','mobile','ny','octopi','office','officepc','paris','pi','pixel-6a','pluto','printer','proxy','r2d2','raspberry','rocket','rome','router','server','shelly1','smartphone','smartwatch','soundbar','sunnyboy','surface','switch','tablet','tau','tolino','tv','ubuntu','vega','venus','xrx','zeus') # sorted alphabetically
|
$names = @('ad','amnesiac','archlinux','auriga','berlin','boston','brother','canon','castor','cisco','echodot','epson','fedora','fireball','firewall','fritz.box','fritz!repeater','gassensor','gateway','hippo','heizung','hodor','homemanager','io','iphone','jarvis','jenkins','la','laptop','jupiter','mars','mercury','miami','mobile','ny','octopi','office','officepc','paris','pi','pixel-6a','pluto','printer','proxy','r2d2','raspberry','rocket','rome','router','sentinel','server','shelly1','smartphone','smartwatch','soundbar','sunnyboy','surface','switch','tablet','tau','tigercat','tolino','tv','ubuntu','vega','venus','xrx','zeus') # sorted alphabetically
|
||||||
[int]$pingTimeout = 600 # ms
|
[int]$pingTimeout = 600 # ms
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Write-Progress "Sending pings to the local hosts..."
|
Write-Progress "Sending pings to the local hosts..."
|
||||||
|
|
||||||
$queue = [System.Collections.Queue]::new()
|
$queue = [System.Collections.Queue]::new()
|
||||||
foreach($hostname in $hostsArray) {
|
foreach($name in $names) {
|
||||||
$ping = [System.Net.Networkinformation.Ping]::new()
|
$ping = [System.Net.Networkinformation.Ping]::new()
|
||||||
$obj = @{ Host = $hostname; Ping = $ping; Async = $ping.SendPingAsync($hostname, $pingTimeout) }
|
$queue.Enqueue( @{Host=$name; Ping=$ping; Async=$ping.SendPingAsync($name, $pingTimeout)} )
|
||||||
$queue.Enqueue($obj)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[string]$result = ""
|
$up = ""
|
||||||
while ($queue.Count -gt 0) {
|
do {
|
||||||
$obj = $queue.Dequeue()
|
$obj = $queue.Dequeue()
|
||||||
try {
|
try {
|
||||||
if ($obj.Async.Wait($pingTimeout) -eq $true) {
|
if ($obj.Async.Wait($pingTimeout)) {
|
||||||
if ($obj.Async.Result.Status -ne "TimedOut") {
|
if ($obj.Async.Result.Status -ne "TimedOut") {
|
||||||
$result += "$($obj.Host) "
|
$up += "$($obj.Host) "
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
if ($obj.Async.IsCompleted -eq $true) { continue }
|
if ($obj.Async.IsCompleted) { continue }
|
||||||
}
|
}
|
||||||
$queue.Enqueue($obj)
|
$queue.Enqueue($obj)
|
||||||
}
|
} while ($queue.Count -gt 0)
|
||||||
|
|
||||||
Write-Progress -completed "Done."
|
Write-Progress -completed "Done."
|
||||||
Write-Host "✅ Up: $($result)"
|
Write-Host "✅ Up: $($up)"
|
||||||
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