Creating Windows Server 2012 Hyper-V VMs with unattend.xml

I recently had the opportunity to work with the PowerShell Deployment Tool kit (PDT).  The PowerShell Deployment Toolkit is a set of scripts and xml files that helps automate the build out of System Center servers.  It’s absolutely the coolest set of scripts.

VMCreator.ps1 is the script that automatically builds the VMs and installs the OS.  It relies on a Variable.xml file which can be configured for your farm deployment.  This is a general tool and not System Center specific.

In this post I’ll be sharing my simplified version of VMCreator.ps1 – called CreateVM.ps1.  Having seen what VMCreator does, I’ve modified it to be able to spin up single base VMs passing parameters on the command line.  It runs on Windows Server 2012 and creates a VM on the server that the script executes on.  The VMs are created based on existing .vhd or .vhdx files.  These files can be created through the use of Create-WindowsImage.ps1 and a windows .ISO file.

The script accepts the following parameters:

  • VMname   – the name of the VM to be created
  • VMFolder – the folder where the VM will be placed
  • OSDisk      – the name of the OS disk (.vhdx) to create the VM
  • Memory    – amount of memory in MB to allocate to the VM
  • CPU           – number of CPUs to allocate to the VM

Syntax:  .\CreateVM –VMName DB01 –VMFolder d:\VMs –OSDisk d:\VHD\WIN2012.vhdx –Memory 4096 –CPU 2

Worth noting is the creation of Windows Setup unattend.xml file.  Unattend.xml automates the installation of the Windows OS in the VM.  It enables you to script the installation with configuration information such as Computer name, IPAddress, Domain to join, Service accounts and Administrator password, just to name a few.  The installation executes without having to log in – saving you from doing all the mundane tasks.

In this script, Unattend.xml is created dynamically as the script runs and it’s written off to a file within the VM.   Note the @” and “@ symbols that encapsulate the xml.  Also note the variables that are being used within the xml.

@”
<?xml version=”1.0″ encoding=”utf-8″?>
<unattend xmlns=”urn:schemas-microsoft-com:unattend”>
    <settings pass=”specialize”>
        <component name=”Microsoft-Windows-Shell-Setup” processorArchitecture=”amd64″ publicKeyToken=”31bf3856ad364e35″ language=”neutral” versionScope=”nonSxS” xmlns:wcm=”http://schemas.microsoft.com/WMIConfig/2002/State” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>
            <ComputerName>$VMName</ComputerName>
            <RegisteredOrganization></RegisteredOrganization>
            <RegisteredOwner></RegisteredOwner>
        </component>
“@ | Out-File “$Drive`:\unattend.xml” -Encoding ASCII

Download the script and try it out – I hope it saves you a bunch of time.  CreateVM
Rename it from .txt to .ps1

Also, checkout the PDT – I guarantee you will be amazed!

– Lisa

One thought on “Creating Windows Server 2012 Hyper-V VMs with unattend.xml

  1. Great posts, good links! Nice to have a simple script all set up. Can I use this to set up a VM I would use to test my migrations? If so, how would I do this? If not, what else do I need to do? Perhaps these questions can be the subject of a future post.

Leave a Reply

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