VMware Cloud Community
jswager1
Contributor
Contributor
Jump to solution

CLI command (or script) to determine if a resource pool has enough resources for a VM

Is there PowerCLI script - or a series of commands - that can be used to determine if a VMs resource pool has enough available resources to start the VM WITHOUT triggering a vSphere error?

For performance testing reasons, we use a resource pool with maximums for RAM and CPU.  Each VM in the pool has a reservation set for CPU and RAM.  Testing automation will attempt to start as many VMs as possible in the pool when testing.  The CLI will return an error when starting the VM will exceed the amount of allowed resources.  When that happens, an error is shown on the vSphere console "Insufficient resources".  Instead of constantly starting the VM, failing, and generating the error - is there a way to check to see if there is enough space?

Thanks,

Jason

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Jason,

you can try if the following PowerCLI function is what you need. The function does not look at expandable reservations.

function Get-VmStartPossible {
  param($VM)

  $VM = Get-VM $VM
  $ResourcePool = $VM | Get-ResourcePool

  $CpuReservationUsed = $ResourcePool.ExtensionData.Runtime.Cpu.ReservationUsed
  $VMCpuReservation = $VM.ExtensionData.ResourceConfig.CpuAllocation.Reservation
  $ResourcePoolCpuLimit = $ResourcePool.ExtensionData.Config.CpuAllocation.Limit
    
  $MemoryReservationUsed = $ResourcePool.ExtensionData.Runtime.Memory.ReservationUsed
  $VMMemoryReservation = $VM.ExtensionData.ResourceConfig.MemoryAllocation.Reservation
  $ResourcePoolMemoryLimit = $ResourcePool.ExtensionData.Config.MemoryAllocation.Limit*1MB
    
  if (($CpuReservationUsed + $VMCpuReservation -gt $ResourcePoolCpuLimit) -or ($MemoryReservationUsed + $VMMemoryReservation -gt $ResourcePoolMemoryLimit))
  {
    $false
  }
  else
  {
    $true
  }
}

You can call the function with:

Get-VmStartPossible -VM MyVM

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
2 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Jason,

you can try if the following PowerCLI function is what you need. The function does not look at expandable reservations.

function Get-VmStartPossible {
  param($VM)

  $VM = Get-VM $VM
  $ResourcePool = $VM | Get-ResourcePool

  $CpuReservationUsed = $ResourcePool.ExtensionData.Runtime.Cpu.ReservationUsed
  $VMCpuReservation = $VM.ExtensionData.ResourceConfig.CpuAllocation.Reservation
  $ResourcePoolCpuLimit = $ResourcePool.ExtensionData.Config.CpuAllocation.Limit
    
  $MemoryReservationUsed = $ResourcePool.ExtensionData.Runtime.Memory.ReservationUsed
  $VMMemoryReservation = $VM.ExtensionData.ResourceConfig.MemoryAllocation.Reservation
  $ResourcePoolMemoryLimit = $ResourcePool.ExtensionData.Config.MemoryAllocation.Limit*1MB
    
  if (($CpuReservationUsed + $VMCpuReservation -gt $ResourcePoolCpuLimit) -or ($MemoryReservationUsed + $VMMemoryReservation -gt $ResourcePoolMemoryLimit))
  {
    $false
  }
  else
  {
    $true
  }
}

You can call the function with:

Get-VmStartPossible -VM MyVM

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
jswager1
Contributor
Contributor
Jump to solution

Thanks!  This looks like what I need.  I'll give it a try.

Reply
0 Kudos