# getProcessConnectionPort.ps1

$computerlist=@'
SQL1
SQL2
'@

$computernames=@($computerList -split "`n")|%{$_.Trim()}
$processname='sqlservr'
$state='Established'

$results=@()
foreach ($computername in $computernames){
    $result=invoke-command -computername $computername{
        param($processName,$state='Established')
        try{
            $processid=(get-process $processName).Id            
            $ports=Get-NetTcpConnection -OwningProcess $processid -State $state|Group-Object -Property LocalPort
            write-host "$env:computername`: $processName PID is found as $processid running on port(s) $($ports.Name)"
            return $ports
        }catch{
            write-warning $_
            return $false
        }        
    } -Args $processName,$state
    $results+=$result
}
$results