The output window correctly displays ProcessName and UserName.
However, if I run this in console It fails to populate these two fields.
I'm stuck, thoughts?
>> Running (get-nettcp.ps1) Script...
>> Platform: V5 64Bit (STA) (Elevated)
RemoteAddress LocalPort PID ProcessName UserName
------------- --------- --- ----------- --------
74.125.21.188 51885 6116 chrome AMER\fosterbi
18.213.221.141 60019 6116 chrome AMER\fosterbi
- # Make a lookup table by process ID
- $Processes = @{ }
- Get-Process -IncludeUserName | ForEach-Object {
- $Processes[$_.Id] = $_
- }
- Get-NetTCPConnection |
- Where-Object { $_.State -eq "Established" } |
- Select-Object RemoteAddress,
- LocalPort,
- @{ Name = "PID"; Expression = { $_.OwningProcess } },
- @{ Name = "ProcessName"; Expression = { $Processes[[int]$_.OwningProcess].ProcessName } },
- @{ Name = "UserName"; Expression = { $Processes[[int]$_.OwningProcess].UserName } } |
- Sort-Object -Property ProcessName, UserName |
- Format-Table -AutoSize