PowerShell/docs/ping-local-hosts.md

86 lines
2.8 KiB
Markdown
Raw Permalink 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
2024-05-19 10:25:56 +02:00
PS> ./ping-local-hosts.ps1 [[-timeout] <Int32>] [<CommonParameters>]
-timeout <Int32>
Required? false
Position? 1
Default value 600
Accept pipeline input? false
Accept wildcard characters? false
2024-01-03 12:11:22 +01:00
[<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-05-19 10:25:56 +02:00
param([int]$timeout = 600) # ms ping timeout
2024-03-27 17:36:59 +01:00
2024-01-03 12:11:22 +01:00
try {
2024-08-15 09:51:46 +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
2024-01-03 12:11:22 +01:00
$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-05-19 10:25:56 +02:00
$queue.Enqueue( @{Host=$name; Ping=$ping; Async=$ping.SendPingAsync($name, $timeout)} )
2024-01-03 12:11:22 +01:00
}
2024-05-19 10:25:56 +02:00
[string]$up = ""
2024-08-15 09:51:46 +02:00
Write-Host "✅ Up: " -noNewline
2024-05-19 10:25:56 +02:00
while ($queue.Count -gt 0) { $obj = $queue.Dequeue()
try { if ($obj.Async.Wait($timeout)) {
2024-08-15 09:51:46 +02:00
if ($obj.Async.Result.Status -ne "TimedOut") { Write-Host "$($obj.Host) " -noNewline }
2024-01-03 12:11:22 +01:00
continue
}
2024-05-19 10:25:56 +02:00
} catch { continue }
2024-01-03 12:11:22 +01:00
$queue.Enqueue($obj)
2024-05-19 10:25:56 +02:00
}
2024-01-03 12:11:22 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
```
2024-08-15 09:51:46 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of ping-local-hosts.ps1 as of 08/15/2024 09:50:52)*