Test multiple ports on remote servers using PowerShell script

Some time ago I added articles about testing connection on several ports. In one of them I was explaining how to format PortQry results and other was about PowerShell function. This time I wanted to speed up things a little bit 🙂

Testing connection

We can test connection in several ways. In this case I wanted to make a test on lots of remote machines in fast way. In this example I would like to show you how test multiple ports inside one scriptblock.

For purpose of this script I will test below ports – Domains and Trusts:

  • RPC “135”
  • LDAP “389”
  • LDAP “636”
  • LDAP “3268”
  • LDAP “3269”
  • DNS “53”
  • Kerberos “88”
  • SMB “445”

Below you can find script which will use Test-NetConnection and add results to an array. This is nothing new apart from that it will check it faster than in previous examples:

$Servers = "ADSO01","ADFS02"
$Ports   =  "135",
            "389",
            "636",
            "3268",
            "3269",
            "53",
            "88",
            "445"

$Destination = "DC01"
$Results = @()
$Results = Invoke-Command $Servers {param($Destination,$Ports)
                $Object = New-Object PSCustomObject
                $Object | Add-Member -MemberType NoteProperty -Name "ServerName" -Value $env:COMPUTERNAME
                $Object | Add-Member -MemberType NoteProperty -Name "Destination" -Value $Destination
                    Foreach ($P in $Ports){
                        $PortCheck = (Test-NetConnection -Port $p -ComputerName $Destination ).PingSucceeded
                        If($PortCheck -notmatch "True|False"){$PortCheck = "ERROR"}
                        $Object | Add-Member Noteproperty "$("Port " + "$p")" -Value "$($PortCheck)"
                    }
                $Object
           } -ArgumentList $Destination,$Ports | select * -ExcludeProperty runspaceid, pscomputername

$Results | Out-GridView -Title "Testing Ports"

$Results | Format-Table -AutoSize

Final results:

Multiple ports - results

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.