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

Setting Trace Logging for MS Dynamics CRM

Function 1: Set CRM Tracing # crmTraceLogSettings.ps1 $traceDirectory="L:\crmTraceLogs" $enable=$false $categories='*:\Verbose' $fileSize=10 $resetIis=$true function setCrmTrace{ param…

PowerShell: Restart a Service on All Hyper-V Hosts of a Cluster

$serviceName='vmms' $clusterName='HyperV-cluster001' function restartServiceAllClusterNodes($service='vmms',$clusterName){ function restartService($serviceName){ $waitSeconds=40 $isValidProcess=try{[bool](get-service $serviceName -EA Stop)}catch{$false} if($isValidProcess){ try{ $process=Start-Process -FilePath…

JavaScript: Build a Tribute Page

Demo: https:// codepen.io/dragoncoin/pen/EZJxYY HTML Code: <html><head> <body> <img src="http://www.animationxpress.com/wp-content/uploads/2015/10/Mahatma-Gandhi.jpg"> <h1>Mahatma Gandhi</h1> <div class="hidden"> <h2>"You must…