If you like this solution then you might like a newer script that creates lots of VMs based on specs that are stored in a CSV file.
Here you will find a PowerShell script I just wrote to deploy a lot of Windows Server 8 Hyper-V VMs with the minimum of effort. I created it because I wanted more load to stress my 20 GbE Live Migration network and creating the VMs by hand was too slow. Yes, it took me time to figure out and write the script in ISE, but I have it for the future and can lash out a lab in no time now.
Note that this script is using the new cmdlets for Hyper-V (and one cmdlet for clustering) that are in Windows Server 8, and not the VMM cmdlets.
What the script will do:
- Create a new folder for each VM on an SMB 2.2 file server shared folder
- Create a differencing disk pointing to a parent VHD. This is for lab purposes only. You’d do something different like create a new VHDX or copy an existing sysprepped VHDX in production.
- Create a new VM (e.g. VM1) using the VHDX
- Configure Dynamic Memory
- Start the VM
- Add the VM to a cluster
- It’ll do this 20 times (configurable in the foreach loop).
- Windows Server 8 SMB file share that is correctly configured
- A Windows Server 8 Hyper-V cluster
- A parent VHDX that has been sysprepped. That will automate the configuration of the VM when it powers up for the first time.
Here’s the script. My old programmer instinct (which refuses to go away) tells me that it could be a lot cleaner, but this rough and ready script works. There is also zero error checking which the old programmer instinct hates but this is just for deploying a lab workload.
$parentpath = “\\fileserver\Virtual Machine 1\WinSvr8Beta.vhdx”
$path = “\\fileserver\Virtual Machine 1″
foreach ($i in 1..20)
#Create the necessary folders
$vmpath = “$path\VM$i”
New-Item -Path $vmpath -ItemType “Directory”
New-Item -Path “$vmpath\Virtual Hard Disks” -ItemType “Directory”
#create a VHDX – differencing format
$vhdpath = “$vmpath\Vitual Hard Disks\Disk0.vhdx”
New-VHD -ParentPath $parentpath -Differencing -Path $vhdpath
#Create the VM
New-VM -VHDPath “$vhdpath” -Name “VM$i” -Path “$vmpath\Virtual Machine” -SwitchName “External1″
#Configure Dynamic Memory
Set-VMMemory -VMName “VM$i” -DynamicMemoryEnabled $True -MaximumBytes 8GB -MinimumBytes 512MB -StartupBytes 1GB
#Start the VM
#Add the VM to the cluster
Add-ClusterVirtualMachineRole -Cluster “hvc1″ -VMName “VM$i”
This blog post is the property of Aidan Finn (@joe_elway / http://www.aidanfinn.com) and may not be reused in any manner without prior consent of Aidan Finn. You may quote one paragraph from this blog post if you link to the original blog post.