VMware Cloud Community
andybgrant
Enthusiast
Enthusiast
Jump to solution

Script to export System Resource Allocation reservations to CSV

LucD - your book is fabulous, but still a little over my head Smiley Happy

I am looking for a way to get a CSV file of _all_ the CPU/Mem reservations at the host level as found under the Configuration/System Resource Allocation

resourceallocation.jpg

Can this be achieved easily?  A list of all the items listed even if the reservation is 0MB

From this post (http://communities.vmware.com/thread/174234) we can set them, but I'm just looking for a list.

Thanks !

Andy

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try it like

function Get-VMHostSystemResource{
   
param($node)
   
   
if($node.Child){
       
$node.Child | %{
           
Get-VMHostSystemResource $_
        }
    }
   
New-Object PSObject -Property @{
        Key
= $node.Key
        CPUReservation
= $node.Config.CpuAllocation.Reservation
        CPULimit
= $node.Config.CpuAllocation.Limit
        CPUShares
= $node.Config.CpuAllocation.Shares.Shares
        CPUExpandable
= $node.Config.CpuAllocation.ExpandableReservation
        MemReservation
= $node.Config.MemoryAllocation.Reservation
        MemLimit
= $node.Config.MemoryAllocation.Limit
        MemShares
= $node.Config.MemoryAllocation.Shares.Shares
        MemExpandable
= $node.Config.MemoryAllocation.ExpandableReservation
    }
}

$esx = Get-VMHost MyESX
$report
= Get-VMHostSystemResource $esx.ExtensionData.SystemResources
$report | Export-Csv "C:\SysResource.csv" -NoTypeInformation -UseCulture

The script uses a function that is called recursively for each node.

Note that the New-Object to create each entry does not keep the order of the properties as they are defined in the hash table.

If that is a requirement let me know and I can give you some solutions.

Thanks for the book reference 🙂

The idea was that the reader should be able to use the book while he progresses his PowerCLI knowledge.

And that it would be available as a Reference when you have a task to perform in your vSphere environment.


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

View solution in original post

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

Try it like

function Get-VMHostSystemResource{
   
param($node)
   
   
if($node.Child){
       
$node.Child | %{
           
Get-VMHostSystemResource $_
        }
    }
   
New-Object PSObject -Property @{
        Key
= $node.Key
        CPUReservation
= $node.Config.CpuAllocation.Reservation
        CPULimit
= $node.Config.CpuAllocation.Limit
        CPUShares
= $node.Config.CpuAllocation.Shares.Shares
        CPUExpandable
= $node.Config.CpuAllocation.ExpandableReservation
        MemReservation
= $node.Config.MemoryAllocation.Reservation
        MemLimit
= $node.Config.MemoryAllocation.Limit
        MemShares
= $node.Config.MemoryAllocation.Shares.Shares
        MemExpandable
= $node.Config.MemoryAllocation.ExpandableReservation
    }
}

$esx = Get-VMHost MyESX
$report
= Get-VMHostSystemResource $esx.ExtensionData.SystemResources
$report | Export-Csv "C:\SysResource.csv" -NoTypeInformation -UseCulture

The script uses a function that is called recursively for each node.

Note that the New-Object to create each entry does not keep the order of the properties as they are defined in the hash table.

If that is a requirement let me know and I can give you some solutions.

Thanks for the book reference 🙂

The idea was that the reader should be able to use the book while he progresses his PowerCLI knowledge.

And that it would be available as a Reference when you have a task to perform in your vSphere environment.


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

Reply
0 Kudos
andybgrant
Enthusiast
Enthusiast
Jump to solution

LucD - you da'man!  Worked superbly.

I absolutely agree, that's why I got the book, I just seem to always need to write something 3 steps ahead of my ability and two days behind my timeframe

Smiley Happy

Andy

Reply
0 Kudos
andybgrant
Enthusiast
Enthusiast
Jump to solution

LucD - admittedly, all I can do is fiddle Smiley Happy  But here's what I've added.

Since I only wanted a sum total of the host System Resource Allocation reservations I commented out the unneeded items from the loop leaving only a single entry (CPU or memory reservation).

The output to CSV or out-gridview provided a two collumn list of all the reservations.  My goal was to get a single numerical output of the total reservation. The only problem with the output is that the total "free" RAM available for VM usage is also considered a reservation.  So here's what I added.

$ReportSum = $report | Measure-Object -Sum MemReservation $Total = $ReportSum.sum - $esx.ExtensionData.SystemResources.Config.MemoryAllocation.Reservation $Total

Thanks again LucD!

Andy

Reply
0 Kudos