PowerShell/scripts/ping-local-hosts.ps1

50 lines
1.9 KiB
PowerShell
Raw Normal View History

2023-11-08 13:44:17 +01:00
<#
.SYNOPSIS
2023-11-09 07:25:36 +01:00
Pings local hosts
2023-11-08 13:44:17 +01:00
.DESCRIPTION
This PowerShell script pings the computers in the local network and lists which one are up.
2023-11-08 13:44:17 +01:00
.EXAMPLE
PS> ./ping-local-hosts.ps1
2024-01-31 10:53:05 +01:00
Up: hippo jenkins01 jenkins02 rocket vega
2023-11-08 13:44:17 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-02-01 20:12:31 +01:00
$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','none','none-1','none-2','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
2024-01-31 10:53:05 +01:00
[int]$pingTimeout = 600 # ms
2023-11-08 13:44:17 +01:00
try {
2024-01-03 11:59:38 +01:00
Write-Progress "Sending pings to the local hosts..."
2024-01-31 11:36:09 +01:00
2023-11-08 13:44:17 +01:00
$queue = [System.Collections.Queue]::new()
2024-01-31 11:36:09 +01:00
foreach($name in $names) {
2023-11-08 13:44:17 +01:00
$ping = [System.Net.Networkinformation.Ping]::new()
2024-01-31 11:36:09 +01:00
$queue.Enqueue( @{Host=$name; Ping=$ping; Async=$ping.SendPingAsync($name, $pingTimeout)} )
2023-11-08 13:44:17 +01:00
}
2024-01-31 11:36:09 +01:00
$up = ""
do {
2024-01-03 11:59:38 +01:00
$obj = $queue.Dequeue()
2023-11-08 13:44:17 +01:00
try {
2024-01-31 11:36:09 +01:00
if ($obj.Async.Wait($pingTimeout)) {
2024-01-03 11:59:38 +01:00
if ($obj.Async.Result.Status -ne "TimedOut") {
2024-01-31 11:36:09 +01:00
$up += "$($obj.Host) "
2023-11-08 13:44:17 +01:00
}
continue
}
} catch {
2024-01-31 11:36:09 +01:00
if ($obj.Async.IsCompleted) { continue }
2023-11-08 13:44:17 +01:00
}
2024-01-03 11:59:38 +01:00
$queue.Enqueue($obj)
2024-01-31 11:36:09 +01:00
} while ($queue.Count -gt 0)
Write-Progress -completed "done."
2024-01-31 11:36:09 +01:00
Write-Host "✅ Up: $($up)"
2023-11-08 13:44:17 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}