Powershell终止过程 - 我的字符串有什么问题?



我知道工作代码是

Get-Process firefo* | Stop-Process

但我的第一个猜测是

Get-Process | findstr firefox | Stop-Process

它不工作。

Stop-Process:输入对象不能绑定到命令的任何参数要么是因为命令不接受管道输入,要么是因为输入和它的属性与接受管道输入的任何参数不匹配。在第1行char:33+ Get-Process | findstr firefox | Stop-Process+                                 ~~~~~~~~~~~~+ CategoryInfo: InvalidArgument:(1379 317…:PSObject) [Stop-Process], ParameterBindingException+ fulllyqualifiederrorid: InputObjectNotBound,Microsoft.PowerShell.Commands.StopProcessCommand

我明白字符串

 1342 306 1228412 1279864 -1671…71,42 35912 firefox

对于进程终止是不好的,但是为什么呢?

PS C:Usersadamg> Get-Process firefo*
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
   1342     306  1228412    1279864 -1671 ...71,42  35912 firefox

上面的工作很好,即使在回复中有列标题。

findstr是一个生成字符串输出的命令行实用程序。Get-Process输出Process对象,这是Stop-Process期望的输入。它也可以处理一个进程id列表,但是它不能解析findstr的格式化字符串。

在PowerShell中,你通常不会使用findstr。使用Where-Object过滤器代替:

Get-Process | Where-Object { $_.ProcessName -like '*firefox*' } | Stop-Process

最新更新