# testRemotePort.ps1

$connectFrom=@'
windows1
windows2
'@
$connectTo=@'
\\servername\sharename
'@
$testPort=445

$sources=@($connectFrom -split "`n")|%{$_.Trim()}
$destinations=@($connectTo -split "`n")|%{$_.Trim()}
$results=@()
foreach($source in $sources){
    foreach($destination in $destinations){
        $result=invoke-command -computername $source {
            param($destination,$testPort)
            write-host "$env:computername to $destination`:$testPort => " -nonewline
            $result=try{if(test-netconnection $destination -port $testPort -informationLevel Quiet){'Open'}else{'Closed'}}catch{write-warning $_;'Unknown'}
            write-host "$result"
            return $result
        } -Args $destination,$testPort
        $results+=[pscustomobject]@{
            source=$source
            destination=$destination
            port=$testPort
            status=$result
        }
    }
}