# Archive for November 22nd, 2010

## Quick and Dirty – Easy Script based VM creation in Hyper-V

Posted by Mikael Nystrom on November 22, 2010

Now, I have to admit that I’m not a guru on PowerShell (Thomas Lee is one of these strange “creatures” that master PowerShell way better than I do), but if I can make it work, so can you. In my work I need to create VMs based on Hyper-V often and fast so I started a bit of thinking, what do I need to make in a way I think is ok…hmmm…. I need:

• Something that can convert the install.wim file from the DVD into a VHD with the correct settings
• Something that then can build a VM using differencing disk

Let me just explain this shortly, the reason for converting WIM to VHD is simple, even if it is funny to install Windows it do take some time. I could have one image, but I would like the possibility to do this in a way that can be repeated and since the source is a DVD I will base my toolset on that. The reason for using differencing disks is simply space, I have in many cases 50-60 VMs on my laptop. So differencing disks are a parent-child related disk, I only create one reference image and then a create one difference disk for each VM.

So, fine, this can of course be done with out any scripts at all, but hey the WIM to VHD are some step to do manually and using Hyper-V manager is also possible, but I don’t have that time.

So, the first tool is VIM2VHD (I did a post on that sometime back – http://itbloggen.se/cs/blogs/micke/archive/2010/08/25/using-wim2vhd-to-create-reference-images-for-hyper-v.aspx)

The second tool/script we need is on CodePlex, go to http://pshyperv.codeplex.com/ and download both the PsHyper-V Install.ZIP AND the PSHyperV-R2 documentation. Install PSHyper-V using the installer and we have almost everything we need (WIM2VHD does require some tools from WAIK, so if you have not done it, download and install The Windows® Automated Installation Kit (AIK) for Windows® 7 – http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=696dd665-9f76-4177-a811-39c26d3b3b34)

Now, here is the command line to create a reference image based on VHD from the WIM file:

cscript WIM2VHD.wsf /wim:”C:\MDTLab\Operating Systems\Windows Server 2008 R2 x64 RTM\sources\install.wim” /sku:SERVERENTERPRISE /unattend:”c:\tools\wim2vhd\Unattend – US.xml”

It takes about 6-8 minutes and then my VHD’s is done, moving on to the next step

Since I’m “old” I still use the CMD prompt, So I did batch file with parameters that will be passed into powershell, so lets look at the batch file first:

powershell -ExecutionPolicy RemoteSigned -file bvm.ps1 %1 %2 %3 %4 %5

Really exiting :-), so this cmd file is called mvm.cmd (MakeVM) and when used correctly it looks like this:

First parameter = Location of VM, format is E:\VMs
Second parameter = Name of VM, format is MJ01
Third parameter = Memory, format is 1024 (in Megabyte)
Fourth parameter = Network, format is LAN (name of network)
Fifth parameter = ParentVHD for the OS disk, format is c:\Ref\W2K8r2x64.vhd

mvm.cmd E:\VM MIKE001 1024 LAN c:\Ref\W2K8r2x64.vhd

So, running that command will in this case build a VM called MIKE001 with 1GB of ram, connected to the network called LAB using a child disk based on the parent c:\ref\w2k8r2x64.vhd, place it in E:\VM and fire it up. I know perfectly well that I can do everything from within powershell, but I still prefer to do this from the cmd prompt. Running this takes less then 30 seconds and that’s ok. Now we need to see how the powershell script looks like and here it is:

import-module “c:\Program Files\modules\HyperV\HyperV.psd1”
$VMLOC =$args[0]
$VMNAME =$args[1]
$VMMEM =$args[2]
$VMNET =$args[3]
$VMSRC =$args[4]
$VM = New-VM -Name$VMNAME -Path $VMLOC Set-VMCPUCount -VM$VM -CPUCount 1
Set-VMMemory         -VM   $VM -Memory$VMMEM
Add-VMNIC            -VM   $VM -VirtualSwitch$VMNET
Add-VMSCSIController -VM   $VM -name “SCSI Controller”$Disk1 = New-VHD     -VHDPaths $VMLOC\$VMNAME-disk1.vhd -ParentVHDPath $VMSRC -Verbose -Wait$Disk2 = New-VHD     -VHDPaths $VMLOC\$VMNAME-disk2.vhd -Size 146gb -Verbose -Wait
Add-VMDisk           -VM   $VM -ControllerID 0 -LUN 0 -Path$Disk1
Add-VMDisk           -VM   $VM -ControllerID 0 -LUN 0 -Path$Disk2 -SCSI
Add-VMDisk           -VM   $VM -controllerID 1 -lun 1 “C:\MEDIA-TSLAB-LTI\LiteTouchMedia.iso” -DVD Start-VM -VM$VM

As you can see it is “almost” readable :-), this template will create a VM based on what I explained before, but it will also create a blank VHD (dynamic) on the first SCSI and it will also attach the LiteTouchMedia.ISO and the reason for that is simple, on that media I have a bunch of Post-OS Configuration tasks, so to be honest, my unattened.xml file will fire up the LiteTouch wizard and then I can select witch Post OS task I would like to run, something like DC, SQL Server, Deployment Server, TS or something like that

So, here is something else to try, fire up powershell, execute import-module “c:\Program Files\modules\HyperV\HyperV.psd1” and try this 4 different commands:

• get-command -module HyperV
• now you can see that there are some commands in that module :-)
• get-command -module Hyperv| get-help | format-table name,synopsis –auto
• Much better, thank you