如何使用PowerShell脚本获取每个内核或处理器的负载?

时间:2014-11-24 12:45:13

标签: powershell

我为Windows Server 2012编写了以下脚本& Windows 7使用Get-WmiObject -Class Win32_PerfFormattedData_PerfOS_Processor显示每个处理器的负载。它运行得很好并且还在控制台上显示负载,但是当我继续使用性能监视器(处理器时间百分比)检查控制台输出时,它与控制台输出不匹配。我的脚本用于检查每个处理器的负载是否正确?如何使用性能监视器汇总我的脚本?

while(1)
{
$cpucores = get-wmiobject win32_perfformatteddata_perfos_processor 
$currenttime = get-date | out-file report.txt -append 
write-host
write-host "CPU Cores individually usage Monitoring"
write-host  $currenttime
for ($i=0; $i -lt $cpucores.count ; $i++) 
    {
    if($i -eq $cpucores.count-1) 
    {
    #write-host 
    $value=$cpucores[$i].percentprocessortime
    write-host "Total CPU " $value"% usage" 
    write-output "Total CPU  $value % usage" >> report.txt 
    write-output "..............................................................." >> report.txt

        }
    else
        {
        $load_value=[double]$cpucores[$i].percentprocessortime
        write-host "CPU Core"$i $load_value  "% usage"  -foregroundcolor green 
        write-output "CPU Core $i   $load_value %  usage" >> report.txt

        }
  }
  start-sleep -seconds 1

 }

1 个答案:

答案 0 :(得分:1)

您是否考虑过内置的Get-Counter cmdlet?

$counter = Get-Counter "\processor(*)\% processor time"
$counter.countersamples

我认为你会有更好的结果。