CPU 비율 및 메모리 사용량을 표시하는 Windows Powershell

CPU 비율 및 메모리 사용량을 표시하는 Windows Powershell

답변을 사용할 수 있습니다.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는 100이 아니라 400입니다. 따라서 전체 사용률(최대 100)을 얻으려면 각 프로세스의 CPU 값을 코어 수로 나누어야 합니다.

$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)$"}=> 프로세스 이름의 첫 번째 부분에 유휴, _total 또는 시스템이 있는 프로세스 이름을 제거합니다.

Sort-Object=> 프로세스를 CPU 사용량에 따라 내림차순으로 정렬합니다.

Select-Object=> 처음 15개 항목만 선택(상위 15개 CPU 프로세스)

ft=> 열 제목과 전체 화면을 사용하여 테이블 형식을 제공합니다.

start-sleep=> 대략 5초마다 실행되도록 루프를 잠자기 상태로 유지합니다.

관련 정보