Skip to content

Commit 36aa290

Browse files
author
Markus Fleschutz
committed
Updated ping-local-hosts.ps1
1 parent 3ff1d87 commit 36aa290

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

scripts/ping-local-hosts.ps1

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,36 @@
1212
Author: Markus Fleschutz | License: CC0
1313
#>
1414

15-
$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
15+
$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
1616
[int]$pingTimeout = 600 # ms
1717

1818
try {
1919
Write-Progress "Sending pings to the local hosts..."
20+
2021
$queue = [System.Collections.Queue]::new()
21-
foreach($hostname in $hostsArray) {
22+
foreach($name in $names) {
2223
$ping = [System.Net.Networkinformation.Ping]::new()
23-
$obj = @{ Host = $hostname; Ping = $ping; Async = $ping.SendPingAsync($hostname, $pingTimeout) }
24-
$queue.Enqueue($obj)
24+
$queue.Enqueue( @{Host=$name; Ping=$ping; Async=$ping.SendPingAsync($name, $pingTimeout)} )
2525
}
2626

27-
[string]$result = ""
28-
while ($queue.Count -gt 0) {
27+
$up = ""
28+
do {
2929
$obj = $queue.Dequeue()
3030
try {
31-
if ($obj.Async.Wait($pingTimeout) -eq $true) {
31+
if ($obj.Async.Wait($pingTimeout)) {
3232
if ($obj.Async.Result.Status -ne "TimedOut") {
33-
$result += "$($obj.Host) "
33+
$up += "$($obj.Host) "
3434
}
3535
continue
3636
}
3737
} catch {
38-
if ($obj.Async.IsCompleted -eq $true) { continue }
38+
if ($obj.Async.IsCompleted) { continue }
3939
}
4040
$queue.Enqueue($obj)
41-
}
41+
} while ($queue.Count -gt 0)
42+
4243
Write-Progress -completed "Done."
43-
Write-Host "✅ Up: $($result)"
44+
Write-Host "✅ Up: $($up)"
4445
exit 0 # success
4546
} catch {
4647
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"

0 commit comments

Comments
 (0)