|
5 | 5 | This PowerShell script pings the computers in the local network and lists which one are up.
|
6 | 6 | .EXAMPLE
|
7 | 7 | PS> ./ping-local-hosts.ps1
|
8 |
| - ✅ Up: Hippo Jenkins01 Jenkins02 Rocket Vega |
| 8 | + ✅ Up: hippo jenkins01 jenkins02 rocket vega |
9 | 9 | .LINK
|
10 | 10 | https://github.com/fleschutz/PowerShell
|
11 | 11 | .NOTES
|
12 | 12 | Author: Markus Fleschutz | License: CC0
|
13 | 13 | #>
|
14 | 14 |
|
| 15 | +$hostsArray = @('amnesiac','archlinux','berlin','boston','brother','canon','castor','cisco','echodot','epson','fedora','fireball','firewall','fritz.box','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 |
| 16 | +[int]$pingTimeout = 600 # ms |
| 17 | + |
15 | 18 | try {
|
16 | 19 | Write-Progress "Sending pings to the local hosts..."
|
17 |
| - [string]$hosts = "Amnesiac,ArchLinux,Berlin,Boston,Brother,Canon,Castor,Cisco,EchoDot,Epson,Fedora,Fireball,Firewall,fritz.box,GasSensor,Gateway,Hippo,HomeManager,Io,iPhone,Jarvis,Jenkins01,Jenkins02,LA,Laptop,Jupiter,Mars,Mercury,Miami,Mobile,NY,OctoPi,Paris,Pixel-6a,Pluto,Printer,Proxy,R2D2,Raspberry,Rocket,Rome,Router,Server,Shelly1,SmartPhone,SmartWatch,Soundbar,Sunnyboy,Surface,Switch,Tablet,Tolino,TV,Ubuntu,Vega,Venus,XRX,Zeus" # sorted alphabetically |
18 |
| - $hostsArray = $hosts.Split(",") |
19 |
| - $count = $hostsArray.Count |
20 |
| - |
21 |
| - [int]$timeout = 600 # ms |
22 | 20 | $queue = [System.Collections.Queue]::new()
|
23 | 21 | foreach($hostname in $hostsArray) {
|
24 | 22 | $ping = [System.Net.Networkinformation.Ping]::new()
|
25 |
| - $obj = @{ Host = $hostname; Ping = $ping; Async = $ping.SendPingAsync($hostname, $timeout) } |
| 23 | + $obj = @{ Host = $hostname; Ping = $ping; Async = $ping.SendPingAsync($hostname, $pingTimeout) } |
26 | 24 | $queue.Enqueue($obj)
|
27 | 25 | }
|
28 | 26 |
|
29 | 27 | [string]$result = ""
|
30 | 28 | while ($queue.Count -gt 0) {
|
31 | 29 | $obj = $queue.Dequeue()
|
32 | 30 | try {
|
33 |
| - if ($obj.Async.Wait($timeout) -eq $true) { |
| 31 | + if ($obj.Async.Wait($pingTimeout) -eq $true) { |
34 | 32 | if ($obj.Async.Result.Status -ne "TimedOut") {
|
35 | 33 | $result += "$($obj.Host) "
|
36 | 34 | }
|
|
0 commit comments