Page 1 of 1

Odd issue

Posted: Thu Mar 05, 2020 10: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.  
  3. $Processes = @{ }
  4.  
  5. Get-Process -IncludeUserName | ForEach-Object {
  6.  
  7.     $Processes[$_.Id] = $_
  8.  
  9. }
  10.  
  11.  
  12.  
  13. Get-NetTCPConnection |
  14.  
  15. Where-Object { $_.State -eq "Established" } |
  16.  
  17. Select-Object RemoteAddress,
  18.  
  19.               LocalPort,
  20.  
  21.               @{ Name = "PID"; Expression = { $_.OwningProcess } },
  22.  
  23.               @{ Name = "ProcessName"; Expression = { $Processes[[int]$_.OwningProcess].ProcessName } },
  24.  
  25.               @{ Name = "UserName"; Expression = { $Processes[[int]$_.OwningProcess].UserName } } |
  26.  
  27. Sort-Object -Property ProcessName, UserName |
  28.  
  29. Format-Table -AutoSize
  30.  
  31.  

Re: Odd issue

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

Re: Odd issue

Posted: Thu Mar 05, 2020 3: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 4: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 6: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 7:58 am
by jvierra
What are "minor things"? There is nothing stopping you from changing the code as long as you do it correctly.