Setup a Lab Environment using Windows 8 & Hyper-V


I was looking for a good way to deploy a lab environment using Hyper-V on Windows 8. One of the things I wanted was that my machines need to interact with the host and that they need to connect to the internet. The other thing I wanted was that not to create machines manually.

After searching the internet I didn’t find a single blog or info that fulfilled my needs. So I took every piece of information that I needed and started to build out my lab environment.

I will try to explain how I build my lab environment by taking you through every step I took to build it.

These are the steps I followed in order to build the test lab are:

  1. Configure Hyper-V Networking settings.
  2. Create a base template of the OS needed.
  3. Create the virtual machines via PowerShell.
  4. Using a router.

Configure Hyper-V Networking settings

Before we can begin configuring Hyper-V, I assume that Hyper-V is already added to your Windows 8 machine.

In order to communicate with the virtual machines you want to create from your host and the communication between the machines it is necessary to configure the Hyper-V networking settings.

  1. Start Hyper-V Manager and go to the Virtual Switch Manager.
  2. Configure your network cards (Wired and WiFi) and configure an internal only. Do not forget to check:

  1. When you’re done, the settings should be similar to mine:

 

Create a base template of the OS needed for your deployment

The steps involved are:

  1. Create a Virtual Machine with the OS you will use to build your LAB.
  2. When the machine is installed, run Windows Update to patch the OS.
  3. Run sysprep, you can find it under C:\WINDOWS\SYSTEM32\SYSPREP\SYSPREP.EXE.
  4. Configure sysprep to do an OOBE and check the Generalize button! Choose Shutdown.
  5. Copy the VHD(X) to your template folder e.g. X:\VM\Template and rename it.

The steps are slightly different than described in this blog that I used as a source.

 

Create Virtual Machines via PowerShell

If you’re building a Lab, you will use multiple machines, when creating them through the Hyper-V manager is a lot of clicking. My goal for this part was to have a basic script that I could use not only to build my basic machines, but also to add additional servers and not to worry about the machines that where already created. Searching the internet I came across this blog where a Microsoft Employee has written a script to create virtual machines from a template. When testing the script I felt the need to add additional lines in order to fulfill my goal.

The PowerShell script I use:

 Import-module Hyper-V

$LABVMs =@(‘DC01′;‘DC02′;‘EX01′;‘SPS01′;‘ADFS’;‘SQL01′;‘SQL02′;‘LYNC’)
$VMTemplate = "D:\VM’s\Template\Template.vhdx"
$LabPath = "D:\VM’s\LAB"
$Switch = "Internal-Switch"
$Memsize = 1024MB
$VMGen = "New"
 
 Foreach ($LABVM in $LABVMs) {
 
 	$VHD = Get-VHD -Path "$LabPath\$LABVM\$LABVM.vhdx" -ErrorAction SilentlyContinue
	Write-Host "Creating VHD for VM $LABVM..." -NoNewline
	If ($VHD -eq $null) {
		New-VHD -ParentPath $VMTemplate -Path "$LabPath\$LABVM\$LABVM.vhdx" -Differencing | Out-Null
		Write-Host -ForegroundColor Green " - Done."
	} else {
		Write-Host -ForegroundColor Gray " - Already Created"
	}
	
	$VM = Get-VM -Name $LABVM -ErrorAction SilentlyContinue
	If ((Get-WMIObject win32_OperatingSystem).Version -match "6.2.92") {
		Write-Host "Creating VM $LABVM..." -NoNewline
		If ($VM.Name -ne $LABVM) {
			New-VM -VHDPath "$LabPath\$LABVM\$LABVM.vhdx" -VMName $LABVM -MemoryStartupBytes $Memsize -SwitchName $Switch | Out-Null
			Write-Host -ForegroundColor Green " - Done."
		} else {
			Write-Host -ForegroundColor Gray " - Already Created"
		}
	} elseIf ((Get-WMIObject win32_OperatingSystem).Version -match "6.3.9431") {
		Write-Host "Creating VM $LABVM..." -NoNewline
		If ($VM.Name -ne $LABVM -and $VMGen -eq "Old") {
			New-VM -VHDPath "$LabPath\$LABVM\$LABVM.vhdx" -VMName $LABVM -MemoryStartupBytes $Memsize -SwitchName $Switch | Out-Null
			Write-Host -ForegroundColor Green " - Done."
		} elseif ($VM.Name -ne $LABVM -and $VMGen -eq "New") {
			New-VM -VHDPath "$LabPath\$LABVM\$LABVM.vhdx" -Name $LABVM -MemoryStartupBytes $Memsize -SwitchName $Switch | Out-Null
			Write-Host -ForegroundColor Green " - Done."
		} else {
			Write-Host -ForegroundColor Gray " - Already Created"
		}
	}
	
	Write-Host "Starting VM $LABVM..." -NoNewline
	If ($VM.State -ne "Running") {
		Start-VM -Name $LABVM
		Write-Host -ForegroundColor Green " - Done."
	} else {
		Write-Host -ForegroundColor Gray " - Already Running"
	}
 }

Save the script as Create-Lab.ps1. Start a PowerShell window with Run As Administrator and don’t forget to alter the execution policy if needed!

Using a Router

 

Create the VM for the Router

For the router I will use DD-WRT.

In order to create a router for connection between the host and the virtual machines we need to create these steps:

  1. Create a VHD on the Host Machine
    1. Open Disk management and create a new VHD that is 50mb, fixed size. Make a note of the disk number, we need it later!
  2. Download the necessary software
    1. Download physdiskwrite from http://m0n0.ch/wall/physdiskwrite.php
    2. Download the latest DD-WRT image for x86: http://www.dd-wrt.com/routerdb/de/download/X86/X86///dd-wrt_public_vga.image/3744. Or Search the router database at www.dd-wrt.com for x86.
  3. Installing the DD-WRT software
    1. Open a command prompt with administrative privileges and run the following command: Physdiskwrite –u dd-wrt_public_vga.image
    2. It will prompt you to put the disk number in. This will erase the entire drive, so double check your disk number.
    3. Once this process is complete, dismount the VHD from the host machine.
  4. Create the VM
    1. Create a new VM. Set the memory to 32mb and NOT connected to a network.
    2. Open the settings of the new VM and remove the Network adapter and add 2 new Legacy Network adapters.
    3. Connect one of the Legacy network adapters to the internal network. Leave the other disconnected.
    4. Boot the VM.

 

Test the configuration

To check if the router is functioning test the configuration from another VM:

  1. From any VM on the internal network, set it to use DHCP.
  2. The router should give you a 192.168.1.x address. If you don’t get one, try switching the Internal network to the other legacy adapter in the VM configuration.
  3. Once you get an address, open http://192.168.1.1 – if you get a web page, you did it correctly!
  4. Connect the second VM Legacy network adapter to the external network (pick the one that is currently active).
Note:
As soon as the second interface (WAN) is enabled, the router seems to restart his routing engine (or whatever there’s under the hood in this Linux thingy J). And by doing that it changes the interfaces. So WAN becomes LAN and vice versa. After that you cannot connect to the admin web page.

Change the virtual switches in the legacy network adaptors and you should be fine.

Enable the WAN port:

Enable the WAN on the settings page. From there you should have a fully functional router/firewall in your environment.

I use port forwarding to allow RDP (port 3389) to one of my VMs.

This solution provides great isolation, internet access, and management access from your host machine.

Source for setting up the DD-WRT Router: http://blogs.technet.com/b/askpfeplat/archive/2013/03/04/your-personal-isolated-lab-featuring-windows-8-hyper-v.aspx?PageIndex=2#comments

Advertisements

One thought on “Setup a Lab Environment using Windows 8 & Hyper-V

  1. When I run the script I am getting errors in the coding…is this an encoding issue when copying from the web?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s