Using PowerCLI to set CPU/Memory reservation on VM´s in vSphere

I have made a function to set reservations on VM´s in PowerCLI. The function takes a VM as parameter and also percentage of CPU and Memory you want to reserve.

The function checks the Mhz of the physical CPU from the Host the VM is running on and makes the reservation from this.

In the function I can set both memory and cpu reservation or just one of them

here is a screendump of the function being used

and here is the PowerCLI

function Set-VMReservation{
Set VM Reservation on Memory and/or CPU

Use this function set VM reservations 

Use this parameter to set the percent of memory reserved for the VM

Use this parameter to set the percent of CPU Mhz from the host CPU to be reserved

Author: Niklas Akerlund
Date: 2012-07-19
    param (
   [Parameter(Position=0,Mandatory=$true,HelpMessage="A VM",
	$MemoryMB = 0,
	$CpuMhz = 0
	$VM = Get-VM $VM
	if ($MemoryMB -ne 0 -and $CpuMhz -ne 0){
		$MemoryMB = $MemoryMB/100
		[int]$MemoryMB = $VM.MemoryMB*$MemoryMB
		$CpuMhz = $CpuMhz/100
		[int]$CpuMhz = ([math]::truncate(((Get-VMHost -id $*$VM.NumCpu*$CpuMhz
		$VM | Get-VMResourceConfiguration |Set-VMResourceConfiguration -CpuReservationMhz $CpuMhz -MemReservationMB $MemoryMB
	}elseif ($MemoryMB -ne 0 -and $CpuMhz -eq 0){
		$MemoryMB = $MemoryMB/100
		[int]$MemoryMB = $VM.MemoryMB*$MemoryMB
		$VM | Get-VMResourceConfiguration |Set-VMResourceConfiguration -MemReservationMB $MemoryMB
	}elseif ($MemoryMB -eq 0 -and $CpuMhz -ne 0){
		$CpuMhz = $CpuMhz/100
		[int]$CpuMhz = ([math]::truncate(((Get-VMHost -id $*$VM.NumCpu*$CpuMhz
		$VM | Get-VMResourceConfiguration |Set-VMResourceConfiguration -CpuReservationMhz $CpuMhz
	}elseif ($MemoryMB -eq 0 -and $CpuMhz -eq 0){
		$VM | Get-VMResourceConfiguration |Set-VMResourceConfiguration -CpuReservationMhz 0 -MemReservationMB 0

In this example I have set the CPU reservation to 60 % and Memory to 40 %

Updated: Change to Powershell console in Windows 2012 Core

This friday I wrote a post about how to change from CMD to powershell and also found Thomas Lee´s posts about it.

What i wrote in my post was that i found the registry key that holds the command prompt but could not alter it. Andrew Morgan had already done all in his post, he also describes how you do, the secret is to take the ownership of the registry part (otherwise it is TrustedInstaller that is owner and that was why I could not change anything), after you change owner you also want to add some permissions, cause Administrator has only read by default.


And why do I want to add an registry key under AlternateShell instead of the Shell registry key that holds Explorer.exe. One reason is that when I enable the GUI from Core mode I do not get the full GUI. So when I run Get-WindowsFeature *gui-shell* | Add-WindowsFeature and then reboot the server it looks like the following screendump:

As you can see in the following screendump and in Andrew´s post, I keep the CMD and just set an new registry key that has a higher value, but not as high as Explorer. Doing this I get the Powershell console when in Core and the Explorer when in Full GUI mode. When in full GUI mode I get a temporary Pagefile error though 🙁 , the system seems to work correctly.. I have tested the Get-WindowsFeature *gui-shell* | Add-WindowsFeature on an installation where I have not done any alternation with the shells and there the page file dialog appears also so this is caused by another issue that I will investigate in another blog post.


Change to Powershell console in Windows 2012 Core instead of CMD

In twitter yesterday I saw that there was a discussion about setting default shell to Powershell in Windows Core, Jeffery Hicks has done this in Windows 2008 R2 and I wanted to test if his little trick worked in Windows 2012 also (by the way, why is it not default by default in 2012 Core? )

I have my testmachine ready and so lets go

Interestingly, When I check the registry I got that my shell in the 2012 Core was Explorer.exe,

but when I got a bit deeper I could see under AlternativeShells that the CMD was there

I tried to edit that but got an error 🙁

well lets try to exchange the explorer.exe in shell  to powershell.exe instead, I use the $env:userprofile to get the same directory as with cmd

$shell = "Powershell.exe -NoExit cd <code>$env:userprofile"
set-itemproperty "hklm:\software\microsoft\windows nt\currentversion\winlogon" shell $shell

And here you can see in my registry, notice that the $env:userprofile is still there thanks to the in the string variable and that means that when another user log in they will get their own directory:

 And when I try to log in again I get the Powershell Console by default and also in the “right” directory

Lets hope Microsoft set Powershell console as default also in the Core version when Windows 2012 becomes RTM 🙂

Update: I did apparently not follow the tweets so far that I noticed that the PS master Thomas Lee already had made a blog about this, in his post I would just add this to get the users directory

Set-ItemProperty -Confirm  -Path $RegPath -Name Shell -Value 'PowerShell.exe -noExit -Command "$psversiontable; cd $env:userprofile"'