PowerShell Friday: Setting Reservations with PowerCLI
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.
Related Posts
1 Comment
Leave a Reply Cancel reply
You must be logged in to post a comment.
Hello Anne Jan,
looking for a statement to remove (set to 0) current memory reservations I stumbled across your site because I didn’t want to do it all manually. Unfortunately I noticed that the first PowerCLI statement does not work because (in PowerCLI 6.3 Release 1 at least) the Parameter ‘MemoryReservation’ cannot be found.
In case you want to update your article please consider that the parameters have changed.
What does work is the following bit.
Read current config:
Get-VM -Name VMName | Get-VMResourceConfiguration
Write new Memory Reservation value:
Get-VM -Name VMName | Get-VMResourceConfiguration | Set-VMResourceConfiguration -MemReservationMB 0
Of course this can also be achieved with the parameter -MemReservationGB 0
Thank you for your code help anyway, that was the way to go initially.