Powershell - Scan An IP Scope For Active Devices

List this:
$scopes=("192.168.1.","192.168.2.","192.168.3.")

Run this:
Function EvalTargetIP{
 [CmdletBinding()]
 param($target,$syscount)
 $target | % {
  $targethostname=$null
  $targethostname=([System.Net.Dns]::GetHostbyAddress("$target")).hostname
  Trap {Continue}
  if ($targethostname -eq $null) {$targethostname="unknown"}
  if (Test-Connection $target -BufferSize 16 -Quiet -Count 1 -ErrorAction SilentlyContinue)
   { $targetstate="UP"
    write-host "$targethostname|$target|$targetstate" -ForegroundColor GREEN }
  else
   { $targetstate="DOWN"
    write-host "$targethostname|$target|$targetstate" -ForegroundColor RED }
  Return New-Object Psobject -property @{IP=$target;SystemName=$targethostname;SystemState=$targetstate;SystemCount=$syscount}
 }
}

$ElapsedTime = [System.Diagnostics.Stopwatch]::StartNew()
$startime = $(get-date)
$allresult = @()
$syscount = 1
foreach ($scope in $scopes)
 { 1..255 | % { $allresult += EvalTargetIP ($scope + $_) ($syscount++) }
 }
$allresult | select-object -property SystemName,IP,SystemState,SystemCount | sort-object -property SystemCount | Out-GridView -Title "AmIUp?"
write-host "Script Started at $startime"
write-host "Script Ended at $(get-date)"
write-host "Total Elapsed Time: $($ElapsedTime.Elapsed.ToString())"

View this:


No comments:

Post a Comment