Fast and Easy: Build a more or less complete Windows Server 2012 environment using PowerShell and Hyper-V
Posted by Mikael Nystrom on August 21, 2012
I promised to post this long time ago, so here it is. The script I use to build more or less complete quick and dirty demo environments. I did have another script before that did create all the VMs’, but then I still needed to configure things like name, IP, domain join and all that so I decided to do pretty much like a normal deployment solution do, use a template unattend.xml file that I then modify on the fly. It is possible to add even more parameters if needed.
How does it work?
It works like this, it will create a VM in Hyper-V for Windows Server 2012 based on a differencing disk, add some more disks, mount the VHDx file, copy a unattend.xml to the virtual hard disk, edit the file by doing a simple search and replace on some keywords, save the file, close the disk, dismount everything and start the VM. The VM will go through mini setup and read the unattend.xml file and apply those settings. That way I can add things like IP, DNS, Name and so on. The script will accept a bunch of parameters which will be used both as parameters for the VM, but also for settings inside the VM. There for, the name on the VM will also be the name inside the VM. For me it takes less than 6 minutes to build 2-3 servers with the correct names and IPs and more.
Well, easy. First you need to create a reference image the VHDx format and that should be created using Microsoft Deployment Toolkit 2012 Update 1. But if you are lazy you can use WIM2VHD.PS1. You might also need WIMINFO.PS1 to find out the index number of the WIM. So now you have a reference image for Windows Server 2012 in the VHDx format. Next is to download this script pack, unpack it and the run it.
Here is an example that will create a VM based on a difference disk:
.\MakeVM-DiffwUA.ps1 -VMBaseLocation D:\VMs -VMMemory 1024mb -VMRefDisk C:\Ref\WS2012.vhdx -VMNetwork External -UAtpl .\uafor2012wg.tpl -PW P@ssw0rd -GW 192.168.0.1 -DNS 192.168.0.10 -VMName DEMO -IP 192.168.0.100
There are two template files, one is called uafor2012wg.tpl and the wg stands for WorkGroup, the other one is called uafor2012dj.tpl and in that case dj stands for Domain Join. I did not specify the domain name to join as a parameter, so you need to modify that manually or extend the script with parameters for that to.