Hot Shot
Hot Shot

get VM CPU stats

getting snagged on this, but here is what I have:

$vms = get-cluster N-MMK-COOP-151-166 | Get-VM

foreach ($vm in $vms) {

get-stat -Entity $vm -Start 9/27/2016 -Finish 10/18/2016 -CPU | Select Value | export-csv -Path B:\scripts\GetHostStats\$vm.csv


The above creates a csv file for each VM.  I'd like to have the data for each VM just be a new column in the same csv file.  Thanks for any thoughts!

0 Kudos
1 Reply

Try something like this.

Note that the script will produce incorrect results should one of the VMs be poweredoff/poweredon during the interval.

To cover that situation, the script will need to be changed.

$clusterName = 'N-MMK-COOP-151-16'

$startDate = Get-Date 27/9/2016

$finishDate = Get-Date 18/10/2016

$vms = Get-Cluster -Name $clusterName | Get-VM

$report = Get-Stat -Entity $vms -Start $startDate -Finish $finishDate -CPU -ErrorAction SilentlyContinue |

Group-Object -Property Timestamp | Sort-Object -Property Name | %{

    $obj = [ordered]@{

            Date = $_.Name


    $_.Group | Group-Object -Property {$_.Entity.Name} | Sort-Object -Property Name | %{

        $_.Group | Sort-Object -Property MetricId | %{




    New-Object PSObject -Property $obj


$report | Export-Csv C:\report.csv -NoTypeInformation -UseCulture

Blog:  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos