VMware Cloud Community
BenBrazil
Enthusiast
Enthusiast
Jump to solution

Script to count total amount of Memory allocated to VM's on each ESX host

Hi,

I am looking for a script to add up all of the RAM allocated to VM's per ESX host in vCenter.

I can quite easily display the MemoryUsageMB and the MemoryTotalMB but I'd like to get the total amount of memory assigned to VM's on a host by host basis?

Any help would be appreciated.

Thanks,

Ben

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

Get-VMhost |
Select Name,@{N="Memory used MB";E={
 
$_ | Get-VM | %{$_.ExtensionData.Summary.QuickStats.HostMemoryUsage} |
 
Measure-Object -Sum | Select -ExpandProperty Sum
}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

9 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

Get-VMhost |
Select Name,@{N="Memory used MB";E={
 
$_ | Get-VM | %{$_.ExtensionData.Summary.QuickStats.HostMemoryUsage} |
 
Measure-Object -Sum | Select -ExpandProperty Sum
}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

BenBrazil
Enthusiast
Enthusiast
Jump to solution

Many thanks,

I substituted $_.ExtensionData.Summary.QuickStats.HostMemoryUsage

for

$_MemoryMB

to get what I was after.

Thanks also for your post on VIProperties - http://www.lucd.info/viproperties/

Very useful.

Reply
0 Kudos
andrewsardinha
Enthusiast
Enthusiast
Jump to solution

I just ran LucD's script and then sub'd in your modification and the two scripts produce two different results.

Your modification:

PowerCLI C:\> Get-VMhost | Select Name,@{N="Memory used MB";E={$_ | Get-VM | %{$_.MemoryMB} | Measure-Object -Sum | Select -ExpandProperty Sum}}

Name                                                                                                                                   Memory used MB
----                                                                                                                                   --------------
10.2.5.86                                                                                                                                       75776
10.2.5.87                                                                                                                                      106496
10.2.5.85                                                                                                                                       88064

LucD's script:

PowerCLI C:\> Get-VMhost | Select Name,@{N="Memory used MB";E={ $_ | Get-VM | %{$_.ExtensionData.Summary.QuickStats.HostMemoryUsage} |  Measure-Object -Sum | Select -ExpandProperty Sum}}

Name                                                                                                                                   Memory used MB
----                                                                                                                                   --------------
10.2.5.86                                                                                                                                       63415
10.2.5.87                                                                                                                                       87734
10.2.5.85                                                                                                                                       81205

I ran the two variations back to back. LucD's takes a bit longer to run than yours. When I look at the host's summary page the memory utilization is more consistent with LucD's version. I'm not quite sure why there is a difference or where it is coming from, I just thought I'd throw my two cents in.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Like you noticed these are 2 different properties we are measuring.

The MemoryMB value is the amount of memory you assigned when the VM was created.

The HostMemoryUsage property is, and I quote, "Host memory utilization statistics, in MB. This is also known as consumed host memory. This is between 0 and the configured resource limit."

These are the 2 properties in the vSphere client

memory.png


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
BenBrazil
Enthusiast
Enthusiast
Jump to solution

Hi,

I've added MemoryTotalMB to get the Host Memory Total. I'd like to output the figure in GB and not MB.

Get-VMhost |
Select Name,MemoryTotalMB,@{N="Memory used MB";E={
 
$_ | Get-VM | %{$_.MemoryMB} |
 
Measure-Object -Sum | Select -ExpandProperty Sum
}}

I've had a look around but cant see how this can be done.

Thanks again,

Ben

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try it like this

Get-VMhost |
Select Name,
   
@{N="MemoryTotalGB";E={[math]::Round($_.MemoryTotalMB/1KB,1)}},
   
@{N="Memory used GB";E={
  [
Math]::Round(($_ | Get-VM | %{$_.MemoryMB} |
 
Measure-Object -Sum | Select -ExpandProperty Sum)/1KB,1)
}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
andrewsardinha
Enthusiast
Enthusiast
Jump to solution

About the only way I know how to do it is listed below. You can of course break it out from the single line and you can also leave out the "-as [int]". That just tells PS to format it as an integer. Normally if you weren't working with an already formatted number (the out put of MemoryMB is already formatted for the usual (MB * 1024) I'd assume) you might get a long number with many digits after the decimal. If that were the case you could clean up the output to just a whole number by using the "-as [int]".

You'll notice that I changed the property text to reflect GB rather than MB. The two scripts are the same but mine is in a single line and I reworked yours to keep your formatting.

Get-VMhost | Select Name,@{N="Memory used GB";E={$_ | Get-VM | %{($_.MemoryMB / 1KB) -as [int]} | Measure-Object -Sum | Select -ExpandProperty Sum}}

Get-VMhost |
Select Name,MemoryTotalMB,@{N="Memory used GB";E={
 
$_ | Get-VM | %($_.MemoryMB / 1KB) -as [int]} |
 
Measure-Object -Sum | Select -ExpandProperty Sum
}}

Reply
0 Kudos
andrewsardinha
Enthusiast
Enthusiast
Jump to solution

Ah, you beat me to it! I'm not as advanced of a scripter as you so you're technique is probably better than mine.

Reply
0 Kudos
BenBrazil
Enthusiast
Enthusiast
Jump to solution

Luc and Andrew,

thank you for your help.

Ben

Reply
0 Kudos