Posted On January 30, 2023

PowerShell: Get Connected Port by Process Name

kimconnect 0 comments
blog.KimConnect.com >> Codes , Windows >> PowerShell: Get Connected Port by Process Name
# 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

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Quick Notes of Beachy Systems Upgrade

Projects Proposal for Beachy Group (Fictional Entity)   Initial Fixed Costs Overview: SonicWall TZ215 $370…

How To Install Graylog in a Kubernetes Cluster Using Helm Charts

The following narrative is based on the assumption that a Kubernetes (current stable version 20.10)…

Cisco Fabric Switch – MDS Zoning Template

<# What this script does: 1. Checks to see if an Internet connection via PowerShell…