Get Virtual Machines Performance Statistics with Powershell Part 2

in previous post, we had the script up and running, I am going to give each part its own description, accordingly we’ll have a better understanding of Powershell logic.

as already been told, when tackling with Powershell, there are times your desired data resides in different cmdlets and you don’t want to do excel engineering to combine the outputs. Therefore, there should be a way to mix the result right in Powershell, with that in mind, you need to get to know the concept of an empty array. This is where to start and make a file with customized fields.

this variable is an empty array, which we’ll fill with our report

$metrics is the variable of our desired metrics.

$vms is the variable containing your desired virtual machines. I have opted for powered-on VMs.

$start variable is used to determine the time range for the performance data to collect, which here we have it for the last 30 days. for more details on Get-Date cmdlet, visit Microsoft website here.

This is the variable for performance collection interval in minutes.

As we are going to create a table and add entries from different cmdlets, we need to do it in a foreach loop. for more details on foreach loop, see my blogpost here.

$InpObj = $null

$InpObj = New-Object -TypeName PSCustomObject

new-object is a cmdlet which gives you a new powershell object, therefore you can give it members with your desired properties. here we add what we need our table to be like. The properties we give our object is retrieved from the previous outputs.

$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

for the rest of you report, we need some performance statistics with get-stat cmdlet and these data to our table.

$stat = get-stat -Entity $vm.Name -Start $start -stat $metrics -IntervalMins $intervalmins

we’ll get the result for our metrics defined in $metrics, which is a long list of numbers for every hour in the last 30 days, which is not that applicable if we need the data for sizing purposes. Therefore, we need a way to give us the average values in the time period.

$CPUstat = $stat | ? {$_.MetricId -eq 'cpu.usagemhz.average'}

here we get the value specifically for CPU, then in next step with measure command, we’ll get average, maximum, and minimum values. The same thing happens for Memory and Disk statistics as shown below.

now, we have another set of data which is required to be added to our report. again with add-member cmdlet, we populate other fields to our report.

Last thing to do is to populate our report with every entry received and the close the loop.

then you can export the result to an excel sheet with export-csv cmdlet.

I hope this has been informative for you.

Leave a Reply

Your email address will not be published. Required fields are marked *