Odd issue

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
bfoster68
Posts: 4
Joined: Sun Mar 03, 2013 12:41 pm

Odd issue

Post 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.  

User avatar
Olga_B
Site Admin
Posts: 149
Joined: Mon May 15, 2017 9:06 am

Re: Odd issue

Post by Olga_B »

Restart PowerShell Studio, As Administrator.

jvierra
Posts: 14579
Joined: Tue May 22, 2007 9:57 am
Answers: 1
Been upvoted: 1 time
Contact:

Re: Odd issue

Post 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

jvierra
Posts: 14579
Joined: Tue May 22, 2007 9:57 am
Answers: 1
Been upvoted: 1 time
Contact:

Re: Odd issue

Post 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

Ricardofes
Posts: 1
Joined: Thu Mar 05, 2020 4:27 am

Re: Odd issue

Post by Ricardofes »

Is it possible to add minor things to this code or will any changes break it?

jvierra
Posts: 14579
Joined: Tue May 22, 2007 9:57 am
Answers: 1
Been upvoted: 1 time
Contact:

Re: Odd issue

Post by jvierra »

What are "minor things"? There is nothing stopping you from changing the code as long as you do it correctly.

Locked