2016-10-02 43 views
0

我知道,工作代码是Powershell查杀流程 - 我的字符串有什么问题?

Get-Process firefo* | Stop-Process 

但我的第一个猜测是

Get-Process | findstr firefox | Stop-Process 

它没有工作。

Stop-Process : The input object cannot be bound to any parameters for the command 
either because the command does not take pipeline input or the input and its 
properties do not match any of the parameters that take pipeline input. 
At line:1 char:33 
+ Get-Process | findstr firefox | Stop-Process 
+         ~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidArgument: ( 1379  317...    :PSObject) [Stop-Process], ParameterBindingException 
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.StopProcessCommand

据我所知,串

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

是坏的过程中杀死,但为什么呢?

PS C:\Users\adamg> 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 

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

+0

您必须查看cmdlet的'Get-Help -full'并查看Input和Output对象。 dos命令'findstr'只生成控制台文本作为输出,不能用作Powershell cmdlet的输入。 – user4317867

回答

2

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

在PowerShell中,您通常不会使用findstr。改为使用Where-Object过滤器:

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