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.
This topic is 4 years and 3 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
bfoster68
Posts: 4
Last visit: Thu Sep 21, 2023 12:39 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. $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.  
User avatar
Olga_B
Site Admin
Posts: 196
Last visit: Thu Mar 28, 2024 8:34 am

Re: Odd issue

Post by Olga_B »

Restart PowerShell Studio, As Administrator.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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
Last visit: Fri Mar 06, 2020 5:11 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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.
This topic is 4 years and 3 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked