Get-VMStat and Resource Allocation

We have a problem that a lot of VI administrators (especially us young ones) run into – VM Sprawl.

In attempting to reduce the chaos and get a much better understanding of our virtual infrastructure I’ve run into a very helpful cmdlet provided in the VI Toolkit.

This tutorial is intended for the complete novice.

Step 1:

Get your system set up for VI Toolkit.

Download PowerShell.
Download the VI Toolkit.

Step 2:

Preparing your PowerShell environment.

Go to Start -> Run and type powershell. Press enter.

echo $profile
# C:\Documents and Settings\ME\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

See Windows PowerShell Profiles to set up a profile.


new-item -path $profile -itemtype file -force

If you’re familiar with bash, think of this as a /~/.bash_profile. Any “aliases” or cmdlets you place in here can be accessed throughout your powershell session.

A notepad window should open. If it didn’t, run the following command:

notepad $profile

Now, paste the Get-VMStat cmdlet in your profile.

function Get-VMStat
param( $VM,[Int32]$Hours = 1 )
 $VM | Sort Name |
 Select Name, @{N="CPU";E={[Math]::Round((($_ |
 Get-Stat -Stat cpu.usage.average -Start (Get-Date).AddHours(-$Hours) -IntervalMins 5 -MaxSamples ($Hours*12) |
 Measure-Object Value -Average).Average),2)}}, @{N="MEM";E={[Math]::Round((($_ |
 Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-$Hours) -IntervalMins 5 -MaxSamples ($Hours*12) |
 Measure-Object Value -Average).Average),2)}}

Save your profile.

Step 3: Use the cmdlet.

Launch the VI-Toolkit (Start > Programs > VMware > VMware VI Toolkit)

Then, run the following:

Get-VC # you will be asked for your VC server
Get-VMStat (Get-VM) -Hours 2160

2160 hours gives the average CPU and RAM for all of my VMs for the last 90 days. This is giving us a wonder baseline of information to make some resource allocation decisions.


One Comment

  1. Hi,

    The scripts works great !

    I was wondering though, was it possible to get MAX and MIN CPU used over a given period and the same for MAX and MIN Memory used. You can get these stats from the VI client, but i was wondering is powershell can get these stats ?


Comments are closed.