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
|
2024-01-03 11:44:36 +01:00
|
|
|
|
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
|
2024-01-03 11:44:36 +01:00
|
|
|
|
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-01-31 10:57:28 +01:00
|
|
|
|
$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
|
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..."
|
2023-11-08 13:44:17 +01:00
|
|
|
|
$queue = [System.Collections.Queue]::new()
|
|
|
|
|
foreach($hostname in $hostsArray) {
|
|
|
|
|
$ping = [System.Net.Networkinformation.Ping]::new()
|
2024-01-31 10:53:05 +01:00
|
|
|
|
$obj = @{ Host = $hostname; Ping = $ping; Async = $ping.SendPingAsync($hostname, $pingTimeout) }
|
2024-01-03 11:59:38 +01:00
|
|
|
|
$queue.Enqueue($obj)
|
2023-11-08 13:44:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[string]$result = ""
|
|
|
|
|
while ($queue.Count -gt 0) {
|
2024-01-03 11:59:38 +01:00
|
|
|
|
$obj = $queue.Dequeue()
|
2023-11-08 13:44:17 +01:00
|
|
|
|
try {
|
2024-01-31 10:53:05 +01:00
|
|
|
|
if ($obj.Async.Wait($pingTimeout) -eq $true) {
|
2024-01-03 11:59:38 +01:00
|
|
|
|
if ($obj.Async.Result.Status -ne "TimedOut") {
|
|
|
|
|
$result += "$($obj.Host) "
|
2023-11-08 13:44:17 +01:00
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
2024-01-03 11:59:38 +01:00
|
|
|
|
if ($obj.Async.IsCompleted -eq $true) { continue }
|
2023-11-08 13:44:17 +01:00
|
|
|
|
}
|
2024-01-03 11:59:38 +01:00
|
|
|
|
$queue.Enqueue($obj)
|
2023-11-08 13:44:17 +01:00
|
|
|
|
}
|
|
|
|
|
Write-Progress -completed "Done."
|
2024-01-19 16:02:42 +01:00
|
|
|
|
Write-Host "✅ Up: $($result)"
|
2023-11-08 13:44:17 +01:00
|
|
|
|
exit 0 # success
|
|
|
|
|
} catch {
|
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
|
exit 1
|
2024-01-19 16:02:42 +01:00
|
|
|
|
}
|