Saturday, December 17, 2011

How-To: Rapid Deploy VM Using Powershell

I have created a Powershell script to rapidly deploy VMs on Microsoft Hyper-V host and this was demo during the Hyper-V Workshop.


The demo Clustered Hyper-V infrastructure are as below diagram:

Clustered Hyper-V infrastructure with MS Hyper-V 2008 R2 SP1

On of my interesting demo is to rapid deploy VM on Microsoft Hyper-V host without using SCVMM. In achieving this I have created a Powershell script by referring to existing script found on http://social.technet.microsoft.com/wiki/contents/articles/176.aspx.

The script which I created is not perfect to suit everyone yet, but its enough for most of us out there who in need to create n numbers of VM given a short period. During this event, I have conducted a demo in creating 6 VMs with just a hit on 'enter' and all 6 VMs booted. And right now, I will share with you in details how to achieve this.

What the Powershell script does (In general speaking)
1. Read VMs configuration from CSV line-by-line
2. Create VM into 'C:\clusterstorage\volume1\vname'
3. Set the processor count of each VMs
4. Set the memory for each VMs
3. Create and attach a 'Differencing Disk' to each VMs

Future development of this script
1. To be able to set 'Dynamic Memory' for each VMs
2. To be able to set 'Fixed/Dynamic/Differencing Disk' for each VMs
3. To be able to set desire sysprep/preconfigured or mix OS platform base VMs
4. To be able to add each VMs to CSV cluster if the Hyper-V host is clustered
5. To add and improve comments into the script

CREATEVM.ps1

Import-Module "C:\Program Files\modules\HyperV"
$vmdefaultpath = "C:\ClusterStorage\volume1"
$ParentVHD = "win2k8r2sp1"
$path = "c:\createvm\VM.csv"
 import-csv -path $path|ForEach-Object {
$erroractionpreference = 0    
$vmName = $_.Name
$vmmemory = $_.Memory
$vmcpucount = $_.Cpucount
$vmSwitch = $_.Network
$vmpath = $vmdefaultpath
New-VM -Name $vmname
Set-VMMemory -VM $vmName -Memory $vmmemory
Set-VMCPUCount -VM $vmname -CPUCount $vmcpucount
Add-VMNIC -VM $vmName -VirtualSwitch $vmSwitch
New-VHD -VHDPaths $vmpath\$vmname\$vmname.vhd -ParentVHDPath $vmpath\vmbase\$ParentVHD.vhd
Add-VMDisk -VM $vmname -ControllerID 0 -Path $vmpath\$vmname\$vmname.vhd
Write-Host -BackgroundColor Green -ForegroundColor Black "Virtual Machine $vmname has been successfully created"     
}

VM.csv

CSV file which predefined VMs settings












How to use this script.
1. Create a CSV and list all necessary configuration of each VM as shown sample above.
2. Save the CSV as 'vm.csv'.
3. Copy 'vm.csv' to the Hyper-V host in 'c:\createvm\'
4. Copy the script above and save it as 'createvm.ps1' in 'c:\createvm\' (you may have to edit the variable to suit your requirement and configuration of your Hyper-V host.
5. Execute the PowerShell management Library for Hyper-V by codeplex.
6. Type 'powershell -file createvm.ps1'.

Requirement and caution
1. A syspreped OS image is required
2. Windows Server 2008 R2 and Windows 2008 R2 SP1 with Hyper-V role installed
3. Basic knowledge of Powershell script
4. Changing of variables in the script to suit your Hyper-V environment
5. Use at you own risk as you have been warned this may affects you Hyper-V server. KNOWING WHAT YOU ARE ABOUT TO EXECUTE IS IMPORTANT. 


A video worth thousand words