Build VM Templates Automatically On a Schedule

Building VM Templates for your preferred Hypervisor can take a lot of time and also involves a couple of manual steps. Most importantly, when was the last time you updated your VM Templates?

Templates are like tomatoes, they’re rotten after just a couple of days. Even though you have Windows Updates being applied, the deployment time will get slower and slower each and every day.

Screenshot below Windows 2012 R2 Vanilla ISO, missing only 166 updates!

Windows Updates

Therefore in a perfect world you should use a deployment solution like MDT or SCCM to deploy VMs instead of templates. You should also Automate Your Reference Image Creation.

Unfortunately we don’t live in a perfect world so today I’m going to show you how to Build VM Templates Automatically On a Schedule with the latest Windows Updates.

In the following example I’m going to use VMware to build VM Templates, but the same applies to Hyper-V and XenServer.

PowerCLI_featured

Download and install vSphere PowerCli.

I’m using the free Microsoft Deployment Toolkit (MDT) with my Automation Framework on top to create the VM that later gets converted to a template.

Pick a unique MAC Address that’s going to be used by this VM. This is required for MDT to work 100% Zero Touch. Here’s my CustomSettings.ini

This tells MDT that it will automatically start the Task Sequnce named WS2012-017 and shut down the VM when finished.

My Powershell script will constantly check the VMs Powered State.

Automated VM Template 03

6.5 minutes to build a Windows 2012 R2 template with all the latest Windows Updates. This is only possible if you follow the steps in the post Automate Your Reference Image Creation.

Note: I’m new to vSphere Powercli and haven’t found a way to switch from Manual to Automatic MAC Address. Until then you need to do this manually and also do Sysprep if you don’t automate that part.

Automated VM Template 04

Automated VM Template 05

Convert the VM to a Template inside of vCenter.

Now just create a Schedule Task and you get it all running, almost automatically.

2 Responses to Build VM Templates Automatically On a Schedule

  1. for the mac: Function Set-NetworkAdapterMacAddressGenerated {

    param([parameter(mandatory=$true,ValueFromPipeLine=$true)]
    [VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.NetworkAdapter]$NetworkAdapter)

    process {
    $vm = $NetworkAdapter.Parent
    $nicName = $NetworkAdapter.Name
    $macType = “generated”

    $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
    $dev.operation = “edit”
    $vm.ExtensionData.Config.Hardware.Device |
    Where-Object {$_.DeviceInfo.Label -eq $nicName} |

    ForEach-Object {
    $dev.device = $_
    $dev.device.addressType = $macType
    $dev.device.MacAddress = $null
    }
    $spec.DeviceChange += $dev
    $vm.ExtensionData.ReconfigVM($spec)
    $vm | Get-NetworkAdapter | Where-Object {$_.Name -eq $nicName}
    }
    }

Leave a reply