I recently was looking for a way to improve one of the reports I do for my forecasting. I used to dump everything from Excel in list format and calculate up the guest RAM. Too many guest and too much time. This script saves time and I can see what my actual allocated RAM is on an ESX host. So I had asked Eric to write something up. Here is what he came up with and how it works. This is also posted on VMwarescripting.com. Keep in mind that this assumes you are using a script editor and have already connected to the vCenter that you want to run against. As well you should run against a test environment as well to validate that it works as you want. There are some additional documents on basics of PowerShell in the shared document folder.
Original code
foreach ($vmhost in get-cluster “cluster name” | get-vmhost){
$meminfo = $vmhost | get-vm | measure-object -property memorymb -sum
$guestcount = $vmhost | get-vm | measure-object
"" | select-object @{Name="Host"; Expression={$vmhost.name}},
@{Name="Num VM's"; Expression={$guestcount.count}},
@{Name="Mem Allocation"; Expression={$meminfo.Sum}}
}
Code with comments defined with #
#connect to specified cluster and get all of the vmhost info
foreach ($vmhost in get-cluster “cluster name” | get-vmhost)
{
#define variable for function that calculates amount of allocated memory
$meminfo = $vmhost | get-vm | measure-object -property memorymb –sum
#define variable for function that calculates number of virtual machines
$guestcount = $vmhost | get-vm | measure-object
#build output expression column titled “Host” and populate it
"" | select-object @{Name="Host"; Expression={$vmhost.name}},
#build output expression column titled “Num VM’s” and populate it
@{Name="Num VM's"; Expression={$guestcount.count}},
#build output expression column titled “Mem Allocation” and populate it
@{Name="Mem Allocation"; Expression={$meminfo.Sum}}
}
Your output should look something like this:
|
Name
|
MemoryTotalMB
|
nVMs
|
VM_Mem
|
|
|
|
Server1
|
65530
|
26
|
39424
|
|
|
|
Server2
|
65530
|
15
|
27136
|
|
|
Keep in mind there are a lot of other things you can do in PowerShell so if there is something that you can’t find check out VMwarescripting.com and make a post or ask at a VMUG meeting. Don't forget Ecoshell is a great way to get started with scripting as well. Under the blog section of http://www.thevesi.org/index.jspa there is a Webinar to help you get started. Also check out http://blogs.vmware.com/vipowershell/