您可以在 Windows PowerShell 中使用 Process Cmdlet 來管理本機和遠端處理序。
取得處理序 (Get-Process)
若要取得在本機電腦上執行的處理序,請執行 Get-Process 但不搭配任何參數。
您可以藉由指定處理序的名稱或識別碼來取得特定的處理序。下列命令會取得 Idle 處理序。
PS> Get-Process -id 0 Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 0 0 0 16 0 0 Idle
雖然 Cmdlet 沒有傳回資料有時候算是正常的情形,當您使用 ProcessId 指定處理序時,Get-Process 若找不到相符項目便會產生錯誤,因為其通用目的是擷取執行中的已知處理序。如果指定 Id 的處理序不存在,即表示 Id 不正確或是該處理序已結束:
PS> Get-Process -Id 99 Get-Process : No process with process ID 99 was found. At line:1 char:12 + Get-Process <<<< -Id 99
使用 Get-Process Cmdlet 的 Name 參數可依處理序名稱指定處理序的子集。Name 參數可以使用以逗號分隔的清單中的多個名稱,而且支援萬用字元,所以可以輸入名稱模式。
例如,下列命令會取得名稱以 "ex" 起始的處理序。
PS> Get-Process -Name ex* Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 234 7 5572 12484 134 2.98 1684 EXCEL 555 15 34500 12384 134 105.25 728 explorer
因為 .NET System.Diagnostics.Process 類別是 Windows PowerShell 處理序的基礎,所以會遵循 System.Diagnostics.Process 所使用的一些慣例。其中一項慣例是,可執行檔的處理序名稱絕不會包含可執行檔名稱結尾的 ".exe"。
Get-Process 的 Name 參數也接受多重值。
PS> Get-Process -Name exp*,power* Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 540 15 35172 48148 141 88.44 408 explorer 605 9 30668 29800 155 7.11 3052 powershell
您可以使用 Get-Process 的 ComputerName 參數取得遠端電腦的處理序。例如,下列命令會取得本機電腦 (由 "localhost" 代表) 及兩部遠端電腦的 PowerShell 處理序。
PS> Get-Process -Name PowerShell -ComputerName localhost, Server01, Server02 Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 258 8 29772 38636 130 3700 powershell 398 24 75988 76800 572 5816 powershell 605 9 30668 29800 155 7.11 3052 powershell
在這個顯示中並看不到電腦名稱,但這些名稱會儲存在 Get-Process 所傳回的處理序物件的 MachineName 屬性中。下列命令會使用 Format-Table Cmdlet 來顯示處理序物件的 process ID、ProcessName 和 MachineName (ComputerName) 屬性。
PS> Get-Process -Name PowerShell -ComputerName localhost, Server01, Server01 | Format-Table -Property ID, ProcessName, MachineName Id ProcessName MachineName -- ----------- ----------- 3700 powershell Server01 3052 powershell Server02 5816 powershell localhost
這個較複雜的命令會將 MachineName 屬性新增到標準的 Get-Process 顯示。倒引號字元 (`)(ASCII 96) 是 Windows PowerShell 接續字元。
get-process powershell -computername localhost, Server01, Server02 | format-table -property Handles, ` @{Label="NPM(K)";Expression={[int]($_.NPM/1024)}}, ` @{Label="PM(K)";Expression={[int]($_.PM/1024)}}, ` @{Label="WS(K)";Expression={[int]($_.WS/1024)}}, ` @{Label="VM(M)";Expression={[int]($_.VM/1MB)}}, ` @{Label="CPU(s)";Expression={if ($_.CPU -ne $()` {$_.CPU.ToString("N")}}}, ` Id, ProcessName, MachineName -auto Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName MachineName ------- ------ ----- ----- ----- ------ -- ----------- ----------- 258 8 29772 38636 130 3700 powershell Server01 398 24 75988 76800 572 5816 powershell localhost 605 9 30668 29800 155 7.11 3052 powershell Server02
停止處理序 (Stop-Process)
Windows PowerShell 提供列舉處理序的彈性,但若要停止處理序呢?
Stop-Process Cmdlet 會使用 Name 或 Id 來指定所要停止的處理序。能否停止處理序取決於您的權限。某些處理序絕不容許停止。例如,若您嘗試停止閒置處理序,就會發生錯誤:
PS> Stop-Process -Name Idle Stop-Process : Process 'Idle (0)' cannot be stopped due to the following error: Access is denied At line:1 char:13 + Stop-Process <<<< -Name Idle
您也可以使用 Confirm 參數強制顯示提示。當您使用萬用字元指定處理序名稱時,此參數特別有用,可避免意外停止了您不打算停止但符合比對條件的某些處理序:
PS> Stop-Process -Name t*,e* -Confirm Confirm Are you sure you want to perform this action? Performing operation "Stop-Process" on Target "explorer (408)". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):n Confirm Are you sure you want to perform this action? Performing operation "Stop-Process" on Target "taskmgr (4072)". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):n
使用某些物件篩選 Cmdlet 也能執行複雜的處理序操控。由於 Process 物件的 Responding 屬性值為 true 表示處理序不再有回應,您可使用下列命令停止所有毫無回應的應用程式:
Get-Process | Where-Object -FilterScript {$_.Responding -eq $false} | Stop-Process
同樣的做法也適用於其他情況。例如,假設有個次要的通知區域應用程式,會在使用者啟動其他應用程式時自動執行。您或許已發現該程式在終端機服務工作階段中無法正常執行,但仍想將其保留在執行於實體電腦主控台的工作階段中。連接到實體電腦桌面的工作階段其工作階段識別碼始終為 0,因此您可以使用Where-Object 以及處理序的 SessionId,將其他工作階段中的處理序執行個體全部終止:
Get-Process -Name BadApp | Where-Object -FilterScript {$_.SessionId -neq 0} | Stop-Process
Stop-Process Cmdlet 沒有 ComputerName 參數。因此,若要在遠端電腦上執行停止處理序命令,就需要使用 Invoke-Command Cmdlet。例如,若要停止 Server01 遠端電腦的 PowerShell 處理序,請輸入:
Invoke-Command -ComputerName Server01 {Stop-Process Powershell}
停止其他所有的 Windows PowerShell 工作階段
若能將目前工作階段除外其他所有執行中的 Windows PowerShell 工作階段全部停止,偶而可能有所幫助。如果工作階段佔用太多資源或已無法存取 (可能執行於遠端或其他的桌面工作階段),您或許就不能直接將其停止。但是,若您嘗試停止所有執行中的工作階段,目前工作階段反而也會終止。
每個 Windows PowerShell 工作階段都有 PID 環境變數,其中包含 Windows PowerShell 處理序的識別碼。您可以根據每個工作階段的識別碼來檢查 $PID,然後只終止具有不同識別碼的 Windows PowerShell 工作階段。下列管線命令會進行這項作業,並傳回已終止工作階段的清單 (由於使用了 PassThru 參數):
PS> Get-Process -Name powershell | Where-Object -FilterScript {$_.Id -ne $PID} | Stop-Process - PassThru Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 334 9 23348 29136 143 1.03 388 powershell 304 9 23152 29040 143 1.03 632 powershell 302 9 20916 26804 143 1.03 1116 powershell 335 9 25656 31412 143 1.09 3452 powershell 303 9 23156 29044 143 1.05 3608 powershell 287 9 21044 26928 143 1.02 3672 powershell
啟動、偵錯和等候處理序
Windows PowerShell 所隨附的 Cmdlet 也可用來啟動 (或重新啟動)、偵錯處理序,並可用來等候處理序完成後再執行命令。如需這些 Cmdlet 的詳細資訊,請參閱各個 Cmdlet 的說明主題。
請參閱