Ever wondered how your VM’s are doing ?  What resources are they using, do they have too many CPU’s or too much memory ?

This simple one-liner will give you a list of each of your vms, it will tell you how many cpu’s, the amount of memory, average cpu usage for x amount of days and the average memory usage for x amount of days, how cool is that ?!

Get-VM |Where {$_.PowerState -eq "PoweredOn"} |
Select Name,Host,NumCpu,MemoryMB, @{N="Cpu.UsageMhz.Average";E={[Math]::Round((($_ |Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}}, @{N="Mem.Usage.Average";E={[Math]::Round((($_ |Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}} |Export-Csv c:\Temp\stats.csv

Update 03 July 2009: This has been updated as it was not collecting the average over the given timescales, the original code was selecting 1 sample from the timeframe.

Sample output:

Name Host NumCpu MemoryMB Mem.Usage.Average Cpu.UsageMhz.Average
tstVIC01 tstesx02 1 512 25.32 121
tstVCT01 tstesx04 1 1536 14.32 144
tstFOG01 tstesx03 1 2048 12.79 72
ELGMSF01 tstesx01 1 512 20.19 748
tstADC02 tstesx02 1 384 10.32 17
ELGFIL01 tstesx04 1 384 7.19 20
tstADC01 tstesx01 1 384 10.25 16
VISTA tstesx04 1 768 10.99 5
tstWEB01 tstesx01 1 512 49.79 124
ELGXCH01 tstesx02 1 512 15.59 46
ELGADC01 tstesx01 1 512 11.52 19
SmallVM tstesx04 1 512 0 5
tstSQL01 tstesx04 1 512 11.99 58