I noticed the other day that I had a several vms that were showing memory ballooning. I check the usual suspects and discover that someone has allocated 2GB of memory to the vm but they also set a memory limit of 512mb. There is really no reason in our environment to set a memory limit like this and have a higher allocation configured as well. Now I encountered this previously and thought I had resolved this issue. It looks like someone set it up this way in some of the templates we are using.
You can’t check the resource settings on templates until you convert them to a vm. We have quite a few templates and I didn’t want to do these manually so it was a perfect time for a quick powershell script.
The first thing I needed to do was convert all of my templates into vms.
$vcentername = <vcentername> $foldername = <folderoftemplates> connect-viserver $vcentername $templates = get-template -location (get-folder $foldername) foreach ($name in $templates) { set-template -Template $name -ToVM }
Now that the templates have been converted into virtual machines I need to remove all memory limits from every vm in my environment.
$vcentername = <vcentername> connect-viserver $vcentername Get-VM | Get-VMResourceConfiguration | where {$_.MemLimitMB -ne '-1'} | Set-VMResourceConfiguration -MemLimitMB $null
With the memory limits having been removed I need to convert the template vms back into actual templates.
$vcentername = <vcentername> $foldername = <templatefoldername> connect-viserver $vcentername $vms = get-vm -location (get-folder $foldername) | get-view foreach ($name in $vms) { $name.markastemplate() }
That’s it.