Hey there checkyourlogs.net fans!
Today I needed to ping sweep an entire subnet from a Windows Server 2012 box. Simple, right? There are plenty of free and paid tools out there ready to perform this task. My dilemma was, and is, I don’t want to install additional software, no matter how small and tight, on servers. Especially anything requiring additional licensing or support software such as, GASP, JAVA. Nope, nada, not on my watch. Keep server configurations clean and concise is my motto.
What to do? My need still existed, and oh, by the way was fairly urgent. PowerShell to the rescue!
The original idea for this code comes from a blog post offered a few years ago by my friend, PowerShell MVP Jeff Hicks. I tweaked the code for my current needs, adding variables as required and sending the results to a .csv file easily imported int Excel where sorting and filtering is easy. I added comments explaining the purpose of each variable. No need to rehash those. Here’s the code in action.
Those looking closely, probably notice one screenshot uses VSCode, while the other leverages the good ‘ole ISE. The server I ran this script on is 2012 R2. The ISE was already installed. The ISE proved once again that old doesn’t mean useless. The ISE ran this ping sweep code without missing a beat.
Don’t worry, I won’t make you retype the code from a screenshot. Here it is in easy to cut and paste form, ready for work.
# Subnet identifier for ping sweep $subnet = "10.113.4" # Host IP to start ping sweep $start = 1 # Host IP to finish ping sweep $end = 254 # Number of times to attempt each ping $ping = 1 # Path and filename for results file $OutPath ="C:\Temp\Output.csv" While ($start -le $end) { $IP = "$subnet.$start" $Test = Test-Connection -ComputerName $IP -count $ping -Quiet Write-Host "$IP,$Test" Add-Content -LiteralPath $OutPath -Value "$IP,$Test" $start++ }
The code in this post, by design, is quite unrefined. My goal is showing how a little PowerShell goes a long way in helping us IT Pros get our jobs done, day in and day out. In coming blog posts, I’ll refine this code into a function, add parameters, and even add it into my custom “ITPro Toolkit” PowerShell Module. These mods, among other things, will allow us to look for specific results, such as just hosts responding to our pings.
Thanks everyone! I hope this code helps in your day to day IT troubleshooting challenges!
John Sr.
If you really want a fast ping, better use WMI Win32_PingStatus class or .Net Net.NetworkInformation.Ping
You will be amazed by difference in speed.
You could even optimize more by use of threading like below
$ips = 1..255 | % {“192.168.0.$_”}
$t=$ips|%{(New-Object Net.NetworkInformation.Ping).SendPingAsync($_,250)}
[Threading.Tasks.Task]::WaitAll($t)
$t.Result |Select Address,Status,RoundTripTime | % {if($_.Status -eq ‘Success’) {$_}}
Thanks for the comment! I agree WMI provides much faster ping, as does using direct .Net functionality. My goal with this post was creating something quick and easy for unexperienced PowerShell users to understand and put to work. My criteria included relying on cmdlets included with PS 5.1.