PowerShell/docs/ping-local-hosts.md

87 lines
2.6 KiB
Markdown
Raw Normal View History

2024-01-25 13:37:12 +01:00
Script: *ping-local-hosts.ps1*
========================
2024-01-03 12:11:22 +01:00
This PowerShell script pings the computers in the local network and lists which one are up.
Parameters
----------
```powershell
PS> ./ping-local-hosts.ps1 [<CommonParameters>]
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
Example
-------
```powershell
PS> ./ping-local-hosts.ps1
2024-03-27 17:36:59 +01:00
✅ Up: hippo jenkins01 jenkins02 rocket vega
2024-01-03 12:11:22 +01:00
```
Notes
-----
Author: Markus Fleschutz | License: CC0
Related Links
-------------
https://github.com/fleschutz/PowerShell
Script Content
--------------
```powershell
<#
.SYNOPSIS
Pings local hosts
.DESCRIPTION
This PowerShell script pings the computers in the local network and lists which one are up.
.EXAMPLE
PS> ./ping-local-hosts.ps1
2024-03-27 17:36:59 +01:00
✅ Up: hippo jenkins01 jenkins02 rocket vega
2024-01-03 12:11:22 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-03-27 17:36:59 +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
[int]$pingTimeout = 600 # ms
2024-01-03 12:11:22 +01:00
try {
Write-Progress "Sending pings to the local hosts..."
$queue = [System.Collections.Queue]::new()
2024-03-27 17:36:59 +01:00
foreach($name in $names) {
2024-01-03 12:11:22 +01:00
$ping = [System.Net.Networkinformation.Ping]::new()
2024-03-27 17:36:59 +01:00
$queue.Enqueue( @{Host=$name; Ping=$ping; Async=$ping.SendPingAsync($name, $pingTimeout)} )
2024-01-03 12:11:22 +01:00
}
2024-03-27 17:36:59 +01:00
$up = ""
do {
2024-01-03 12:11:22 +01:00
$obj = $queue.Dequeue()
try {
2024-03-27 17:36:59 +01:00
if ($obj.Async.Wait($pingTimeout)) {
2024-01-03 12:11:22 +01:00
if ($obj.Async.Result.Status -ne "TimedOut") {
2024-03-27 17:36:59 +01:00
$up += "$($obj.Host) "
2024-01-03 12:11:22 +01:00
}
continue
}
} catch {
2024-03-27 17:36:59 +01:00
if ($obj.Async.IsCompleted) { continue }
2024-01-03 12:11:22 +01:00
}
$queue.Enqueue($obj)
2024-03-27 17:36:59 +01:00
} while ($queue.Count -gt 0)
Write-Progress -completed "done."
Write-Host "✅ Up: $($up)"
2024-01-03 12:11:22 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
```
2024-03-27 17:36:59 +01:00
*(generated by convert-ps2md.ps1 using the comment-based help of ping-local-hosts.ps1 as of 03/27/2024 17:36:30)*