Streamlining Port Connectivity Checks in PowerShell

In the ever-evolving landscape of network administration, the ability to quickly and efficiently verify open ports across multiple hosts and destinations is paramount. This task, though crucial, can often be cumbersome and time-consuming. Recently, I found myself in need of a more streamlined method to perform these checks – a quest that led me to PowerShellBros.com. Their insights into PowerShell scripting laid the groundwork for what would become a more advanced iteration of a port connectivity checker.

Building upon the foundation laid by PowerShellBros.com, I ventured into enhancing this script to cater to more intricate network setups. The goal was simple yet ambitious: create a PowerShell script capable of testing port connectivity from several servers to all writable Domain Controllers within a domain. This adaptation not only saves time but also adds a layer of comprehensive checks that are crucial in large-scale network environments.


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

# Retrieve all writable Domain Controllers
$Destinations = Get-ADDomainController -Filter {IsReadOnly -eq $false} | Select-Object -ExpandProperty HostName

$Results = @()

$ScriptBlock = {
    param($Destinations, $Ports)
    foreach ($Destination in $Destinations) {
        $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).TcpTestSucceeded
            If ($PortCheck -notmatch "True|False") { $PortCheck = "ERROR" }
            $Object | Add-Member Noteproperty "$("Port " + "$p")" -Value "$($PortCheck)"
        }
        $Object
    }
}

foreach ($Server in $Servers) {
    $Results += Invoke-Command -ComputerName $Server -ScriptBlock $ScriptBlock -ArgumentList $Destinations, $Ports
}

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

$Results | Format-Table -AutoSize

The modified script leverages the power of PowerShell to dynamically retrieve a list of writable Domain Controllers and then iterates through these, along with a predefined list of critical ports, to check for connectivity. This automation not only streamlines the process but also ensures a thorough and systematic approach to port checking.

I extend my thanks to PowerShellBros.com for their initial coverage of this topic. Their work provided a solid starting point from which this iteration was developed. It’s my hope that this enhanced script will be a valuable tool for network administrators and IT professionals, helping them to more effectively manage and troubleshoot their network environments.

Here is another version for use with any destination servers not just DCs.


$Servers = "ADSO01", "ADFS02"
$Ports = "135", "389", "636", "3268", "3269", "53", "88", "445"
$Destinations = "DC01", "DC02"  # Add your destination servers here

$Results = @()

$ScriptBlock = {
    param($Destinations, $Ports)
    foreach ($Destination in $Destinations) {
        $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).TcpTestSucceeded
            If ($PortCheck -notmatch "True|False") { $PortCheck = "ERROR" }
            $Object | Add-Member Noteproperty "$("Port " + "$p")" -Value "$($PortCheck)"
        }
        $Object
    }
}

foreach ($Server in $Servers) {
    $Results += Invoke-Command -ComputerName $Server -ScriptBlock $ScriptBlock -ArgumentList $Destinations, $Ports
}

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

$Results | Format-Table -AutoSize

You May Also Like

About the Author: Phil

Leave a Reply

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