我试图从特定主机上的每个虚拟机中提取各种统计信息。一切正常,除了$maxcpu
部分....我是一个 PS 菜鸟,所以我只是尝试根据我在风格上拉出的其他统计数据来建模它,但将 CPU 计数器更改为.maximum
而不是.average
. 通过我的实验,我要么在 CSV 中得到一个空白条目,要么它崩溃并给出以下错误:
The metric counter "cpu.usagemhz.maximum" doesn't exist for entity "XXXX"
.
foreach ($vmhost in $hosts) {
$vms = Get-VM -Location $vmhost
$vmHost | echo
foreach ($vm in $vms) {
" " + $vm | echo
$avgcpu = @{N="Cpu.UsageMhz.Average";E={[Math]::Round((($_ |Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-7) | Measure-Object Value -Average).Average),2)}}
$maxcpu = @{N="Cpu.UsageMhz.Maximum";E={[Math]::Round((($_ |Get-Stat -Stat cpu.usagemhz.maximum -Start (Get-Date).AddDays(-7) | Measure-Object Value -Maximum).Maximum),2)}}
$avgmem = @{N="Mem.Consumed.Average";E={[Math]::Round((($_ |Get-Stat -Stat mem.consumed.average -Start (Get-Date).AddDays(-7) | Measure-Object Value -Average).Average),2)/1000}}
$vm | Select Host, Name, NumCpu, $avgcpu, $maxcpu, MemoryMB, $avgmem | Export-Csv -Append "somewhere\csv.csv"
}
}