When sizing a VMware environment, let’s say, for optimization purposes or re-architecting the existing environment. You’ll need a baseline of current situation, meaning, in terms of capacity and performance, what considerations have to be observed. When it comes to virtual machines, you can get individual VMs performance charts in vCenter GUI, but if you want all VMs statistics in a certain period of time, that’s where PowerCLI comes in.
In this post series, I am going to get VMs performance and capacity data in the last day and export it as a report. I will try to explain each part, so you can have a better understanding of Powershell logic.
Performance and Capacity info
I need two categories of information, one comes in virtual machine configuration, items like, Allocated memory, Disk Capacity, Number of vCPUs which can be found in Get-VM cmdlet, and the other comes in virtual machine performance statistics, which yo will have to look for in Get-Stat cmdlet. Therefore, here the challenge is combining these two outputs together as one report in a single table.
The following script gets virtual machines’ statistics for metrics, CPU, Memory, Disk, Disk Usage.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#Variables $report = @() $metrics = "cpu.usagemhz.average","mem.consumed.average","disk.usage.average" $vms = Get-Vm | where {$_.PowerState -eq "PoweredOn"} $start = (get-date).AddDays(-30) $intervalmins = 60 ####################### foreach ($vm in $vms){ $InpObj = $null $InpObj = New-Object -TypeName PSCustomObject $InpObj | Add-Member -MemberType NoteProperty -Name "VMName" -Value $vm.Name $InpObj | Add-Member -MemberType NoteProperty -Name "NumCPU" -Value $vm.NumCpu $InpObj | Add-Member -MemberType NoteProperty -Name "MemoryGB" -Value $vm.MemoryGB $InpObj | Add-Member -MemberType NoteProperty -Name "UsedSpaceGB" -Value $vm.UsedSpaceGB $InpObj | Add-Member -MemberType NoteProperty -Name "ProvisionedSpaceGB" -Value $vm.ProvisionedSpaceGB $stat = get-stat -Entity $vm.Name -Start $start -stat $metrics -IntervalMins $intervalmins $CPUstat = $stat | ? {$_.MetricId -eq 'cpu.usagemhz.average'} $CPUMeasure = $CPUstat.value |measure -Average -Maximum -Minimum $MemoryStat = $stat | ? {$_.MetricId -eq 'mem.consumed.average'} $MemoryMeasure = $MemoryStat.value |measure -Average -Maximum -Minimum $DiskStat = $stat | ? {$_.MetricId -eq 'disk.usage.average'} $DiskStatMeasure = $DiskStat.value -eq |measure -Average -Maximum -Minimum #CPU Statsics $InpObj | Add-Member -MemberType NoteProperty -Name "MinCPUGhz" -Value $CPUMeasure.Minimum/1000 $InpObj | Add-Member -MemberType NoteProperty -Name "AvgCPUGhz" -Value $CPUMeasure.Average/1000 $InpObj | Add-Member -MemberType NoteProperty -Name "MaxCPUGhz" -Value $CPUMeasure.Maximum/1000 #Memory Statistics $InpObj | Add-Member -MemberType NoteProperty -Name "MinMemoryGB" -Value $MemoryMeasure.Minimum $InpObj | Add-Member -MemberType NoteProperty -Name "AvgMemoryGB" -Value $MemoryMeasure.Average $InpObj | Add-Member -MemberType NoteProperty -Name "MaxMemoryGB" -Value $MemoryMeasure.Maximum #Disk Statistics $InpObj | Add-Member -MemberType NoteProperty -Name "DiskIOMinKBps" -Value $DiskStatMeasure.Minimum $InpObj | Add-Member -MemberType NoteProperty -Name "DiskIOMbKBps" -Value $DiskStatMeasure.Minimum $InpObj | Add-Member -MemberType NoteProperty -Name "DiskIOMbKBps" -Value $DiskStatMeasure.Minimum $report += $InpObj } $report | Export-Csv -Path C:\Amir\vConceptions\stats.csv -NoTypeInformation -UseCulture |
in the next blogpost, I will get into details of the script, explaining all the parts.