Try something like this.
It uses the Export-Xlsx function from my Export-Xlsx, the sequel, and ordered data post.
$entities=Get-VM
$start= (Get-Date).AddHours(-2)
$stat="cpu.usage.average","mem.usage.average","disk.usage.average","disk.maxtotallatency.latest"
Get-Stat-Entity$entities-Stat$stat-Start$start|
Group-Object-Property {$_.Entity.Name} |%{
$vmName=$_.Values[0]
$reportCPU=@()
$reportMem=@()
$reportDisk=@()
$_.Group|Group-Object-PropertyTimestamp|%{
$reportCPU+=New-ObjectPSObject-Property@{
Time=$_.Group[0].Timestamp
Usage=$_.Group|where {$_.MetricId-eq"cpu.usage.average"} |Select-ExpandPropertyValue
}
$reportMem+=New-ObjectPSObject-Property@{
Time=$_.Group[0].Timestamp
Usage=$_.Group|where {$_.MetricId-eq"mem.usage.average"} |Select-ExpandPropertyValue
}
$reportDisk+=New-ObjectPSObject-Property@{
Time=$_.Group[0].Timestamp
Usage=$_.Group|where {$_.MetricId-eq"disk.usage.average"} |Select-ExpandPropertyValue
"Highest latency"=$_.Group|where {$_.MetricId-eq"disk.maxtotallatency.latest"} |Select-ExpandPropertyValue
}
}
$reportDisk|SelectTime,Usage,"Highest latency"|
Export-Xlsx-Path"C:\$vmName-report.xlsx"-WorksheetName"Disk"-ChartType"xlLine"
$reportMem|SelectTime,Usage|
Export-Xlsx-Path"C:\$vmName-report.xlsx"-WorksheetName"Mem"-ChartType"xlLine"
$reportCPU|SelectTime,Usage|
Export-Xlsx-Path"C:\$vmName-report.xlsx"-WorksheetName"CPU"-ChartType"xlLine"
}
I attached the complete script, including the Export-Xlsx function.