Below is my script to get process utilization of individual w3wp.exe app pools, the problem is each iteration takes about 2 seconds there are about 25 app pools. Can you please help me to fine tune the below script for 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
}