The script below exports performance data for the last 7 days for a specified list of virtual machines. Data is first stored into a hash table where it is then exported into CSV files using various filters. One CSV file is created per performance metric per virtual machine.
- You can modify $metrics as needed, see VMWare Performance Counters for a detailed list.
- The first two lines in the script are only required if executing the script automatically via Task Scheduler using Windows Powershell (and not PowerCLI).
- The TimeStamp attribute is modified so in Excel it shows as a ‘general’ field, rather than ‘custom’. ‘Custom’ fields were not working correctly when plotting the data into line graphs.
add-pssnapin VMware.VimAutomation.Core Connect-ViServer vcenter.domain.com $metrics = "cpu.usage.average","mem.usage.average" $vms = Get-VM VM1, VM2 $start = (Get-Date).AddDays(-7) $foldername = get-date -Format "ddMMyyyy" New-Item -ItemType directory -Path "C:\Perf\$foldername" $hashtable = get-stat -Entity $vms -Stat $metrics -Start $start | Select -ExcludeProperty Timestamp Entity, MetricID, Unit, Value,@{N="TimeStamp";E={$_.Timestamp.ToString("MMM dd yyyy HH:mm:ss")}} | group Entity, MetricID -AsHashTable -AsString $vms | ForEach-Object { $vm = $_.Name $i = 0 $metrics | ForEach-Object { $path = "C:\Perf\$foldername\" + $vm + '_' + $metrics[$i] + '.csv' $metric = $vm + ', ' + $metrics[$i] $hashtable.$metric | sort-object TimeStamp | export-csv $path -NoTypeInformation -UseCulture $i++ } }
NOTE: I’ve only tested this in vSphere 6.0, although in multiple environments.
Task Scheduler
I used the command powershell.exe -file "C:\Perf\Perf.ps1"
in Task Scheduler as below.
Any comments or suggestions welcome.
Acknowledgements
- LucD for the original idea and your help on the subject (topic).
- Jeffery Hicks for an awesome Powershell blog.