Revisiting VMware PowerShell Find VMs with Memory Reservations
Revisiting VMware PowerShell Find VMs with Memory Reservations
Back in March 2021 I wrote a quick one line PowerShell snippet to find memory reservations for a friend that wanted a simple and quick way to return objects that had reservations. I extended the one liner to also return VM objects that have either CPU or memory reservations.
get-VM | Where-Object {$_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation -ne "0" -or $_.ExtensionData.ResourceConfig.cpuAllocation.Reservation -ne "0"} | Export-csv -path c:\ps\test.csv
This one liner was great and there is an example output below.
Good questions come around mulitple times, and I had the same query come in from someone wanting to understand the reservation footprint across clusters. When I revisited the code I relalised the MemoryGB figure is the configured RAM not a refelction of the reserved RAM.
In order to get that I built the script below.
$vm_mem_r = get-VM | Where-Object {$_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation -ne "0" -or $_.ExtensionData.ResourceConfig.cpuAllocation.Reservation -ne "0"} foreach($vm in $vm_mem_r){ $vm | select name,@{Name="MemReservation";Expression={($_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation)}},MemoryGB,@{Name="CPUReservation";Expression={($_.ExtensionData.ResourceConfig.cpuAllocation.Reservation)}},@{Name="CPUs";Expression={($_.ExtensionData.Config.Hardware.NumCPU)}},@{Name="Cores/Socket";Expression={($_.ExtensionData.config.hardware.NumCoresPerSocket)}} }
This uses the one liner above in a variable, and then for each returned vm object from that one liner return the name, reserved RAM, RAM, Reserved CPU, sockets and cores.
With output as per the above.
Hopefully this is useful for someone!
Simon