Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: PowerShell: Get-NetTCPConnection script that also shows Username & Process Name

luckman212:

I created a script to output Get-NetTCPConnection data but additionally show Process Name and Username. The script does work, but I would love any tips to simplify or make this more canonical.

I am wondering if there is a more efficient way to add the ProcessName and Username to the output without having to preload the values into the custom PSObject ($obj array). I am concerned that the custom e={($obj |? PID -eq $_.OwningProcess | select -ExpandProperty UserName)}} expression is overly complex.


[email protected]()

Foreach($p In (Get-Process -IncludeUserName | where {$_.UserName} | `
select Id, ProcessName, UserName)) {
$properties = @{ 'PID'=$p.Id;
'ProcessName'=$p.ProcessName;
'UserName'=$p.UserName;
}
$psobj = New-Object -TypeName psobject -Property $properties
$obj+=$psobj
}

Get-NetTCPConnection | where {$_.State -eq "Established"} | select `
RemoteAddress, `
RemotePort, `
@{n="PID";e={$_.OwningProcess}}, @{n="ProcessName";e={($obj |? PID -eq $_.OwningProcess | select -ExpandProperty ProcessName)}}, `
@{n="UserName";e={($obj |? PID -eq $_.OwningProcess | select -ExpandProperty UserName)}} |
sort -Property ProcessName, UserName |
ft -auto

Here's a screenshot with some sample output:



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: PowerShell: Get-NetTCPConnection script that also shows Username & Process Name

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×