I created this script because I needed to get performance information for a specific timeframe for multiple VMs out of VirtualCenter and into (something like) Excel for further analysis, before logs were rolled up. I'm still just learning Powershell so I'm sure this could be improved upon, but I'm very happy with the results. Things that could be changed:
Read the list of VMs from a text file ($VMs=gc c:\vms.txt)
Get stats for vCenter established groups of VMs would be a relatively simple change, like all those in a cluster or on a given host. (Get-cluster | get-vm)
A more user-friendly way to change the timeframe being captured, some kind of configurable variable. Currently hardcoded in script to "-realtime", which is in my environment 1 hr of data with 20 second data points.
#Configurable variables
$VMs = @("<server 1 name>","<server 2 name>","<server 3 name>","<etc>")
$outfolder = "c:\temp"
$metric = "disk.usage.average"
$VIServer = "<vCenter host name>"
#Main script
Connect-VIServer -Server $VIServer -credential (get-credential)
clear-host
$start=get-date
write-host "Started "(Get-Date)
$VMObjs = @()
$VMs | % {$i=1} {
Write-Progress "Getting VM managed object references..." $_ -percentcomplete (($i/$VMs.Length)*100)
$vmview = Get-View -ViewType VirtualMachine -Filter @{"Name" = "^$_$"}
$VMObjs += Get-VIObjectByVIView -moref $vmview.moref
$i++
}
write-progress "Getting VM managed object references..." "Completed" -Completed
write-host "Getting $metric stats..."
$stats = $VMObjs | get-stat -stat $metric -realtime
write-host "Recompiling data table... "
$new_stats = @()
$stats | sort timestamp | group timestamp |% {
$new_stat = "" | select timestamp
$new_stat.timestamp = $_.name
$_.group |% {
add-member -inputobject $new_stat -membertype noteproperty -name $_.entity -value $_.value
}
$new_stats += $new_stat
}
$outfile = "VMStats-$metric-{0:yyyyMMdd-HHmmss}.csv" -f ($start)
write-host "`nWriting output to $outfolder\$outfile"
$new_stats | Export-CSV "$outfolder\$outfile" -notypeinformation
Disconnect-VIServer -Confirm:$false
write-host Finished.
$finish=get-date
"Elapsed time: {0} seconds" -f (($finish-$start).totalseconds.tostring("0.00"))