Build a Step by Step #HyperV and #VMM Lab with #PowerShell

6 min read

Hello Folks,

PowerShell is king!

Let’s build today a Hyper-V and VMM lab with PowerShell.

Are you ready?

Before we get started, let’s illustrate first the hardware and software requirements:

Hardware Requirements:

  • Computer running Windows Server 2012 R2 with Hyper-V enabled. (Get the evaluation of Windows Server 2012 R2 ISO here: WS2012 R2).
  • Minimum 16GB RAM.  This is required to support all of the running virtual machines.
  • 200GB or more free disk space.
  • SSD or faster HDD. I strongly recommend using SSD for better performance.

Software Requirements:

  • System Center 2012 R2 Virtual Machine Manager – pre-installed .VHD evaluation
    Download @ VMM 2012 R2
  • Microsoft Hyper-V Server 2012 R2 – Free hypervisor installation .ISO image
    Download @ Hyper-V 2012 R2 Core
  • Convert Window Image Script
    Download @  Convert-WindowsImage.ps1
  • Create a Generation 2 Sysprep VM using Convert-WindowsImage.ps1
    Run the following script using the ISO Eval Image for WS2012R2 downloaded above
    PS C:\>.\Convert-WindowsImage.ps1 -SourcePath D:\9600.16384.WINBLUE_RTM.130821-1623_X64FRE_SERVER_EVAL_EN-US-IRM_SSS_X64FREE_EN-US_DV5.ISO -VHDPath C:\Hyper-V\base\WS12R2_Server_Datacenter.vhdx -SizeBytes 50GB -VHDFormat vhdx -VHDType Dynamic -VHDPartitionStyle GPT -Edition ServerDataCenter

Choose a home base folder on the hard disk drive that has 200GB or more free space, create or pick a folder where you’ll run everything from.  In my demo, I am using the root of my C:\ SSD drive.
Put the downloaded software in the \Base folder.
Move or copy the downloaded three evaluation software files (VMM .VHD, WS2012R2 .VHDX and Hyper-V Core .ISO image) into the \Base folder. The entire contents of the \Base folder should then look like this:

Lab Folder

 

 

 

 

 

Turn on “Enhanced Session Mode” for better interaction with your virtual machines as you’re setting them up.
In Hyper-V Manager, select your host server on the left-hand side, and then under Actions on the right, click on Hyper-V Settings
In the Hyper-V Settings dialog, on the left under Server, select Enhanced Session Mode Policy.
On the right, make sure to check the box Allow enhanced session mode. Then click OK.
In the same window settings, on the left under User, select Enhanced Session Mode as well.
On the right, make sure to check the box Use enhanced session mode. Then click OK.

HVSettings01

HVSettings02

Run the VM Creation Script below:

 # Author: Charbel Nemnom
 # Website: www.https://charbelnemnom.com
 # Email: charbel[at]nemnom.name
 # Date: 29/04/2014
 #
 # The script will create 6 Virtual Machines:
 #
 # DC01: Domain Controller
 #
 # FS01: File Server 01
 # FS01: File Server 02
 # As Scale-Out Filer Server Cluster
 #
 # Hyper-V01: Hyper-V 01 Server
 # Hyper-V01: Hyper-V 02 Server
 # As Hyper-V Cluster
 #
 # SCVMM01: System Center Virtual Machine Manager to manage the hosts
 #
 # This is to run on your lab host that already has Hyper-V role installed.
 # The script configures your Hyper-V host with a private virtual switch, several
 # Virtual hard disk files, and several virtual network cards.

$InstallRoot = "C:\Hyper-V\"  # Location for all of your VMs, and the place where you downloaded the evaluation software.

$WinServerDiskPath = $InstallRoot + "base\WS12R2_Server_Datacenter.vhdx"
 $HVserverISOPath = $InstallRoot + "base\en_microsoft_hyper-v_server_2012_r2_x64_dvd_2708236.iso"
 $VMMDiskPath = $InstallRoot + "base\SC2012_R2_VMM_EVALVHD.vhd"

$vSwitchName = "Lab_vSwitch"
# Create the Private virtual switch
 $vSwitch = Get-VMSwitch | where Name -eq $vSwitchName
 if (!$vSwitch) {
 New-VMSwitch -Name $vSwitchName -SwitchType Private
 }
# Create folders and virtual hard disks based on parent disks.

# DC01
 md ($InstallRoot + "DC01")
 New-VHD -ParentPath $WinServerDiskPath -Path ($InstallRoot + "DC01\Virtual Hard Disks\DC01.vhdx") -Differencing

# FS01
 md ($InstallRoot + "FS01")
 New-VHD -ParentPath $WinServerDiskPath -Path ($InstallRoot + "FS01\Virtual Hard Disks\FS01.vhdx") -Differencing
 New-VHD -Path ($InstallRoot + "FS01\Virtual Hard Disks\FDISK1.vhdx") -SizeBytes 500GB -Dynamic
 New-VHD -Path ($InstallRoot + "FS01\Virtual Hard Disks\FDISK2.vhdx") -SizeBytes 500GB -Dynamic
 New-VHD -Path ($InstallRoot + "FS01\Virtual Hard Disks\FDISK3.vhdx") -SizeBytes 500GB -Dynamic
 New-VHD -Path ($InstallRoot + "FS01\Virtual Hard Disks\FDISK4.vhdx") -SizeBytes 500GB -Dynamic
 New-VHD -Path ($InstallRoot + "FS01\Virtual Hard Disks\FSSPACE1.vhdx") -SizeBytes 2TB -Dynamic
 New-VHD -Path ($InstallRoot + "FS01\Virtual Hard Disks\FSSPACE2.vhdx") -SizeBytes 2TB -Dynamic
 New-VHD -Path ($InstallRoot + "FS01\Virtual Hard Disks\FSSPACE3.vhdx") -SizeBytes 2TB -Dynamic
 New-VHD -Path ($InstallRoot + "FS01\Virtual Hard Disks\FSSPACE4.vhdx") -SizeBytes 2TB -Dynamic
 New-VHD -Path ($InstallRoot + "FS01\Virtual Hard Disks\SSD01.vhdx") -SizeBytes 127GB -Dynamic
 New-VHD -Path ($InstallRoot + "FS01\Virtual Hard Disks\SSD02.vhdx") -SizeBytes 127GB -Dynamic
 New-VHD -Path ($InstallRoot + "FS01\Virtual Hard Disks\SSD03.vhdx") -SizeBytes 127GB -Dynamic
 New-VHD -Path ($InstallRoot + "FS01\Virtual Hard Disks\SSD04.vhdx") -SizeBytes 127GB -Dynamic

# FS02
 md ($InstallRoot + "FS02")
 New-VHD -ParentPath $WinServerDiskPath -Path ($InstallRoot + "FS02\Virtual Hard Disks\FS02.vhdx") -Differencing
 New-VHD -Path ($InstallRoot + "FS02\Virtual Hard Disks\FDISK1.vhdx") -SizeBytes 500GB -Dynamic
 New-VHD -Path ($InstallRoot + "FS02\Virtual Hard Disks\FDISK2.vhdx") -SizeBytes 500GB -Dynamic
 New-VHD -Path ($InstallRoot + "FS02\Virtual Hard Disks\FDISK3.vhdx") -SizeBytes 500GB -Dynamic
 New-VHD -Path ($InstallRoot + "FS02\Virtual Hard Disks\FDISK4.vhdx") -SizeBytes 500GB -Dynamic
 New-VHD -Path ($InstallRoot + "FS02\Virtual Hard Disks\FSSPACE1.vhdx") -SizeBytes 2TB -Dynamic
 New-VHD -Path ($InstallRoot + "FS02\Virtual Hard Disks\FSSPACE2.vhdx") -SizeBytes 2TB -Dynamic
 New-VHD -Path ($InstallRoot + "FS02\Virtual Hard Disks\FSSPACE3.vhdx") -SizeBytes 2TB -Dynamic
 New-VHD -Path ($InstallRoot + "FS02\Virtual Hard Disks\FSSPACE4.vhdx") -SizeBytes 2TB -Dynamic
 New-VHD -Path ($InstallRoot + "FS02\Virtual Hard Disks\SSD01.vhdx") -SizeBytes 127GB -Dynamic
 New-VHD -Path ($InstallRoot + "FS02\Virtual Hard Disks\SSD02.vhdx") -SizeBytes 127GB -Dynamic
 New-VHD -Path ($InstallRoot + "FS02\Virtual Hard Disks\SSD03.vhdx") -SizeBytes 127GB -Dynamic
 New-VHD -Path ($InstallRoot + "FS02\Virtual Hard Disks\SSD04.vhdx") -SizeBytes 127GB -Dynamic

# SCVMM01
 md ($InstallRoot + "SCVMM01")
 New-VHD -ParentPath $VMMDiskPath -Path ($InstallRoot + "SCVMM01\Virtual Hard Disks\SCVMM01.vhd") -Differencing
 copy $LibraryVHDPath ($InstallRoot + "SCVMM01\Virtual Hard Disks")

# Create and configure Virtual Machines

# DC01
 New-VM -VHDPath ($InstallRoot + "DC01\Virtual Hard Disks\DC01.vhdx") -Generation 2 -MemoryStartupBytes 2048MB -Name DC01 -Path $InstallRoot -SwitchName $vSwitchName
 Set-VMMemory -VMName DC01 -DynamicMemoryEnabled $True -MinimumBytes 1024MB -MaximumBytes 2048MB
 Set-VMProcessor -VMName DC01 -Count 2
 Add-VMNetworkAdapter -VMName DC01 -IsLegacy $False -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName DC01 -IsLegacy $False -SwitchName $vSwitchName

# FS01
 New-VM -VHDPath ($InstallRoot + "FS01\Virtual Hard Disks\FS01.vhdx") -Generation 2 -MemoryStartupBytes 2048MB -Name FS01 -Path $InstallRoot -SwitchName $vSwitchName
 Set-VMMemory -VMName FS01 -DynamicMemoryEnabled $True -MinimumBytes 1024MB -MaximumBytes 4096MB
 Set-VMProcessor -VMName FS01 -Count 2
 Add-VMNetworkAdapter -VMName FS01 -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName FS01 -SwitchName $vSwitchName
 Add-VMHardDiskDrive -VMName FS01 -ControllerType SCSI -Path ($InstallRoot + "FS01\Virtual Hard Disks\FDISK1.vhdx")
 Add-VMHardDiskDrive -VMName FS01 -ControllerType SCSI -Path ($InstallRoot + "FS01\Virtual Hard Disks\FDISK2.vhdx")
 Add-VMHardDiskDrive -VMName FS01 -ControllerType SCSI -Path ($InstallRoot + "FS01\Virtual Hard Disks\FDISK3.vhdx")
 Add-VMHardDiskDrive -VMName FS01 -ControllerType SCSI -Path ($InstallRoot + "FS01\Virtual Hard Disks\FDISK4.vhdx")
 Add-VMHardDiskDrive -VMName FS01 -ControllerType SCSI -Path ($InstallRoot + "FS01\Virtual Hard Disks\FSSPACE1.vhdx")
 Add-VMHardDiskDrive -VMName FS01 -ControllerType SCSI -Path ($InstallRoot + "FS01\Virtual Hard Disks\FSSPACE2.vhdx")
 Add-VMHardDiskDrive -VMName FS01 -ControllerType SCSI -Path ($InstallRoot + "FS01\Virtual Hard Disks\FSSPACE3.vhdx")
 Add-VMHardDiskDrive -VMName FS01 -ControllerType SCSI -Path ($InstallRoot + "FS01\Virtual Hard Disks\FSSPACE4.vhdx")
 Add-VMHardDiskDrive -VMName FS01 -ControllerType SCSI -Path ($InstallRoot + "FS01\Virtual Hard Disks\SSD01.vhdx")
 Add-VMHardDiskDrive -VMName FS01 -ControllerType SCSI -Path ($InstallRoot + "FS01\Virtual Hard Disks\SSD02.vhdx")
 Add-VMHardDiskDrive -VMName FS01 -ControllerType SCSI -Path ($InstallRoot + "FS01\Virtual Hard Disks\SSD03.vhdx")
 Add-VMHardDiskDrive -VMName FS01 -ControllerType SCSI -Path ($InstallRoot + "FS01\Virtual Hard Disks\SSD04.vhdx")

# FS02
 New-VM -VHDPath ($InstallRoot + "FS02\Virtual Hard Disks\FS02.vhdx") -Generation 2 -MemoryStartupBytes 2048MB -Name FS02 -Path $InstallRoot -SwitchName $vSwitchName
 Set-VMMemory -VMName FS02 -DynamicMemoryEnabled $True -MinimumBytes 1024MB -MaximumBytes 2048MB
 Set-VMProcessor -VMName FS02 -Count 2
 Add-VMNetworkAdapter -VMName FS02 -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName FS02 -SwitchName $vSwitchName
 Add-VMHardDiskDrive -VMName FS02 -ControllerType SCSI -Path ($InstallRoot + "FS02\Virtual Hard Disks\FDISK1.vhdx")
 Add-VMHardDiskDrive -VMName FS02 -ControllerType SCSI -Path ($InstallRoot + "FS02\Virtual Hard Disks\FDISK2.vhdx")
 Add-VMHardDiskDrive -VMName FS02 -ControllerType SCSI -Path ($InstallRoot + "FS02\Virtual Hard Disks\FDISK3.vhdx")
 Add-VMHardDiskDrive -VMName FS02 -ControllerType SCSI -Path ($InstallRoot + "FS02\Virtual Hard Disks\FDISK4.vhdx")
 Add-VMHardDiskDrive -VMName FS02 -ControllerType SCSI -Path ($InstallRoot + "FS02\Virtual Hard Disks\FSSPACE1.vhdx")
 Add-VMHardDiskDrive -VMName FS02 -ControllerType SCSI -Path ($InstallRoot + "FS02\Virtual Hard Disks\FSSPACE2.vhdx")
 Add-VMHardDiskDrive -VMName FS02 -ControllerType SCSI -Path ($InstallRoot + "FS02\Virtual Hard Disks\FSSPACE3.vhdx")
 Add-VMHardDiskDrive -VMName FS02 -ControllerType SCSI -Path ($InstallRoot + "FS02\Virtual Hard Disks\FSSPACE4.vhdx")
 Add-VMHardDiskDrive -VMName FS02 -ControllerType SCSI -Path ($InstallRoot + "FS02\Virtual Hard Disks\SSD01.vhdx")
 Add-VMHardDiskDrive -VMName FS02 -ControllerType SCSI -Path ($InstallRoot + "FS02\Virtual Hard Disks\SSD02.vhdx")
 Add-VMHardDiskDrive -VMName FS02 -ControllerType SCSI -Path ($InstallRoot + "FS02\Virtual Hard Disks\SSD03.vhdx")
 Add-VMHardDiskDrive -VMName FS02 -ControllerType SCSI -Path ($InstallRoot + "FS02\Virtual Hard Disks\SSD04.vhdx")

# Hyper-V01
 New-VM -NewVHDPath ($InstallRoot + "HYPER-V01\Virtual Hard Disks\HYPER-V01.vhdx") -NewVHDSizeBytes 107374182400 -Generation 1 -MemoryStartupBytes 2048MB -Name HYPER-V01 -Path $InstallRoot -SwitchName $vSwitchName
 Set-VMMemory -VMName HYPER-V01 -DynamicMemoryEnabled $True -MinimumBytes 1024MB -MaximumBytes 4096MB
 Set-VMProcessor -VMName HYPER-V01 -Count 4
 Set-VMDvdDrive -VMName HYPER-V01 -ControllerLocation 0 -ControllerNumber 1 -Path $HVserverISOPath
 Add-VMNetworkAdapter -VMName HYPER-V01 -IsLegacy $False -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName HYPER-V01 -IsLegacy $False -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName HYPER-V01 -IsLegacy $False -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName HYPER-V01 -IsLegacy $False -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName HYPER-V01 -IsLegacy $True -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName HYPER-V01 -IsLegacy $True -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName HYPER-V01 -IsLegacy $True -SwitchName $vSwitchName

# Hyper-V02
 New-VM -NewVHDPath ($InstallRoot + "HYPER-V02\Virtual Hard Disks\HYPER-V02.vhdx") -NewVHDSizeBytes 107374182400 -Generation 1 -MemoryStartupBytes 2048MB -Name HYPER-V02 -Path $InstallRoot -SwitchName $vSwitchName
 Set-VMMemory -VMName HYPER-V02 -DynamicMemoryEnabled $True -MinimumBytes 1024MB -MaximumBytes 4096MB
 Set-VMProcessor -VMName HYPER-V02 -Count 4
 Set-VMDvdDrive -VMName HYPER-V02 -ControllerLocation 0 -ControllerNumber 1 -Path $HVserverISOPath
 Add-VMNetworkAdapter -VMName HYPER-V02 -IsLegacy $False -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName HYPER-V02 -IsLegacy $False -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName HYPER-V02 -IsLegacy $False -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName HYPER-V02 -IsLegacy $False -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName HYPER-V02 -IsLegacy $True -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName HYPER-V02 -IsLegacy $True -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName HYPER-V02 -IsLegacy $True -SwitchName $vSwitchName

# SCVMM01
 New-VM -VHDPath ($InstallRoot + "SCVMM01\Virtual Hard Disks\SCVMM01.vhd") -Generation 1 -MemoryStartupBytes 2048MB -Name SCVMM01 -Path $InstallRoot -SwitchName $vSwitchName
 Set-VMMemory -VMName SCVMM01 -DynamicMemoryEnabled $True -MinimumBytes 2048MB -MaximumBytes 8192MB
 Set-VMProcessor -VMName SCVMM01 -Count 4
 Add-VMNetworkAdapter -VMName SCVMM01 -IsLegacy $False -SwitchName $vSwitchName
 Add-VMNetworkAdapter -VMName SCVMM01 -IsLegacy $False -SwitchName $vSwitchName
 Add-VMHardDiskDrive -VMName SCVMM01 -ControllerType SCSI -Path ($InstallRoot + "SCVMM01\Virtual Hard Disks\Library.vhdx")

#Start Virtual Machines
 Get-VM * | Start-VM

And Voilà…

HVLab

Until next time, enjoy your day!

Cheers,
Charbel

About Charbel Nemnom 510 Articles
Charbel Nemnom is a Cloud Architect, ICT Security Expert and Microsoft Most Valuable Professional (MVP), totally fan of the latest's IT platform solutions, accomplished hands-on technical professional with over 17 years of broad IT Infrastructure experience serving on and guiding technical teams to optimize the performance of mission-critical enterprise systems. Excellent communicator is adept at identifying business needs and bridging the gap between functional groups and technology to foster targeted and innovative IT project development. Well respected by peers through demonstrating passion for technology and performance improvement. Extensive practical knowledge of complex systems builds, network design, virtualization, and cloud security.

3 Comments

1 Trackback / Pingback

  1. Must Read – April 2014 Edition « The Lazyadmin.com

This site uses Akismet to reduce spam. Learn how your comment data is processed.