Home |

Trigat

PowerShell Test-NetConnection on Multiple Servers/Ports

05-24-2019

Use PowerShell Test-NetConnection to scan list of servers for list of open ports.

Language or Platform: Powershell

Code:

<#

.SYNOPSIS
Scan list of servers for list of open ports.

.PARAMETER
Define list of servers and ports in specified .txt files
One name or port on each line of the .txt file.

#>

$Servers = gc "$PSScriptRoot\servers.txt"
$Port    = gc "$PSScriptRoot\ports.txt"
$Log     = "$PSScriptRoot\log.csv"
 
$Result = foreach ($S in $Servers) {
    foreach ($P in $Port) {
        Test-NetConnection -ComputerName $S -Port $P -WarningAction SilentlyContinue
    }
}

$Result | Select-Object -Property ComputerName, TcpTestSucceeded, RemotePort, NameResolutionResults | Export-Csv -NoTypeInformation -Path $Log

Back