2012-06-20 110 views
2

似乎沒有任何關於如何獲取PowerShell中的進程的CPU百分比的簡單解釋。我搜索了它並在這裏搜索,我沒有看到任何明確的。有人可以用通俗的術語解釋如何獲得一個進程的CPU百分比?謝謝!Powershell獲取CPU百分比

這裏的東西,讓你開始;)

$id4u = gps | ? {$_.id -eq 412} 
function get_cpu_percentage { 
# Do something cool here 
} 
get_cpu_percentage $id4u 

回答

-1

這個怎麼樣?

gps powershell_ise | Select CPU 

請注意,這是一個scriptProperty,不會顯示任何遠程系統。這是已知的。

+0

gps wmplayer |選擇CPU給我一個值683.484375。我正在尋找100%的百分比。我不知道如何計算循環中的總CPU百分比。 –

2

使用WMI:

get-wmiobject Win32_PerfFormattedData_PerfProc_Process | ? { $_.name -eq 'powershell' } | select name, PercentProcessorTime 

功能:

function get_cpu_percentage ($id) 
{ 
(get-wmiobject Win32_PerfFormattedData_PerfProc_Process | ? { $_.idprocess -eq $id.id }).PercentProcessorTime 
} 

$id4u = gps | ? {$_.id -eq 412} 
get_cpu_percentage -id $id4u 
-1

GET櫃檯,提供系統性能信息。您也可以獲得有關單個過程的信息。這裏有更多的關於:http://social.technet.microsoft.com/Forums/lv/winserverpowershell/thread/8d7502d4-9e67-43f7-94da-01755b719cf8和這裏http://blogs.technet.com/b/heyscriptingguy/archive/2010/02/16/hey-scripting-guy-february-16-2010a.aspx 我不確定這是你在找什麼,但是這裏有一種使用進程名和get-counter的可能性。

注意:如果你有超過1個處理器,我相信你應該把結果除以處理器的數量。你可以用得到這個值:

$env:NUMBER_OF_PROCESSORS 



    function get_cpu_percentage { 
    param([string[]]$myProc) 
    return (get-counter -Counter "\Process($myProc)\% processor time" -SampleInterval 5 -MaxSamples 5 | select -ExpandProperty countersamples | select -ExpandProperty cookedvalue | Measure-Object -Average).average 
    } 
    get_cpu_percentage ("powershell") 

,或者使用PROC ID和GPS:

注:我相信通過GET-過程的返回值是使用的過程,因爲啓動的CPU秒。這不是使用的CPU百分比。

function get_cpu_since_start{ 
    param ([system.object]$p) 
    $id4ucpu = $p | select-object CPU 
    return $id4ucpu.CPU 
    } 
get_cpu_since_start (gps | ? {$_.id -eq 412})