Hyper-V VM Tribbles – VMs Everywhere!

image

I was thinking this week of how to demonstration / highlight the cool new processing capacity of new multi-core processors.

I came up with a really short PowerShell script (based on James O’Neill’s Hyper-V management library available on Codeplex) that creates a specific number of VMs on Hyper-V using a base VHD file and differencing disks.  It’s really easy, and ultimately could be the basis for any home grown Virtual Desktop Infrastructure or “cloud on demand” service.

The script is really very simple – here’s the copy I used to make the screen shot shown:

# requires Hyper-V Management Library from Codeplex (https://pshyperv.codeplex.com/)
#uses defaults for RAM and does not setup NIC - easy to adjust

$MaxVMs = 24
$Diff_Dir = "C:\ClusterStorage\Volume1\DIFFs"
$BaseVM = "C:\ClusterStorage\Volume1\VDI_Fun\WS2008R2_EnterpriseCore_x64.vhd" 
$index = 1
do {
$new_VM_name="Dif_VM_$($index)"
$new_VHD_name="$($Diff_Dir)\Diff$($index).vhd"
new-vhd $new_vhd_name -parentVHD $BaseVM -wait
$myVM = (new-VM $new_vm_name)
add-vmdisk $myVM 0 0 $new_VHD_name
Start-VM $myVM
new-vmconnectsession -vM $myVM
# next line WAITS for the new VM to respond before going on... comment out for more RAPID provisioning
# test-vmheartbeat $myVM -timeout 300
$index++
}
While ($index -le $MaxVMs)

So what did this snippet do on a server in my tiny little lab?  It quickly created 24 VM's based on a single Windows Server 2008 R2 Core installation VHD I had previously created!  This sort of script works against Hyper-V Server (and even runs on Hyper-V Server 2008 R2 since it has PowerShell support!).

Can you imagine how easy (and cheap) it could be to spin up VMs of all kinds with scripts like this!  It’s even better with the System Center tools, but you can’t beat the price of Hyper-V Server 2008 R2 and the PowerShell library on Codeplex!.

-John