-
-
Save joegasper/93ff8ae44fa8712747d85aa92c2b4c78 to your computer and use it in GitHub Desktop.
# Inspiration from https://twitter.com/mrhvid/status/929717169130176512 @mrhvid @Lee_Holmes | |
function ResolveIp($IpAddress) { | |
try { | |
(Resolve-DnsName $IpAddress -QuickTimeout -ErrorAction SilentlyContinue).NameHost | |
} catch { | |
$null | |
} | |
} | |
function Get-PingSweep { | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$true)] | |
[string]$SubNet, | |
[switch]$ResolveName | |
) | |
$ips = 1..254 | ForEach-Object {"$($SubNet).$_"} | |
$ps = foreach ($ip in $ips) { | |
(New-Object Net.NetworkInformation.Ping).SendPingAsync($ip, 250) | |
#[Net.NetworkInformation.Ping]::New().SendPingAsync($ip, 250) # or if on PowerShell v5 | |
} | |
[Threading.Tasks.Task]::WaitAll($ps) | |
$ps.Result | Where-Object -FilterScript {$_.Status -eq 'Success' -and $_.Address -like "$subnet*"} | | |
Select-Object Address,Status,RoundtripTime -Unique | | |
ForEach-Object { | |
if ($_.Status -eq 'Success') { | |
if (!$ResolveName) { | |
$_ | |
} else { | |
$_ | Select-Object Address, @{Expression={ResolveIp($_.Address)};Label='Name'}, Status, RoundtripTime | |
} | |
} | |
} | |
} | |
<# | |
Get-PingSweep -SubNet '10.56.161' | |
Address Status RoundtripTime | |
------- ------ ------------- | |
10.56.161.1 Success 2 | |
10.56.161.48 Success 0 | |
10.56.161.49 Success 0 | |
10.56.161.51 Success 0 | |
10.56.161.52 Success 0 | |
10.56.161.53 Success 0 | |
10.56.161.54 Success 1 | |
Get-PingSweep -SubNet '10.56.161' -ResolveName | |
Address Name Status RoundtripTime | |
------- ---- ------ ------------- | |
10.56.161.1 b86-bop12-sw18.contoso.com Success 3 | |
10.56.161.48 it-box1-dhwin7.contoso.com Success 1 | |
10.56.161.49 it-box2-dhwin10.contoso.com Success 0 | |
10.56.161.51 it-dept1-uh7yg2.contoso.com Success 0 | |
10.56.161.52 ds101sync.contoso.com Success 0 | |
10.56.161.53 Success 1 | |
10.56.161.54 it-ithd-12tr889.contoso.com Success 1 | |
(Measure-Command -Expression {Get-PingSweep -SubNet '10.56.161' -ResolveName}).Milliseconds | |
441 | |
#> |
@joegasper Thanks for the speedy response! I may be doing it wrong however I am getting this message.
Exception calling "WaitAll" with "1" argument(s): "One or more errors occurred."
At line:21 char:5
-
[Threading.Tasks.Task]::WaitAll($ps)
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : NotSpecified: (:) [], MethodInvocationException
- FullyQualifiedErrorId : AggregateException
I get that error if I try something like:
Get-PingSweep -SubNet 192.168.1.56
The correct command would be:
Get-PingSweep -SubNet 192.168.1
Just give the command the first 3 numbers (octets).
That example will ping from 192.168.1.1 to 192.169.1.254 and list any responding IP addresses in that range.
Hi, the script is owesome thanks, but i have some problems.
Every time when i make a scan, different IP addresses are shown, for example:
Each time I am getting different result (list of active devices) somethink like:
Scan 1:
192.168.20.1
192.168.20.6
192.168.20.164
Scan 2:
192.168.20.1
192.168.20.6
192.168.20.31
192.168.20.164
Scan 3:
192.168.20.1
192.168.20.6
192.168.20.107
192.168.20.99
but i know that one device has 192.168.20.100 Address, and then when i manually make a ping 192.168.20.100
and make pingsweep again, then is Address in the list.
Scan 4:
192.168.20.1
192.168.20.6
192.168.20.107
192.168.20.100
My problem is actually, I need to know which devices are online, it seems that pingsweep is super fast but therfore unreliable.
Do you have any advice?
Thanks in advance!
@aussiejack586 the code will just scan a /24 (255.255.255.0) subnet - you just enter the first 3 octets of the subnet as in the 2 examples.
So Get-PingSweep -SubNet '10.56.161' will scan IP addresses from 10.56.161.1 to 10.56.161.254.