Last active
April 26, 2023 18:02
-
-
Save tallpeak/4126b4eddf58e36aa2e1b123af765fc2 to your computer and use it in GitHub Desktop.
All ports for each process, with CommandLine, on PowerShell 7.x (for CommandLine support)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/questions/17563411/how-to-get-command-line-info-for-a-process-in-powershell-or-c-sharp | |
if ($PSVersionTable.PSVersion.Major -lt 6 ) { | |
$TypeData = @{ | |
TypeName = 'System.Diagnostics.Process' | |
MemberType = 'ScriptProperty' | |
MemberName = 'CommandLine' | |
Value = {(Get-CimInstance Win32_Process -Filter "ProcessId = $($this.Id)").CommandLine} | |
} | |
Update-TypeData @TypeData | |
} | |
Function Get-PortsByProcess() { | |
$procs = Get-NetTCPConnection | | |
Select-Object LocalPort,OwningProcess | | |
Group-Object -Property OwningProcess | | |
ForEach-Object { | |
[pscustomobject]@{ Ports = ( ($_.Group | Select-Object LocalPort | | |
Sort-Object -Unique -Property LocalPort ) | %{$_.LocalPort} ) -join ","; | |
MinPort = ($_.Group | Select-Object LocalPort | | |
Measure-Object -Minimum -Property LocalPort ).Minimum | |
Pid = $_.Name ; | |
CommandLine=(Get-Process -Id $_.Name).CommandLine } | |
} | | |
Sort-Object -Property MinPort | | |
Select-Object Ports, Pid, CommandLine | | |
Out-GridView -PassThru | |
#Format-List | |
if ($procs) { | |
foreach($proc in $procs) { | |
$proc | Format-List | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment