PowerShell/scripts/ping-local-hosts.ps1

41 lines
1.8 KiB
PowerShell
Raw Normal View History

2024-10-01 13:37:53 +02:00
<#
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-05-17 19:31:10 +02:00
param([int]$timeout = 600) # ms ping timeout
2024-01-31 10:53:05 +01:00
2023-11-08 13:44:17 +01:00
try {
2024-05-19 18:39:00 +02:00
$names = @('accesspoint','AD','AP','amnesiac','archlinux','auriga','berlin','boston','brother','canon','castor','cisco','echodot','epson','epson2550','epson2815','fedora','fireball','firewall','fritz.box','fritz.repeater','gassensor','gateway','hippo','heizung','hodor','homemanager','io','iphone','jarvis','jenkins','LA','laptop','linux','jupiter','mars','mercury','miami','mobile','none','none-1','none-2','NY','octo','office','officepc','paris','PI','pixel-6a','PC','pluto','printer','proxy','R2D2','raspberry','rocket','rome','router','sentinel','server','shelly','shelly1','smartphone','smartwatch','soundbar','sunnyboy','surface','switch','tablet','tau','tigercat','tolino','TV','ubuntu','vega','venus','xrx','zeus') # sorted alphabetically
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-05-15 16:37:50 +02:00
$queue.Enqueue( @{Host=$name; Ping=$ping; Async=$ping.SendPingAsync($name, $timeout)} )
2023-11-08 13:44:17 +01:00
}
2024-05-17 19:31:10 +02:00
[string]$up = ""
2024-05-27 09:28:40 +02:00
Write-Host "✅ Up: " -noNewline
2024-05-15 16:37:50 +02:00
while ($queue.Count -gt 0) { $obj = $queue.Dequeue()
try { if ($obj.Async.Wait($timeout)) {
2024-05-27 09:28:40 +02:00
if ($obj.Async.Result.Status -ne "TimedOut") { Write-Host "$($obj.Host) " -noNewline }
2023-11-08 13:44:17 +01:00
continue
}
2024-05-15 16:37:50 +02:00
} catch { continue }
2024-01-03 11:59:38 +01:00
$queue.Enqueue($obj)
2024-05-15 16:37:50 +02:00
}
2023-11-08 13:44:17 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}