Monday, July 22, 2013

Building a Hyper-V Windows Server in under 3 seconds

I find myself building VMs almost on a daily basis. If I am not building a proof of concept or a demo for a client, I am testing different configurations or architectural approaches to solutions, testing new features of SQL Server, Cumulative Updates etc... Whatever the reason might be, creating a new VM always takes valuable time.

Back in April I blogged about using Hyper-V as your virtualization platform for a lab environment. I walked through building a template Virtual Server and then creating a base drive to be used to build new machines from. Using a combination of base and differential drives reduces the time that it takes to provision a new VM dramatically as you don't have to install the operative system every time. However, it still takes a few minutes to walk through the Hyper-V manager wizards to create your differential disk and new machine. Let's see how to take things to the next level by using two simple PowerShell commands to build your differential disk and Virtual Server.

Assumptions: For these commands to work you must already have a base drive to be used as the parent drive for the differential disk. For step by step instructions creating a base drive check my previous blog post Setting Up a Virtual Server Lab With Hyper-V.

Let's get started...

The goal here is to avoid going through the Hyper-V manager and use PowerShell commands to provision your new VM. See the script below.

Let's take a look at the few lines that initialize the variables that we are going to use as parameters.

#Set variables
$Name = "VMName" #This is the Hyper-V name of the Virtual Machine
$ParentPath = "D:\Hyper-V\Virtual Hard Disks\WS2012Base.vhdx" #Path to the base drive
$DiskPath = "H:\Hyper-V\"+$Name+"\"+$Name+".vhdx" #Path for the differential drive, I use my SSD for this
$VMPath = "D:\Hyper-V\"+$Name+"\" #Path for the Virtual Machine definition files

Then we execute the command to create the differential disk using the variables initialized above.

#Create new differential disk
New-VHD -ParentPath $ParentPath -Path $DiskPath -Differencing -SizeBytes 60GB  

And lastly, we run the command to create the VM using the differential disk created in the previous step.

#Create VM
New-VM -VHDPath $DiskPath -ComputerName "localhost" -MemoryStartupBytes 4GB -Name $Name -Path $VMPath -SwitchName "External Network"

Here it is without the comments:
---------------------------------------------------------------------------------

$Name = "VMName" 
$ParentPath = "D:\Hyper-V\Virtual Hard Disks\WS2012Base.vhdx" 
$DiskPath = "H:\Hyper-V\"+$Name+"\"+$Name+".vhdx" 
$VMPath = "D:\Hyper-V\"+$Name+"\"

New-VHD -ParentPath $ParentPath -Path $DiskPath -Differencing -SizeBytes 60GB  

New-VM -VHDPath $DiskPath -ComputerName "localhost" -MemoryStartupBytes 4GB -Name $Name -Path $VMPath -SwitchName "External Network"

-----------------------------------------------------------------------------------

I typically start the VMs with 4GB of RAM and 60GB of maximum disk space so I hard coded those values in the command. Of course you can substitute those values with additional variables and initialize them in the first step.

In my system, running these two PS commands take 2 to 3 seconds and I am ready to go. That is it! Talk about time savings.

Check out the New-VHD and  New-VM TechNet documentation pages for more information and additional options regarding these two Hyper-V commands.



No comments:

Post a Comment