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.



Sunday, July 7, 2013

SQL Saturday #226 - South Florida

For the second time, I had the pleasure and honor to be part of the organizer's board for SQL Saturday - South Florida 2013. To be part of such a great community event was without a doubt one of the best professional experiences I've had. I worked closely with so many great people including speakers, sponsors, volunteers, fellow organizers and of course the attendees. The networking opportunities were incredible.  It was great to hear so many success stories from people having one thing in common. They all attended SQL Saturday at one point during the last few years and have since then found a path to professional excellence and their ideal jobs. 

This year we set a new record for the number of registrations. We were 2 registrations shy of 800. That is right! 798 registrations. I put together a quick video using Geoflow that shows the registrations by zip code over time.

   

SQL Saturday #226 - South Florida from Luis Figueroa on Vimeo.

I also had the pleasure of hosting one of the sessions. In this presentation I provided an overview of SQL Server Master Data Services 2012 titled Getting started with MDS 2012.

I have posted the slides in slideshare and embedded a link below.


Lastly, if you are a data professional or you just have an interest in the field, I would like to invite you to become part of the PASS community. Getting involved is easy, visit http://www.sqlpass.org/ to get started.     If you are in South Florida, you can attend any of the local chapter meetings:


I look forward to seeing everyone once again at SQL Saturday - South Florida 2014!

If you are interested in attending one of my presentations please visit my whereabouts page for information on upcoming speaking engagements.

Sincerely.
Luis Figueroa