Automating the build of VMs with Powershell and Hyper-V 2012

June 21, 2012 — Leave a comment

Ok a bit of an unusual subject for my blog maybe, but over the last few days I have attended the TechNet UK IT Camp, regarding Hyper-V and System Center 2012 amongst other things. The most interesting subject for me was whats new in Hyper-V 2012 and Virtual Machine Manager 2012. Simon May a Microsoft IT Pro evangelist and blogger at http://simon-may.com/ demonstrated Microsoft VDI in Server 2012, starting his demo by building out 4 VM’s using Powershell 3 in Windows 2012. The script is very simple and similar to scripts I have written in PowerCLI for VMware vSphere, so I fancied the challenge of trying to recreate this script. My script seen below may not be a complete copy of what Simon demonstrated but seems to work well.

$VMNames =@(1..4)

Foreach ($VM in $VMNames)
{
$VM = "Serv"+$VM
New-VHD -ParentPath "E:\VHDs\VHD-Parent\2012-VHDs.vhd" -Differencing -Path "E:\VHDs\Demo\$vm.vhd"
New-VM -VHDPath "E:\VHDs\Demo\$vm.vhd" -VMName $VM -MemoryStartupBytes 1536MB
Set-VMProcessor -VMName $VM -Count 2
Start-VM -Name $VM

}

In the 1st line of the script we are creating an array depicting the amount of VM’s we wish to create, in Simon’s demo his array contained the name of the 4 VM’s this will work either way. We then move on to create a loop, the loop moves through each of the VMs in the array. Inside the loop the VM name is then set to be Serv plus the number from the array. Next we are creating the VHD for the VM, for this demo we are using a differencing disk, a differencing disk is similar to a linked clone in VMware land where the reads come from a set disk and the differences are written to a delta/difference file, in the example above I have referenced a VHD that has had Windows 2012 pre-installed and has been syspred ready to roll and the we have specified the path for the differencing disk. Finally we move on to create the VM in Hyper-V referencing the VHD we have pre-created, setting the member and CPUs and finally starting the VM before going on to repeat the process.

The result will be 4 VMs rolled out with linked to the parent VHD.

hyperv-createvms

No Comments

Be the first to start the conversation!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s