Super-Fast Network Scans (Part 2)

by Mar 24, 2023

In our previous tip we looked at Test-NetConnection and found that it was way too slow, so we replaced it by a much faster Test-RemotePort function.

Today we’ll do the same for simple ICMP requests so you also have a fast alternative for pinging:

function Test-Ping
{
  param
  (
    [Parameter(Mandatory,ValueFromPipeline)]
    [string]
    $ComputerName,
        
    [int]
    $TimeoutMillisec = 1000
  )
    
  begin
  {
    $pinger = [Net.NetworkInformation.Ping]::new() 
  }
  process
  {
    $ComputerName | 
    ForEach-Object {
      $ip = $_
      $pinger.Send($_, $TimeoutMillisec) |
      Select-Object -Property Status, Address, ComputerName |
      ForEach-Object {
        # add the property "computername" which stores the user input
        $_.ComputerName = $ip
        $_
        }
    }
  }
  end
  {
    $pinger.Dispose()
  }
}

Here is a use case: PowerShell scans a network segment and returns ping information (make sure you adjust the IP range to your network requirements):

1..255 | 
    ForEach-Object {
    "192.168.2.$_"
    } |
    ForEach-Object {
        Test-Ping -ComputerName $_ -TimeoutMilliSec 500        
    } |
    Select-Object -Property Status, Address, ComputerName

Tweet this Tip! Tweet this Tip!