From the Lab: Create Thinly Provisioned VM’s in PowerShell

Alex Jauch -- Architect, Microsoft Private Cloud

    

I had an interesting discussion with a customer today that I thought I’d share.  He wanted to use our Fixed-Thin VHD capability that we’ve talked about previously as part of an automated VM provisioning process that he was writing in PowerShell.  No worries, I said.  Just use New-NAVirtualDisk and you’re all set.  Yes, he said, but how do I actually create the VM?  And, by the way I want the VM to be an HA VM running on my cluster.

Hmm.  I assumed that this would be very simple and did a quick internet search.  I couldn’t actually find any PowerShell sample scripts to do this.  I found this a bit strange.  A few minutes of spelunking online and I was able to find the correct commands to do this.  To save you all the trouble of looking this up for yourself, here’s how you do it.

    

First, you need the Hyper-V PS toolkit from Codeplex.   I already have this because it’s so darn useful.  You can get ithere:  http://pshyperv.codeplex.com/releases/view/62842.  I also had the DataONTAP PS toolkit installed which you’ll need.

    

The sample script is pretty simple:

    

Import-Module hyperv

Import-Module dataontap

Import-Module failoverclusters

$VMName = <<Insert VM Name Here>>

New-VM -Name $VMName -Path C:\ClusterStorage\Volume1

 

New-NaVirtualDisk -FullName C:\ClusterStorage\Volume1\FromPS.vhd -Size 10gb

Add-VMNIC -VM $VMName -VirtualSwitch MGT

Add-VMDisk -VM $VMName -Path C:\ClusterStorage\Volume1\FromPS.vhd

Add-ClusterVirtualMachineRole -Name "Sample 1" -VirtualMachine $VMName

Note that this script assumes you are running locally on a node that is a member of the cluster.  The script first creates a new VM on the local host.  Then we create a new Fixed-Thin VHD using our PS toolkit command.  In this case I’m doing the simplest possible thing and only adding a HD and one NIC.  You can use this same technique to configure the VM in any way you want.  Once the VM is configured properly, you use the Add-ClusterVirtualMachineRole cmdlet to make it an HA VM.   Note that this is a completely blank VM.  Empty boot volume, the whole bit.  In my customer’s case he wanted to PXE boot and install windows from scratch.

    

How would this be different if you wanted to do this using a Sysprep’d Gold master?  Just use Copy-NaHostFile.  This will create a space efficient clone of your Sysprep’d template.  Like this:

    

Import-Module hyperv

Import-Module dataontap

Import-Module failoverclusters

    

$VMName = <<Insert VM Name Here>>

 

New-VM -Name $VMName -Path C:\ClusterStorage\Volume1

 

Copy-NaHostFile C:\ClusterStorage\Volume1\SysPrep.vhd C:\ClusterStorage\Volume1\FromPS.vhd

Add-VMNIC -VM $VMName -VirtualSwitch MGT

Add-VMDisk -VM $VMName -Path C:\ClusterStorage\Volume1\FromPS.vhd

Add-ClusterVirtualMachineRole -Name "Sample 1" -VirtualMachine $VMName

Simple, no?

Stay tuned next week for a much more elegant way of doing this from Glenn Sizemore…  (Just kidding.  I think.)