Menu

Follow me on Twitter

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

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, 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:

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: