Link to home
Start Free TrialLog in
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
Avatar of McKnife
McKnife
Flag of Germany image

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

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

Avatar of nico-
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
ASKER CERTIFIED SOLUTION
Avatar of Guy Lidbetter
Guy Lidbetter
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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.