我知道工作代码是
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