2014-11-24 53 views
-1

我已經爲Windows Server 2012編寫了以下腳本& Windows 7,它使用Get-WmiObject -Class Win32_PerfFormattedData_PerfOS_Processor來顯示每個處理器的負載。它運行良好,並且還在控制檯上顯示負載,但是當我持續檢查具有性能監視器(處理器時間百分比)的控制檯輸出時,它與控制檯輸出不匹配。我的腳本是否檢查每個處理器的負載是否正確?我如何用性能監視器來總結我的腳本?如何使用PowerShell腳本獲取每個內核或處理器的負載?

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

您是否考慮過在Get-Counter cmdlet中構建?

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

我想你會有更好的結果。

相關問題