简体   繁体   中英

How can i show only local address and PID in powershell?

 (netstat -ano) -replace '0\.0\.0\.0:(\d+)','$1        '

what i want is to show only local address and pid using above code

  Proto  Local Address          Foreign Address        State           PID
  TCP    135                    0                      LISTENING       1172
  TCP    445                    0                      LISTENING       4
  TCP    5040                   0                      LISTENING       7300
  TCP    5357                   0                      LISTENING       4
  TCP    7680                   0                      LISTENING       14100
  TCP    49664                  0                      LISTENING       988
  TCP    49665                  0                      LISTENING       896
  TCP    49666                  0                      LISTENING       1724
  TCP    49667                  0                      LISTENING       1472
  TCP    49668                  0                      LISTENING       3520
  TCP    49728                  0                      LISTENING       968

but what i want

Local Address             PID
135                       1172
445                       4
5040                      7300
.                         .
.                         .
.                         .

To just get the output you're requesting by column headers in Powershell, you can use:

Get-NetTCPConnection | 
  Select-Object LocalAddress, OwningProcess | 
  Sort-Object LocalAddress, OwningProcess

Your example data seems to show ports rather than addresses, so:

Get-NetTCPConnection | 
  Select-Object LocalPort, OwningProcess | 
  Sort-Object LocalPort, OwningProcess

If you also need UDP connections, substitute Get-NetUDPEndpoint for Get-NetTCPConnection above.

Finally, if you want it all together:

(Get-NetUDPEndpoint |
    Select-Object @{L="Proto"; E={"TCP"}}, LocalAddress, LocalPort, OwningProcess
) + (Get-NetTCPConnection |
    Select-Object @{L="Proto"; E={"UDP"}}, LocalAddress, LocalPort, OwningProcess
) |
    Sort-Object Proto, LocalPort, LocalAddress

Will this do what you want? I assume by "Local Address" you mean port number, and by "PID" you mean the owning process ID. I would not want to rename them if I could avoid it.

Get-NetTCPConnection |
    Where-Object { $_.LocalAddress -notmatch '^(127\..*|192\..*|0\..*|::$)' } |
    Select-Object -Property @{n='LocalAddress';e={$_.LocalPort}},@{n='PID';e={$_.OwningProcess}} |
    Sort-Object -Property LocalAddress

You've got the information you need, now just trim out the excess properties with a nasty regex loop? may need some spacing adjustments

 (netstat -ano) `
  -replace '0\.0\.0\.0:(\d+)','$1        '|
  foreach{
    Write-host `
     -nonewline "$([regex]::split("$_",'\s\s+')[2,5])`r`n"
  }

I've used grave-accent (poweshell escape-key) to escape linebreaks for readability | will pipe to whatever comes on the next line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM