PowerCLI: Reset CPU and Memory Limits

Today I noticed a memory limit on a vm. After investigating my environment using the vEcoShell and the Community PowerPack, I found more vms with memory limits set. It turned out that there was a template which had the limit set. I could easily reset all limits using the GUI, but I thought I rather do it with PowerCLI. 

Alan Renouf did a post already on a oneliner to reset all cpu and memory limits back in july 2009. After trying that code I found it rather slow. If you want to speed up things in PowerCLI you need to use the Get-View cmdlet. After some digging in the vSphere API Reference, I came up with a different peace of code that is much faster. 

First Alan’s code:

Get-VM VM001 | Get-VMResourceConfiguration | Set-VMResourceConfiguration -MemLimitMB $null

This took 18.43 seconds to complete in my environment 

My code:

$spec = new-object VMware.Vim.VirtualMachineConfigSpec
$spec.MemoryAllocation = New-Object VMware.Vim.ResourceAllocationInfo
$spec.MemoryAllocation.Limit = -1
$vm = get-view -ViewType VirtualMachine -Filter @{"Name"="VM001"}
$vm.ReconfigVM_Task($spec)

This took only 1.10 seconds to complete. 

You may notice that my code doesn’t wait for the task to complete (async) while Alan’s code does (sync), but completing the task took only 4 seconds. That makes my code still 14 times faster. 

Now since the report node is already available in the Community PowerPack, I thought it would be nice to add some actions to the PowerPack to reset the limits. 

Extending the Community PowerPack

  1. Start the vEcoShell and expand the VMware Community PowerPack node 
  2. Expand the Virtual Machines node 
  3. Select the Reservations / Shares / Limits node 
  4. Rightclick in the Actions windows on the right and select New->Script Action 
  5. Enter “Reset Memory Limit” as the name for this new action 
  6. Click on the “Display Configuration” button 
  7. Click on the “Use Custom” Button under the Category section and enter “Resource Utils” as the new category name
     
  8. Click “OK” to close the display configuration properties window 
  9. Delete all sample code that is displayed on the Command tab and paste the following code:
    if ($global:defaultviservers) {
    	$Answer = [System.Windows.Forms.MessageBox]::Show("You are about to reset the Memory limits of all selected virtual machines`nClick OK to continue","Warning",[System.Windows.Forms.MessageBoxButtons]::OKCancel,[System.Windows.Forms.MessageBoxIcon]::Warning)
    	Switch ($Answer) {
    		"OK" {
    			$input | ForEach-Object {
    				$spec = new-object VMware.Vim.VirtualMachineConfigSpec
    				$spec.MemoryAllocation = New-Object VMware.Vim.ResourceAllocationInfo
    				$spec.MemoryAllocation.Limit = -1
    				$vm = get-view -ViewType VirtualMachine -Filter @{"Name"=$_.vmname}
    				$vm.ReconfigVM_Task($spec)
    			}
    		}
    		"Cancel" {}
    	}
    }
    Else {
    	[System.Windows.Forms.MessageBox]::Show('You must connect to one or more hosts before you can use this node. Please click on the ''Managed Hosts'' node of the VMware PowerPack, connect to one or more of the servers you have configured there, and then try again.','Connection not established',[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Information) | Out-Null
    }
    
  10. Click “OK” to save our script action

When you now select one or more VMs from the results window, you can reset the memory limit by simply clicking the “Reset Memory Limit” Action.

I also modified the code to reset CPU limits. Just follow the procedure above to create a “Reset CPU Limit” script action and paste the following code instead:

if ($global:defaultviservers) {
	$Answer = [System.Windows.Forms.MessageBox]::Show("You are about to reset the CPU limits of all selected virtual machines`nClick OK to continue","Warning",[System.Windows.Forms.MessageBoxButtons]::OKCancel,[System.Windows.Forms.MessageBoxIcon]::Warning)
	Switch ($Answer) {
		"OK" {
			$input | ForEach-Object {
				$spec = new-object VMware.Vim.VirtualMachineConfigSpec
				$spec.CpuAllocation = New-Object VMware.Vim.ResourceAllocationInfo
				$spec.CpuAllocation.Limit = -1
				$vm = get-view -ViewType VirtualMachine -Filter @{"Name"=$_.vmname}
				$vm.ReconfigVM_Task($spec)
			}
		}
		"Cancel" {}
	}
}
Else {
	[System.Windows.Forms.MessageBox]::Show('You must connect to one or more hosts before you can use this node. Please click on the ''Managed Hosts'' node of the VMware PowerPack, connect to one or more of the servers you have configured there, and then try again.','Connection not established',[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Information) | Out-Null
}

Related posts:

  1. PowerCLI: Match VM and Windows harddisks – Part 2 Tweet This is a follow up on a post I did a couple of weeks ago to create a mapping table between Windows- and VMware hard disks. In another previous...
  2. The VESI – Getting it up and running Tweet On April 8th we had a very good DutchVMUG meeting, which was totally dedicated to using PowerCLI and The VESI (Virtualization EcoShell Initiative). Although setting up the environment is...
  3. $Null or Nothing? Tweet When looking at a lot of PowerCLI scripts available I noticed that people tend to forget to validate their output. For example let’s look at the following piece of...
  4. PowerCLI: Get WMI info from isolated guests Tweet A few weeks back I posted an article on matching Windows and VMware disks. Unfortunately this would work only if you could remotely query WMI information from that VM....
  5. Match VM and Windows harddisks using PowerCLI Tweet Today I was asked to extend a disk of a Windows virtual machine. Normally this is a standard procedure and finished within minutes. The hardest part of the procedure...

12 Comments on “PowerCLI: Reset CPU and Memory Limits”

  1. #1 Gabrie van Zanten
    on Jun 8th, 2010 at 9:42 am

    Great piece of script. Now, how could I exclude two VMs in this for example because they need those limit or reservation set?

    Maybe import a CSV file with VMs that should or shouldnt be reset? Or maybe only reset VMs that don’t have an entry in the notes field like: MemLimit = yes

    Just a thought

  2. #2 Brandon
    on Jun 29th, 2010 at 3:09 pm

    I tried adding your code to the Powerpack. It added OK, but when I run it I get an error at Line 10 char 24 “Method invocation failed because [System.Object[]] doesn’t contain a method named ‘ReconfigVM_Task’.”

    I am going to take a guess that I might be needing an update to my powercli version..

  3. #3 Arnim van Lieshout
    on Jun 29th, 2010 at 4:31 pm

    Brandon,
    Your powerCLI version is ok.

    The error message refers to System.Object[] so an array of objects is returned instead of a single object.
    In other words the query for the vm returns more than one vm.
    This could be caused by:
    multiple VMs with the same name
    multiple connections to the same server
    multiple connections to different servers where a vm with the same name exists

    Make sure the VM name is unique accross all connections.

    An update to the script is needed to check for this condition. I’ll post an update when I find the time.

    -Arnim

  4. #4 Brandon
    on Jun 29th, 2010 at 6:40 pm

    Thanks Arnim! I started playing around with it some more and it seems that about 50 of our 600 systems that did have limits are returning the same issue. Thankfully for the other 550 the script worked fine. I just needed to deselect the problem children.

    Brandon

  5. #5 Ed Grigson
    on Jul 23rd, 2010 at 1:24 pm

    Nice scripts, although can’t you already do this with the built in functionality (not the community powerpack)? Go to the VMWare node, then Virtual Machines then Links -> Resource Configurations. There’s are two Actions there – ‘Set CPU Limit to Unlimited’ and ‘Set Memory Limit to Unlimited’.

  6. #6 Arnim van Lieshout
    on Jul 23rd, 2010 at 2:12 pm

    You are absolutely right Ed!
    Only that option uses the Set-VMResourceConfiguration cmdlet which is rather slow as described in my article. Accessing the API object to do this task is a much faster alternative.

  7. #7 Ed Grigson
    on Jul 24th, 2010 at 12:58 pm

    That makes sense now that you point it out. Thanks for clarifying – I’d wondered why the powerpack sometimes duplicated built-in functionality and this explains perfectly.

  8. #8 Przykazania – po trzecie – NIE UŻYWAJ LIMITÓW PAMIĘCI do cholery! « Wirtualizacja w praktyce
    on Aug 27th, 2010 at 3:12 am

    [...] Arnim van Lieshout – skrypt do resetowania limitów http://www.van-lieshout.com/2010/06/powercli-reset-cpu-and-memory-limits/ [...]

  9. #9 Top 5 Planet V12n blog posts week 22 | Download VDI Solutions
    on Oct 27th, 2010 at 1:00 am

    [...] van Lieshout – PowerCLI: Reset CPU and Memory LimitsToday I noticed a memory limit on a vm. After investigating my environment using the vEcoShell and [...]

  10. #10 Arif
    on May 25th, 2011 at 4:33 am

    Arnim, I must tell you that VMware vSphere PowerCLI Reference: is a great book! I had to preorder this book :-)
    Regarding this script, I have a question, is CPU mask reset and CPU Limit reset to 1 are the same? I need to figure out a way to group vm cpu mask reset using PowerShell. Thanks

  11. #11 Arnim van Lieshout
    on Jun 8th, 2011 at 10:30 am

    Arif,

    no, they’re not the same!
    a CPU Limit is a hard limit on the amount of CPU GHz a vCPU wil receive.
    a CPU mask on the other hand is a way to hide specific bits that identify the CPU and its features. For vMotion compatibility certain bits MUST match otherwise vMotion will fail.

  12. #12 Resolving VM Memory Limits with PowerCLI
    on Dec 14th, 2012 at 12:52 pm

    [...] editing the settings of each VM, comparing configured memory with the limit, and so on. I found a lot of good resources for clearing them in a sweeping manner, but I wanted an approach that would allow [...]

Leave a Comment