windows powershell顯示CPU百分比和記憶體使用情況

windows powershell顯示CPU百分比和記憶體使用情況

我可以使用答案CPU百分比

While(1) { $p = get-counter '\Process(*)\% Processor Time'; cls; 
$p.CounterSamples | sort -des CookedValue | select -f 15 | ft -a}

現在,我遇到了無法同時顯示電腦上任務的記憶體使用情況和 CPU 百分比的問題。我知道Get-Counter "\Process(*)\Working Set - Private"會獲取內存,但我喜歡同時顯示任務、CPU % 和內存使用情況。

更新1:理想情況下,我希望能夠在 powershell 中將其作為一組命令運行。我希望輸出顯示為:

Process Name CPU (%) Memory (MB) ------------ ------- -----------

在能夠僅在 powershell 中顯示這三個項目後,我將採取在顯示時可以處理的內容。

更新2:這部分程式碼提供了我想要的

while (1) {cls; Get-WmiObject Win32_PerfFormattedData_PerfProc_Process |
  select-object -property Name, PercentProcessorTime, IDProcess, @{"Name" = "WSP(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}} |
  ? {$_.Name -notmatch "^(idle|_total|system)$"} |
  Sort-Object -Property PercentProcessorTime -Descending|
  ft -AutoSize |
  Select-Object -First 15; start-sleep 5}

這將導致以下顯示:

Name                   PercentProcessorTime IDProcess                   WSP(MB)
----                    -------------------- ---------                   -------
MicrosoftEdgeCP#3                          6     18972                        6 WmiPrvSE#3                                 6     27300                        7
svchost#22                                 0      7316                        1

唯一的缺點是它在顯示一次訊息後失敗,我不知道為什麼。對此有什麼幫助嗎?

更新:3

$cores = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors
while ($true) {
    $tmp = Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | 
    select-object -property Name, @{Name = "CPU"; Expression = {($_.PercentProcessorTime/$cores)}}, @{Name = "PID"; Expression = {$_.IDProcess}}, @{"Name" = "Memory(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}} |
    Where-Object {$_.Name -notmatch "^(idle|_total|system)$"} |
    Sort-Object -Property CPU -Descending|
    Select-Object -First 15;
    cls;
    $tmp | Format-Table -Autosize -Property Name, CPU, PID, "Memory(MB)";
    Start-Sleep 5
}

實現了我所需要的一切,並為我的未來提供了一個起點。我對錶格式化程式的問題是它format-table必須是最後一個。

答案1

還有另一種透過 WMI 查詢效能計數器的方法,我認為它可以滿足您的需求。輸出還包括進程 ID,這在追蹤具有多個實例的進程時非常有用。它還使用內部所謂的“計算屬性”select-object將工作設定值從位元組轉換為兆位元組。另請注意,4 核心系統上的最大 CPU 為 400,而不是 100。

$cores = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors
while ($true) {
    Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | 
    Where-Object {$_.Name -notmatch "^(idle|_total|system)$" -and $_.PercentProcessorTime -gt 0} |
    Format-Table -Autosize -Property @{Name = "CPU"; Expression = {[int]($_.PercentProcessorTime/$cores)}}, Name, @{Name = "PID"; Expression = {$_.IDProcess}}, @{"Name" = "WSP(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}}
    Start-Sleep 5
}

CPU Name            PID WSP(MB)
--- ----      --------- -------
  1 chrome        12476      64
  2 wuauclt        7504       0
  3 SearchIndexer 10128      22
  4 TIworker      11380     102

答案2

while (1) {cls; Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | select-object -property Name, PercentProcessorTime, IDProcess, @{"Name" = "WSP(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}} | ? {$_.Name -notmatch "^(idle|_total|system)$"} | Sort-Object -Property PercentProcessorTime -Descending| Select-Object -First 15 | ft Name, PercentProcessorTime, IDProcess, "WSP(MB)" -AutoSize; start-sleep 5}

這就是我一直在尋找的。感謝@Clayton 提供了一個很好的起點。這是每個部分的作用的解釋。

while(1)=> 永遠運行

cls=> 清除目前螢幕

Get-WmiObject=> 用於獲取當前所有進程信息

select-object=> 僅選擇所需的資料列

? {$_.Name -notmatch "^(idle|_total|system)$"}=> 刪除進程名稱第一部分包含idle、_total或system的進程名稱

Sort-Object=> 依 cpu 使用率降序對進程進行排序

Select-Object=> 僅選擇前 15 項(前 15 個 cpu 進程)

ft=> 提供帶有列標題的表格格式並使用整個螢幕

start-sleep=> 使循環休眠,大約每 5 秒運行一次

相關內容