Using PowerCLI to assist in right-sizing Memory for VMs

Lately I have been trying to determine which of my VMs have oversized, or allocated more resources than they need. I know that there are programs, plugins etc which do this, however I wanted to take a shot a just a quick script that could give some direction. Although if anyone has a program/appliance they like(maybe free) for right-sizing VMs let me know.

So without further adieu here is my quick little script.


Param($days, $ref)

$date=get-date -hour 0 -minute 0 -second 0
$vms= get-vm | Where-Object {$_.powerstate -like "*on"} | sort-object

$vms | Where-object {((Get-Stat -Entity (Get-VM $_) -Stat mem.usage.average -Start $date.AddDays(-"$days")`
 -Finish $date.AddDays(-1) | where-object {$_.Instance -eq ""}).value|measure-object -average).average -lt $ref}

The script takes all of the VMs that are powered on and sorts them.
From here it looks at the average usage(a percent) of a VMs memory over the last $days. $days being a number. It then compares that average with the reference number provided($ref), if the overall average over $days is less than $ref, the VM object is returned.

An Example:

./averagemem.ps1 -days 20 -ref 25

Assuming you named the script “averagemem.ps1” like I did, the script will return all the VMs who in the last 20 days have had an average memory usage of under 25%. Pretty neat stuff!

This is in no way an end all be all of whether a VM is right-sized on memory or not, but if you have a large environment it can definitely narrow down how many VMs you should be examining.

I hope this Help!

Reference: http://www.lucd.info/2009/12/30/powercli-vsphere-statistics-part-1-the-basics/

7 Responses to Using PowerCLI to assist in right-sizing Memory for VMs

    • True Story! I hate when someone comes in and says “Hey this VM need 4 CPUs added to it!”…. To which I reply, “Say What Now?!”

  1. You could save some time by removing “Get-VM” from the Get-Stat statement:

    Get-Stat -Entity $_ …..

    Should work just as fine.

  2. How would you change this to display the percentage of average memory used as well?

    So….instead of just showing which VMs fall under the reference average, you could display the percentage too.

    Jim

    • Try this..I took out all the variables. I know it isn’t exactly what you are looking for, but hopefully it will help

      ((Get-Stat -Entity (Get-VM “VM Name“) -Stat mem.usage.average -Start (get-date).AddDays(-“7”) -Finish (get-date).AddDays(-1) | where-object {$_.Instance -eq “”}).value|measure-object -average).average

Leave a Reply