VMware Cloud Community
rogermccarrick
Contributor
Contributor

Powercli - get configured memory on a resource pool.

In vSphere, version 5, when you click on a resource pool and hit the summary tab, in the Memory box under resource settings you can see something like:

Reservation: 0.00MB

Limit: 48:00GB

Configured 29:00GB.

I want to be able to pull out that number for "Configured" with powercli. Then I can see what pools are over or under allocated.

I believe it can be found in a property called summary.configuredMemoryMB from ResourcePoolSummary, but I have no idea how to use either of these objects. I have tried.

I am able to get the Limit, which is MemLimitGB. Here is my script:

**********************************************************************************

$report = @()
$Rpool = Get-ResourcePool  | select Name, ParentID, MemLimitGB, Parent
foreach ($pool in $Rpool)
          {
       
   $row = "" | Select Name, ParentID, MemLimitGB, Parent, "VC"
   $row.Name = $pool.Name
   $row.ParentID = $pool.ParentID
   $row.MemLimitGB = [int]$pool.MemLimitGB
   $row.Parent = $pool.Parent
   $row."VC" = $vc
  
    $report += $row
}
$report | Export-Csv

I would like:

$report = @()
$Rpool = Get-ResourcePool  | select Name, ParentID, MemLimitGB, Parent, "CONFIGMEM"
foreach ($pool in $Rpool)
          {
       
   $row = "" | Select Name, ParentID, MemLimitGB, Parent, "VC"
   $row.Name = $pool.Name
   $row.ParentID = $pool.ParentID
   $row.MemLimitGB = [int]$pool.MemLimitGB
   $row.Parent = $pool.Parent
   $row."VC" = $vc
   $row."CONFIGMEM" = $pool.summary.configuredMemoryMB   <<<<<<- Something like this. THIS IS WHERE I AM STUCK
  
    $report += $row
}
$report | Export-Csv
#############################################
Any help would be appreciated.
thanks.

0 Kudos
10 Replies
LucD
Leadership
Leadership

Try it like this

$report = @()

foreach ($pool in Get-ResourcePool) {
   $row = "" | Select Name, ParentID, MemLimitGB, Parent, "VC", "CONFIGMEM"
   $row.Name = $pool.Name
  
$row.ParentID = $pool.ParentID
   $row.MemLimitGB = [int]$pool.MemLimitGB
   $row.Parent = $pool.Parent
   $row."VC" = $vc
  
$row."CONFIGMEM" = $pool.ExtensionData.summary.configuredMemoryMB
   $report += $row
}
$report | Export-Csv

To reach the vSphere ResourcePool object, you go through the ExtensionData property.


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

0 Kudos
rogermccarrick
Contributor
Contributor

LucD thanks for the reply.

This doesn't work. It doesn't give any error, it just gives a blank value.

0 Kudos
LucD
Leadership
Leadership

The CONFIGMEM property was missing from the Select, or did you already fix that ?

Just tried it in my test environment, and I get the correct value.


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

0 Kudos
rogermccarrick
Contributor
Contributor

LucD, thatnks very much, this works.

I didn't see that you had made some changes at the beginning of the script.

I just worked it out another way. Inside the foreach loop, at the top, I added the line:

     $vm = Get-ResourcePool -name $pool.name | Get-vm

then further down I added:

     $row."CONFIGMEM" = "{0:f1}" -f [int](($vm | Measure-Object -Property MemoryGB -Sum).Sum)

that worked. But I'm going to stick with your method and I changed it slightly to:

     $row."CONFIGMEM" = [int]($pool.ExtensionData.summary.configuredMemoryMB/1024)

So again thanks. This has resolved my question.

Roger

0 Kudos
JohnMcKeown
Enthusiast
Enthusiast

Hi LucD and Roger,

LucD very efficient, I was looking for something almost exactly similar.

A quick question, as I am not brilliant with PowerShell, is it possible to Nest the foreach function.

The report outputs, for each cluster, report Resource pool.

Would it be possible to do something more detailed like, for each cluster report resource pool, then for each resource pool report VM useage?

0 Kudos
LucD
Leadership
Leadership

Yes, that is possible.

But there a couple of things you should keep in mind:

  • how would your output look ? Do you envisage 1 CSV file with 1 type of row in there that holds all the data ? The alternative is to create multiple CSV files, but then you just as well have used 3 seperate scripts
  • depending on the size of your environment, a construction with multiple nested foreach loops could take forever to complete. Depending on the total size you could end up with a script that runs forever, and might exhaust the available memory before it is finished.


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

0 Kudos
JohnMcKeown
Enthusiast
Enthusiast

Wow that was a quick reply, I was worried that this thread was a bit old.

I had my own basic Get-VM line:

Get-VM | Select Name,MemoryGB,@{n="Mem Resource Limit";E={Get-VMResourceConfiguration -VM $_ | Select -ExpandProperty MemLimitGB}},@{n="Mem Reservation";E={Get-VMResourceConfiguration -VM $_ | Select -ExpandProperty MemReservationGB}},NumCpu,@{n="CPU Limit";E={Get-VMResourceConfiguration -VM $_ | Select -ExpandProperty CpuLimitMhz}}| Export-csv c:\temp\virtualmachinesettings.csv

But liked the idea of scripting it the way you did.

Probably I could get away with

Having a report with For each pool and then list the VMs. Personally I have a need for what cluster they are on.

But even the line I use above is a tad intensive as I am pulling so many values.     

0 Kudos
rogermccarrick
Contributor
Contributor

I used separate scripts.

I run a getCluster script and then a getResourcePool script.

My scripts run through 7 VCs, the list of VCs being held in a file, vc.txt.

Each script writes to it's own csv file.

The scripts, amoung other things, get the the ID of the main resource pools and the parent ID for each resource pool.

In your getVM script you can extract the ID of the pool the VM is in.

These IDs give you a link between the csv files.

I upload the files to a linux machine, run shell scripts use grep, sed and awk etc, where I cross reference the csv files using pool and parent IDs, and then output to .html files that I display on Apache. These web pages summarize all the clusters and show how much memory they have what's in use and what's left, and also show a list of all resource pools, with memory limit and memory configured.

This allows me to see in a glance if clusters are running out of resources. It also allows me to see if a pool is configured for more memory than allocated, or perhaps using a lot less memory than allocated. Either one indicates and problem.

We have a dynamic environment so these scripts run 4 times a day.

Roger

0 Kudos
JohnMcKeown
Enthusiast
Enthusiast

I was wondering more about the foreach function though.

And how to nest commandlets, there are other cases where I would like to get information about a device and highlight its parent.

For example the Get-NetworkAdapter, I cant manage to get the name of the vm its connected to.   

0 Kudos
rogermccarrick
Contributor
Contributor

For Get-networkadapter, not following what you want to do.

    Get-networkadapter -vm NAME-OF-VM | select networkname, parent

will give you the portgroup name and the vm name. But the command asks that you specify a VM, Template or Snapshot. How are you trying to use it?

Here's one way to do it:

# In a text file list the names of your VCenters
# separated by carriage return

foreach($vc in Get-content C:\Scripts\vc.txt){
#========================================================================#

Connect-VIServer $vc
 
$clusterName = "*"
foreach ($cluster in get-cluster -Name $clusterName){
Get-VM `
| Select `
@{n="VCenter";E={$vc}},`
@{n="Cluster";E={$cluster}},`
ResourcePool,`
Name,`
MemoryGB,`
@{n="MemResourceLimit";E={Get-VMResourceConfiguration -VM $_ | Select -ExpandProperty MemLimitGB}},`
@{n="MemReservation";E={Get-VMResourceConfiguration -VM $_ | Select -ExpandProperty MemReservationGB}},`
NumCpu,`
@{n="CPULimit";E={Get-VMResourceConfiguration -VM $_ | Select -ExpandProperty CpuLimitMhz}},`
@{n="PortGroup";E={Get-NetworkAdapter -VM $_ | Select -ExpandProperty NetworkName}}`
| Export-Csv ("C:\Scripts\cluster-" + $vc + ".csv") -NoTypeInformation -UseCulture

# Above, Export to a file, name it on the fly using the VC name
# There will be a file per VC.


Disconnect-VIServer -Server * -Force -confirm:$false

}
}

This will give you a csv filer per vcenter. The csv file will have VC, Cluster, Pool, memory etc for each VM.

It gives you the port group to which the VM is connected.

Although this is the reverse of what you were asking, which is what VM is connected to the port group?

And just a note for your csv file, a VM maybe connected to more than one port group so this will give a number of entries in that field spearated by a space.

Your original question was to get "for each cluster, report resource pool, then for each resource pool report VM usage'

So for each cluster, report what about each resource pool? Just it's name? Or a group of other stats? If you need stats from the pool, then you'd probably be best with separate scripts. If you just want VM stats and want to know what cluster and resource pool the VM belongs to, then I think this will do it.

0 Kudos