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
nico-Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

McKnifeCommented:
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 LidbetterCommented:
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-Author Commented:
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
Guy LidbetterCommented:
Hi Nico,

I updated the script a little after posting it, as I left out the Array Value from the IF statement.
I've just retested the script above on 3 of my servers and I received and Open, Closed, Open result as expected.

You may have copied it before I updated the script so could you give it another go please?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nico-Author Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.