Avatar of nico-
nico-
 asked on

Powershell - Testing ports on multiple servers

Hello

I'm looking to use Test-NetConnectivity to check for port 53 but instead of checking one server, I'm hoping to use a script that would import a CSV (with the server ip addresses) and then check port 53 for all servers in the file .. and then output clear results to a separate file.  I'm not sure how to avoid the ping that seems to be a part of the cmdlet.

Hope you can help
PowershellActive Directory

Avatar of undefined
Last Comment
nico-

8/22/2022 - Mon
McKnife

If you have a textfile with servers one in each line,
the command would be
gc D:\list.txt |test-NetConnection -port 53

Open in new window

Guy Lidbetter

Here ya go,

The input CSV needs a column called IPAddress.

Also generates a nice report to CSV file at end for you.

$Port = 53
$Report = @()
$Servers = Import-CSV c:\folder\filename.csv
Foreach ($Server in $Servers) {
		$Alive = Test-NetConnection -Port $port -ComputerName $Server.IPAddress | Select TcpTestSucceeded
		IF ($Alive.TcpTestSucceeded -eq "True") {
			$TestResult = New-Object psobject -Property @{
				IPAddress = $Server.IPAddress
				Result = "Port " + $Port + " is OPEN"
			}
		}
		ELSE {
			$TestResult = New-Object psobject -Prop @{
				IPAddress = $Server.IPAddress
				Result = "Port " + $Port + " is CLOSED"
			}
		}
		$Report += $TestResult
}		
$Report | select IPAddress, Result | Export-Csv c:\Folder\Report.csv -nti

Open in new window

nico-

ASKER
Hello

Very close.  I'm testing 2 servers where I can connect to one but not the other (tested using telnet and test-netconnection.

Your report has both the result of both being closed

Can you re-check

thank you
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Guy Lidbetter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
nico-

ASKER
Absolutely superb.  Shame it's worth only 500 points!  I couldn't find that anywhere on the Internet and then remembered I had an account on hrere!

thanks for you help.  much appreciated.