Let's tackle this step by step.
1) Statistical data for VMs that can have been vmotioned
There is a problem when you ask for -Realtime statistics.
The reason being that these data are kept on the ESX server, not on the VC.
The Get-Stat cmdlet only returns data for the ESX host on which the VM is currently running.
So if we want to see statistical data for VMs that have been vmotioned it is advised to go for one of the historical intervals !
Note that this is not a VITK problem, the QueryPerf method in the SDK displays the same behavior.
2) How to calculate the usage % against the cluster ?
I don't think the script should ask for percentages for the statistics (ex. cpu.usage.average).
In this case it's better to go for statistics that are expressed in a value for which we also have a total value on the cluster level.
What this means is the script should measure for example CPU usage in MHz (cpu.usagemhz.average).
3) What do we take as the available resources on the cluster ?
On a cluster we can do a straightforward summation of the available resources on each of the clusternodes.
For example, a cluster with 3 nodes where each node has a quadcore of 2.669 GHz would have 3 x 4 x 2.669 GHz of CPU resources available.
But is that what we want to measure the VM consumption against ?
On an ESX server (a clusternode) only part of the total available resources on the server is allocated to the guests.
Part of the available resources goes to the "system".
Taking the previous points into account the following script, in my opinion, expresses the CPU and memory usage of each VM as a percentage of the available cluster resources.
$report = @()
$cluster = Get-Cluster <clsutre-name> | Get-View
$clusterCPU = $cluster.Summary.EffectiveCpu
$clusterMem = $cluster.Summary.EffectiveMemory * 1Mb
$from = [Datetime]"03/05/2009 00:00"
$to = [Datetime]"03/05/2009 23:59"
Get-VM | % {
$cpu = $_ | Get-Stat -Stat cpu.usagemhz.average -IntervalMins 5 -Start $from -Finish $to
$mem = $_ | Get-Stat -Stat mem.consumed.average -IntervalMins 5 -Start $from -Finish $to
for($i=0; $i -lt $cpu.Count; $i++){
$row = "" | select VM, Timestamp, CPUperc, Memperc
$row.VM = $cpu[$i].Entity.Name
$row.Timestamp = $cpu[$i].Timestamp
$row.CPUperc = "{0:N2}" -f ($cpu[$i].Value / $clusterCPU * 100)
$row.Memperc = "{0:N4}" -f ($mem[$i].Value / $clusterMem * 100)
$report += $row
}
}
$report