I am trying to query ports from one server to multiple remote servers but I can't figure it out.
Querying to one server wordks good but I want to add multiple remote servers in $destination variable and let the script loop over them to give me all the results in one shot.
The script I found:
$Servers = "127.0.0.1"
$Ports = "80",
"445",
"443"
$Destination = "Computer_1"
$Results = @()
$Results = Invoke-Command $Servers {param($Destination,$Ports)
$Object = New-Object PSCustomObject
$Object | Add-Member -MemberType NoteProperty -Name "ServerName" -Value
$env:COMPUTERNAME
$Object | Add-Member -MemberType NoteProperty -Name "Destination" -Value $Destination
Foreach ($P in $Ports){
$PortCheck = (Test-NetConnection -Port $p -ComputerName $Destination ).TcpTestSucceeded
If($PortCheck -notmatch "True|False"){$PortCheck = "ERROR"}
$Object | Add-Member Noteproperty "$("Port " + "$p")" -Value "$($PortCheck)"
}
$Object
} -ArgumentList $Destination,$Ports | select * -ExcludeProperty runspaceid, pscomputername
$Results | Out-GridView -Title "Testing Ports"
$Results | Format-Table -AutoSize
I get the results:
ServerName Destination Port 80 Port 445 Port 443
---------- ----------- -------- -------- --------
<local host> Computer_1 True True True
I need to add multiple remote in the $destination block. I have tried the following:
$Destination = "Computer_1","Computer_2"
But it is not the right way because I get this:
ServerName Destination Port 80 Port 445 Port 443
---------- ----------- -------- -------- --------
<local host> "Computer_1","Computer_2" False False False
I need to get this result:
ServerName Destination Port 80 Port 445 Port 443
---------- ----------- -------- -------- --------
<local host> Computer_1 True True True
<local host> Computer_2 True True True