Test-NetConnection against more than One IP?

I have an array of servers and wanted to test connectivity over a particular port to 2 other servers

I tried this:

$servers = Get-Content C:\temp\servers.csv

    foreach($server in $servers){
    Invoke-Command -computername $server  -ScriptBlock {test-netconnection -computer '172.20.0.100','172.21.0.105' -port 53 }
}

I don’t get an error but wanted to make sure my use of Test-NetConnection is accurate. Help says -Computername can accept Pipeline input. So, does that mean I should put another variable in there to represent those 2 computers? Or is the string I put in accurate?

Thanks

Test-Connection will take an array of strings in the -computername parameter like you do in your example so that works. BTW, so does Invoke-Command so you don’t need the foreach loop just pass $servers in the -computername parameter of Invoke-Command. This should make your script run faster since the commands will be in parallel (multiple servers running at the same time) instead of sequential (foreach loop).

The -computername parameter of Test-Connection will accept pipeline input if you want but it is by property name and not by value so you couldn’t pass it an array of string literals like you do in your example.

Mike,

sorry for not being clear.

The values in $servers are one set of servers (from each I want to run invoke-command)

The -computer IPs are another, different set of servers that should be checked to see if the port is open.

That is why I doubt my original code

I know. That is what my recommendation will do. Invoke-Command will send the script block to ALL the machines identified in the -computername parameter at the same time. As each computer receives those instructions, they will execute the script (as received so in parallel) and return the results back when complete. In the results (objects) returned, there will be a new property automatically added (technically 2 new ones), called PSComputername. The PSComputername property will identify which remote machine the object belongs to so you don’t have to worry about what machine the results came from.

I revised the code a bit to match what you’re saying (I couldn’t get Test-Connection to work at all with the parameter -tcpPort, per the Help, it’s not there on server 2019) but I’m still missing something when I try anything more than one computername string

This works (but only one computername)

Invoke-Command -computername $servers  -ScriptBlock {Test-NetConnection  -ComputerName 'IP#1'  -Port 53 }

ComputerName     : IP#1
RemoteAddress    : IP#1
RemotePort       : 53
InterfaceAlias   : Ethernet 2
SourceAddress    : Server IP Address
TcpTestSucceeded : True
PSComputerName   : Server host name

This is wonky:

Invoke-Command -computername $servers  -ScriptBlock {Test-NetConnection  -ComputerName 'IP#1','IP#2' -Port 53 }

WARNING: Name resolution of IP#1 IP#2 failed
ComputerName   : IP#1 IP#2