2016-10-02 42 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