![需要从 PS 命令中过滤特定进程的结果](https://linux22.com/image/1583292/%E9%9C%80%E8%A6%81%E4%BB%8E%20PS%20%E5%91%BD%E4%BB%A4%E4%B8%AD%E8%BF%87%E6%BB%A4%E7%89%B9%E5%AE%9A%E8%BF%9B%E7%A8%8B%E7%9A%84%E7%BB%93%E6%9E%9C.png)
get-nettcpconnection | select local*,remote*,state,@{Name='Process';Expression={(Get-Process -Id $_.OwningProcess).ProcessName }}
正在列出所有进程,我需要按进程名称进行过滤。
答案1
如果我正确理解了这个问题,您只想返回某些特定的流程。您可以使用where
过滤器来做到这一点。
添加您想要返回的流程$searchfor
$searchfor = @("wininit","TeamViewer")
get-nettcpconnection |
select local*,remote*,state,@{Name='Process';Expression={(Get-Process -Id $_.OwningProcess).ProcessName }} |
where { $_.Process -in $searchfor }
您也可以这样做,这可能会更快一些,因为您get-nettcpconnection
只针对您感兴趣的进程运行:
$searchfor = @("wininit","TeamViewer")
Get-Process $searchfor | % {
$Proc = $_
Get-nettcpconnection -OwningProcess $_.ID |
select local*,remote*,state,@{Name='Process';Expression={$Proc.ProcessName}}
}