How to: Create multiple VM’s in Hyper-V within a few seconds

Automation is one of my favorite things. Some actions are just time consuming, so let’s automate these things!! For example, create a demo environment within Hyper-V. This environment needs 8 VM’s. With a few lines of code, this is realy easy to automate, off course with PowerShell.


## Create 8 Gen2 Virtual Machines
## 4 vCPU per VM
## 1 GB per VM
## C: drive 50 GB Dynamic
## D: drive 50 GB Dynamic
## NIC renamed to MGMT
$vSwitchName01 = "NIC - PRI"
$InstallRoot = "E:\DEMO"
$VMName = "HV-0"

## How much VM's 1..8 = 8 VM's
1..4 | % {
New-VHD -Path ($InstallRoot + "\$VMName" + "$_\" + "\$VMName" + "$_" + "_C.vhdx") -SizeBytes 50GB -Dynamic
New-VHD -Path ($InstallRoot + "\$VMName" + "$_\" + "\$VMName" + "$_" + "_D.vhdx") -SizeBytes 50GB -Dynamic
New-VM -VHDPath ($InstallRoot + "\$VMName" + "$_\" + "\$VMName" + "$_" + "_C.vhdx") -Generation 2 -MemoryStartupBytes 1GB -Name ("$VMName" + "$_") -Path $InstallRoot -SwitchName $vSwitchName01

Set-VMProcessor -VMName ("$VMName" + "$_") -Count 4
Set-VM -VMName ("$VMName" + "$_") -AutomaticStopAction ShutDown -AutomaticStartAction StartIfRunning
Enable-VMIntegrationService ("$VMName" + "$_") -Name "Guest Service Interface"

Rename-VMNetworkAdapter -VMName ("$VMName" + "$_") -NewName "MGMT"
Set-VMNetworkAdapter -VMName ("$VMName" + "$_") -Name "MGMT" -DeviceNaming On

Add-VMScsiController -VMName ("$VMName" + "$_")
Add-VMHardDiskDrive -VMName ("$VMName" + "$_") -ControllerType SCSI -ControllerNumber 1 -ControllerLocation 0 -Path ($InstallRoot + "\$VMName" + "$_\" + "\$VMName" + "$_" + "_D.vhdx")

Start-VM -Name ("$VMName" + "$_") | Out-Null
}

Within a few seconds, I’ve 8 VM’s up and running.

2016-03-23_10h10_00    2016-03-23_10h10_44    2016-03-23_10h11_23

/ Happy Automation!

How to: Create a VM within a few second in Hyper-V 2012 R2

In some cases I’ve to create multiple VM’s within my Hyper-V environment. You can choose to create and configure each VM manually, but you can also use PowerShell. Now you can see how powerfull PowerShell really is! I’ve created multiple VM’s within a few seconds and ready to rock! I’ve created the following script. The only thing you’ve to do, is changing some variables 🙂

In this example the script will create a new VM named RES-MGN01. The VM will be configured with 2 GB, using Dynamic Memory. The VM is using a differencing disk (VHDK) with a Windows Server 2012 R2 sysprepped parent VHDX, placed on an SSD disk (P: drive) in my server. The virtual switch the VM is connected to, is named NIC – WAN.

$VMName = “RES-MGN01”
$VMMemMaxBytes = 2048MB
$VHDXName = “OS.vhdx”
$VMMemStartup = 512MB
$VMMemMinBytes = 512MB
$VMPath = “E:\”
$VHDParent = “P:\Hyper-V Parents\TMPL-W2012R2DC\Virtual Hard Disks\TMPL-W2012R2DC.vhdx”
$VMSwitchName = “NIC – WAN”

New-VM -Name $VMName -Path $VMpath$VMName -Generation 2 -SwitchName $VMSwitchName

Set-VM -Name $VMName -DynamicMemory -MemoryStartupBytes 512MB -MemoryMinimumBytes $VMMemMinBytes -MemoryMaximumBytes $VMMemMaxBytes

New-VHD -ParentPath $VHDParent -Path $VMPath\$VMName\$VMName\$VHDXName -Differencing

Add-VMHardDiskDrive -VMName $VMName -Path $VMPath\$VMName\$VMName\$VHDXName

Add-VMDvdDrive -VMName $VMName

Start-VM -Name $VMName

2014-01-14_11h48_41    2014-01-14_11h49_20    2014-01-14_11h50_27

2014-01-14_11h51_18    2014-01-14_11h52_12