When configuring your virtual machines sometimes you want to set reservations. Like I said in the article about adding memory:  You can go into the vSphere Client to change the configuration, but where is the fun in that?  Why not do everything from the command line? And preferably by the use of PowerCLI?

The same goes for setting reservations. Why don’t we do this just from the command line since it is possible?

Ok, here we go!

Setting reservations with PowerCLI

Memory Reservations

To reduce the necessary space on disk, for example for VDI, you can set memory reservations. When a virtual machine is powered on a  a swapfile (.vswp) is created. This swapfile is the as large as the difference between the amount of memory a VM has and the reservation that this machine has. When a VM has 4GB and no reservation is created the .vswp is 4GB in size. When you set a reservation of 2GB the .vswp is 2GB in size.

To set the reservation to 4GB:

Get-VM -Name MyVM | Get-VMResourceConfiguration |Set-VMResourceConfiguration -MemoryReservation 4

If you want to select and lock all guest memory you have to do a little bit more.

$guestConfig = New-Object VMware.Vim.VirtualMachineConfigSpec
$guestConfig.memoryReservationLockedToMax = $True
(Get-VM -Name MyVM).ExtensionData.ReconfigVM_task($guestConfig)

If you want to know more about locking all memory read this excellent article by Frank Denneman from 2013.

CPU Reservations

Setting reservations for CPU works the same way. You set the MHz that you want to reserve for this VM. The command below sets the reservation to 2000 MHz.

Get-VM -Name MyVM | Get-VMResourceConfiguration |Set-VMResourceConfiguration -CpuReservationMhz 2000

Getting current reservations

If you want to know your current reservations with PowerCLI  you can use the following oneliner:

Get-VM -Name MyVM | Get-VMResourceConfiguration | Select VM, CpuReservationMhz, MemReservationGB

When a zero is shown in the column there is no reservation active.