powershell - Fine-tuning Get-Counter script for quicker execution -
below script process utilization of individual w3wp.exe app pools, problem each iteration takes 2 seconds there 25 app pools. can please me fine tune below script faster execution.
gwmi win32_process -filter 'name="w3wp.exe"' | % { $name=$_.name $cmd = $pattern.match($_.commandline).groups[1].value $procid = $_.processid $tmp = (get-counter "\process(*)\id process").countersamples | where-object {$_.cookedvalue -eq $procid} | select -expand path $calc = [regex]::match($tmp,'\(([^\)]+)\)').groups[1].value $cooked = (get-counter "\process($calc)\% processor time").countersamples | where-object {$_.instancename -notlike '_total'} | select -expand cookedvalue $cpuper = [math]::round( ($cooked/2), 0) echo $cpuper }
it looks get-counter
has minimum sample time of 1 second. resulting in minimum execution time of 1 second per call. best bet counters front , counters interested in.
this doing. prints process id , % processor time in table.
$proc = 'w3wp' $samples = get-counter '\process($proc*)\id process', '\process($proc*)\% processor time' | select -expand countersamples $samples | group { split-path $_.path } | ft @{ n='id'; e={$_.group[0].cookedvalue} }, @{ n='% processor'; e={[math]::round($_.group[1].cookedvalue/2, 0)} }
Comments
Post a Comment