我目前正在尝试在很长一段时间内收集流程的平均性能值。不幸的是,我的脚本只能平均所有收集的值,而不是单个进程的平均值。
下面的脚本是我最终得到的,遗憾的是它并没有为每个过程提供输出:
Write-Output (Get-Counter -Counter "\Processor(_Total)\% Processor Time","\Process(Chrome)\% Processor Time" -SampleInterval 1 -MaxSamples 25 |
Select-Object -ExpandProperty CounterSamples |
Select-Object -ExpandProperty CookedValue |
Measure-Object -Average).Average
理想情况下,我希望有一个对象的输出值格式如下:
Output.chrome = 5.1283123
Output.total = 23.128732
答案 0 :(得分:1)
这是一种方法:
$chrome = @()
$total = @()
$counterName = "\Processor(_Total)\% Processor Time","\Process(Chrome)\% Processor Time"
Get-Counter -Counter $counterName -SampleInterval 1 -MaxSamples 10 |
Select-Object -ExpandProperty countersamples | % {
$object = New-Object psobject -Property @{
InstanceName = $_.InstanceName
CookedValue = $_.CookedValue
}
if($object.InstanceName -eq "Chrome") {
$chrome += $object
} else {
$total += $object
}
}
$output = [PSCustomObject]@{
Chrome = ($chrome | Measure-Object -Average CookedValue).Average
Total = ($total| Measure-Object -Average CookedValue).Average
}
$output
输出:
Chrome Total
------ -----
5,61702990401208 31,4667298163454
答案 1 :(得分:1)
This works for me:
Get-Counter -Counter "\Processor(_Total)\% Processor Time","\Process(Chrome)\% Processor Time" -SampleInterval 1 -MaxSamples 25 `
| Select-Object -ExpandProperty CounterSamples `
| Group-Object -Property InstanceName `
| ForEach-Object {
$_ | Select-Object -Property Name, @{n='Average';e={($_.Group.CookedValue | Measure-Object -Average).Average}};
} `
| Format-Table -AutoSize;
Output:
Name Average
---- -------
_total 11.8878325281858
chrome 4.80058851283048
It'd be easy enough to conditionally rename the names.
EDIT: Try this for PowerShell v2.0:
Get-Counter -Counter "\Processor(_Total)\% Processor Time","\Process(Chrome)\% Processor Time" -SampleInterval 1 -MaxSamples 25 `
| Select-Object -ExpandProperty CounterSamples `
| Group-Object -Property InstanceName `
| ForEach-Object {
$_ | Select-Object -Property Name, @{n='Average';e={(($_.Group | Measure-Object -Property CookedValue -Average).Average)}};
} `
| Format-Table -AutoSize;