Modifying your organization’s virtual machine templates with PowerShell

One of the System Center Virtual Machine Manager features that I use most often is virtual machine templates. Templates allow you to automate the virtual machine creation process. A couple of months ago, for example, I was preparing to record some video training courses and needed to create a few dozen virtual machines. By using virtual machine templates, I was able to quickly and easily create all of the virtual machines that I needed.

As handy as virtual machine templates may be for the uniform creation of VMs, it is only natural for an organization’s needs to change over time. An organization might find, for instance, that it needs to change its templates to allow VMs to be created with an increased number of virtual CPUs, or perhaps a larger amount of memory.

What about using the Virtual Machine Manager console?

While you can make changes to VM templates through the Virtual Machine Manager console, it is sometimes more efficient to make these types of modifications through PowerShell. This is especially true if there are multiple templates that need to be modified. That being the case, I wanted to take the opportunity to show you how to modify VM templates using the Virtual Machine Manager Command Shell.

Like most other objects in VMM, virtual machine templates are identified by name. Therefore, I like to start out by getting a list of the VM templates that exist on the server. You can acquire such a list by using this command:

Get-SCVMTemplate | Select-Object Name

You can see what this command looks like in the screenshot below.

virtual machine templates

The next step in the process is to figure out which template you want to modify, and then map that template to a variable. There are a few different ways to accomplish this, but the easiest method is to set the variable to be equal to the Get-SCVMTemplate cmdlet, and then specify the name of the template. In my own lab for example, I have a template named CentOS. If I wanted to map that particular template to a variable, the command that I would use is:

$Template = Get-SCVMTemplate CentOS

If you want to confirm that the operation worked, then simply type the variable name and press Enter. PowerShell should show you all of the templates’ various attributes. You can see an example of this in the next screenshot.

Although the screen capture shown above is cut off, it shows many of the attributes that are associated with the template. The vast majority of these attributes can be modified on an as-needed basis. The trick is to locate the attribute that pertains to the part of the template that you want to modify. Once you know the attribute’s name, modifying the attribute is easy.

The template that is shown in the previous screen capture has been designed to create virtual machines with a single virtual processor and 1 gigabyte of memory. For the sake of demonstration, I am going to modify the template so that it can be used to create virtual machines with four virtual CPUs and 8GB of memory.

As previously noted, the first step in doing so is to figure out the names of the attributes that need to be modified. As you look at the previous screen capture, you can see that the virtual CPU count is associated with an attribute named CPUCount, and the template’s memory allocation is tied to an attribute named Memory (other attributes would be used if the template were designed to use dynamic memory). I have displayed these particular attributes a bit more clearly in the screenshot below.

virtual machine templates
One of the things that you will notice about the screenshot above is that I didn’t have to use the Get-SCVMTemplate cmdlet, because I have already mapped the cmdlet to a variable. That being the case, I simply specified the variable’s name, and then used the Select-Object cmdlet to tell PowerShell which attributes that I wanted to display.

As you have no doubt noticed by now, the Get-SCVMTemplate cmdlet is used to display information about a given VM template. However, this cmdlet is incapable of making modifications to the template. For that, a different cmdlet is needed — Set-SCVMTemplate.

Using the Set-SCVMTemplate cmdlet

There are a number of parameters that can be used with the Set-SCVMTemplate cmdlet, but in most cases you will only have to supply two parameters. These include the name of the template that you want to modify and the name of the attribute that you want to modify. In this case, however, it isn’t even necessary to provide the template name because the template has already been mapped to a variable.

So with that said, let’s go ahead and change the template’s virtual CPU count. As you will recall, the template is mapped to a variable named $Template, and the virtual CPU count is tied to an attribute called CPUCount. Therefore, the command looks like this:

Set-SCVMTemplate -Template $Template -CPUCount 4

You can see what this looks like in the next image.

The process used for changing the template’s memory allocation is nearly identical. The command used in doing so is:

Set-SCVMTemplate -Template $Template -MemoryMB 8192

Notice that even though PowerShell lists the attribute name as “Memory,” I was not able to reference Memory in the command. Doing so results in an error message stating that the use of Memory is ambiguous. Instead, I had to reference MemoryMB. You can see the end result in the screenshot below.

Virtual machine templates: Why I use PowerShell

For simple template modifications, it may be easier to simply use the VMM console rather than delving into PowerShell. However, PowerShell tends to be a far more efficient tool if you need to modify a number of templates. This is especially true given that PowerShell allows you to create a filtered list of templates. You might, for example, change the memory allocation for every template that is set up to use 1GB or less of memory.

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top