Page 1 of 1

Odd issue

Posted: Thu Mar 05, 2020 9:36 am
by bfoster68
Using the code below while running in Powershell Studio 5.7.173
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

  1. # Make a lookup table by process ID
  2. $Processes = @{ }
  3. Get-Process -IncludeUserName | ForEach-Object {
  4.     $Processes[$_.Id] = $_
  5. }
  6.  
  7. Get-NetTCPConnection |
  8. Where-Object { $_.State -eq "Established" } |
  9. Select-Object RemoteAddress,
  10.               LocalPort,
  11.               @{ Name = "PID"; Expression = { $_.OwningProcess } },
  12.               @{ Name = "ProcessName"; Expression = { $Processes[[int]$_.OwningProcess].ProcessName } },
  13.               @{ Name = "UserName"; Expression = { $Processes[[int]$_.OwningProcess].UserName } } |
  14. Sort-Object -Property ProcessName, UserName |
  15. Format-Table -AutoSize
  16.  

Re: Odd issue

Posted: Thu Mar 05, 2020 11:00 am
by Olga_B
Restart PowerShell Studio, As Administrator.

Re: Odd issue

Posted: Thu Mar 05, 2020 2:46 pm
by jvierra
This is about the only way to do this. Select run elevated in PSS or run in an elevvated console session.

Code: Select all

$properties = @(
    @{ n = 'ProcessName'; e = { (Get-Process -Id $_.OwningProcess -IncludeUserName).ProcessName } },
    @{ n = 'UserName';    e = { (Get-Process -Id $_.OwningProcess -IncludeUserName).UserName } },
    @{ n = 'PID';         e = { $_.OwningProcess } },
    'RemoteAddress',
    'LocalPort'
)
Get-NetTCPConnection |
    Where-Object { $_.State -eq 'Established' } |
    Select-Object $properties |
    Sort-Object -Property ProcessName, UserName |
    Format-Table -AutoSize

Re: Odd issue

Posted: Thu Mar 05, 2020 3:01 pm
by jvierra
The following also works correctly:

Code: Select all

Get-Process -IncludeUserName | 
    ForEach-Object -Begin {$processes = @{}} -Process {$processes.Add($_.ID,$_)}
$properties = @(
    @{ n = 'ProcessName'; e = { $processes[[int32]$_.OwningProcess].ProcessName } },
    @{ n = 'UserName';    e = { $processes[[int32]$_.OwningProcess].Username } },
    @{ n = 'PID';         e = { $_.OwningProcess } },
    'RemoteAddress',
    'LocalPort'
)
Get-NetTCPConnection |
    Where-Object { $_.State -eq 'Established' } |
    Select-Object $properties |
    Sort-Object -Property ProcessName, UserName |
    Format-Table -AutoSize

Re: Odd issue

Posted: Fri Mar 06, 2020 5:11 am
by Ricardofes
Is it possible to add minor things to this code or will any changes break it?

Re: Odd issue

Posted: Fri Mar 06, 2020 6:58 am
by jvierra
What are "minor things"? There is nothing stopping you from changing the code as long as you do it correctly.