Powershell Script - Port Hunter

Paulo Bazzo

Port Hunter

This is a simple poweshell script I have wrote to help me identify hosts that are live within the network. You have to create a file to feed into the script on the location

You will place one IP per line

The script will go trought every IP on the file, and use the Test-NetConnection cmd-let on powershell to the port you specify.

This command is useful and better than the test-connection or ping because it will send a TCP connection instead of a ICMP, which is great if you are blocking ICMP on your environment, this script will still work.

$targetIp = Get-Content "c:/temp/ips.txt"
$outArray = @()
$outfile = "C:\temp\outFile.txt"
$port = Read-Host "Select the port number you would like to check if its open? " 
forEach ($record in $targetIp){
      $connect = Test-NetConnection $record -Port $port
      if($connect.TcpTestSucceeded -like "True" ){Write-Host "[OPEN] $record Port" -ForegroundColor green;$value = "[OPEN] IP: $record on Port $port"} 
      else{Write-Host "I cant ping $record" -ForegroundColor Red;$value = "[CLOSED] IP: $record Port $port"}
      $outArray += $value
$outArray > $outFile
Write-Host "File exported to $outFile" -ForegroundColor Yellow
Start-Sleep -Seconds 1
#read file that we are able to ping on console that was just exported
$log = Get-Content "C:\temp\outFile.txt"
  forEach($x in $log){
      if ($x -like "*OPEN*"){
          $x = $x.replace("[OPEN] IP: ","").replace(" on Port $port","")
          Write-Host $x -ForegroundColor Green
repsitory link