I'm trying to determine the average VM resource usage for a bunch of VM's. My main goal is determine which VMs can have their cores/memory reduced. I'm trying to run the below script, but I'm getting the same error for each VM "The metric counter "disk.usage.average" doesn't exist for entity".
Any ideas on how I can correct the error or get average resource usage a different way?
Connect-VIServer <Server Name Redacted>
$allvms = @()
$vms = Get-Vm
$stats = Get-Stat -Entity $vms -start (get-date).AddDays(-7) -Finish (Get-Date)-MaxSamples 10000 -stat "cpu.usage.average","mem.usage.average"
$stats | Group-Object -Property Entity | %{
$vmstat = "" | Select VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
$vmstat.VmName = $_.name
$cpu = $_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property value -Average -Maximum -Minimum
$mem = $_.Group | where {$_.MetricId -eq "mem.usage.average"} | Measure-Object -Property value -Average -Maximum -Minimum
$vmstat.CPUMax = [int]$cpu.Maximum
$vmstat.CPUAvg = [int]$cpu.Average
$vmstat.CPUMin = [int]$cpu.Minimum
$vmstat.MemMax = [int]$mem.Maximum
$vmstat.MemAvg = [int]$mem.Average
$vmstat.MemMin = [int]$mem.Minimum
$allvms += $vmstat
}
$allvms | Select VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | Export-Csv "c:\VM_Resources\VMs.csv" -noTypeInformation