VI Toolkit Script adds RAM

The virtual infrastructure client is a great source of information about your virtual infrastructure. Too bad you can’t copy and paste information to excel to do some calculations. I’d like to add the amount of RAM of each vm in a cluster, in order to compare it to the total amount of RAM the ESX hosts have installed. Sure, you could directly query the SQL server. But I like to use PowerShell. Luckily, the VI Toolkit (beta) comes to the rescue!


First, let’s store some administrative credentials in a variable:


$credentials = Get-Credential $user


Next, we connect to the Virtual Center server:

$vcserver = “VCSERVER”

Get-VIServer $vcserver -Credential $credentials -ErrorAction Stop


Now we can grab the virtual machines in one of our clusters:

$cluster = “CLUSTER”

$vms = Get-VM -Location (Get-Cluster -name $cluster)


Let’s add up the values of the MemoryMB properties of the vm’s:

$TotalMemoryMB = $vms | Measure-Object -Property MemoryMB -Sum


I’m betting that will be a pretty large number. Why not transform the value to gigabytes and round the value at two digits:

$TotalMemoryGB = [math]::Round($TotalMemoryMB.Sum/1024,2)


Here we have the value we wanted, ready to be echoed back to the person running the script; like so:

Write-Host “Total amount of RAM used in the cluster $cluster: $TotalMemoryGB GB”


Happy scripting!

Leave a Reply