小编luc*_*212的帖子

PowerShell:Get-NetTCPConnection 脚本也显示用户名和进程名

我创建了一个脚本来输出Get-NetTCPConnection数据,但另外显示Process NameUsername。该脚本确实有效,但我喜欢任何简化或使其更规范的提示。

我想知道是否有更有效的方法将ProcessName和添加Username到输出中,而无需将值预加载到自定义 PSObject ($obj数组)中。我担心自定义e={($obj |? PID -eq $_.OwningProcess | select -ExpandProperty UserName)}}表达式过于复杂。

$obj=@()

Foreach($p In (Get-Process -IncludeUserName | where {$_.UserName} | `
  select Id, ProcessName, UserName)) {
      $properties = @{ 'PID'=$p.Id;
                       'ProcessName'=$p.ProcessName;
                       'UserName'=$p.UserName;
                     }
      $psobj = New-Object -TypeName psobject -Property $properties
      $obj+=$psobj
  }

Get-NetTCPConnection | where {$_.State -eq "Established"} | select `
  RemoteAddress, `
  RemotePort, `
  @{n="PID";e={$_.OwningProcess}}, @{n="ProcessName";e={($obj |? PID -eq $_.OwningProcess | …
Run Code Online (Sandbox Code Playgroud)

windows powershell netstat

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

netstat ×1

powershell ×1

windows ×1