If you’re anything like me, you build up and tear down your lab on a regular basis. You might do this because you’re studying, you might want to try out new ways of doing things, or you might just want to flex your buid muscles while you’re in an operations period to keep your skills fresh. I run a local lab on my home desktop, and found myself tearing it down quite regularly, so I decided it was time to script, streamline and document the process as much as possible.
In this first entry of this series, we’ll do the groundwork for the Lab. We’ll install Hyper-V, create a template disk which can be “cloned” for future builds – all machines will use differencing disks so we can keep the footprint of our lab to an absolute minimum.
The first machine will be a Windows 2012 R2 Server Core install, which is as lean as you can get. We’ll also run a windows update from the comandline, and finally we’ll sysprep the machine in preparation for cloning.
In future entries in this series, we’ll make a child differencing disk and install Server GUI, which will then become the parent disk for any machines which require it, we’ll create an Active Directory, a Lync 2013 Enterprise pool, and maybe a few more goodies.
First, I’ll assume that you’re using a desktop version of Windows 8 or above, and that you’re a local administrator. Hyper-V on Windows 8 includes full PowerShell integration, which is fairly critical if you want to manage your VMs with PowerShell. Fire up an administrative PowerShell session on your workstation and enter the following (note that your machine will automatically restart once it’s done):
Install-WindowsFeature Hyper-V -Restart
This cmdlet is the equivalent of opening your control panel, navigating to Programs>Turn Windows features on and off, and ticking the Hyper-V box (but much quicker). When your machine is back up, you can start Hyper-V manager from the Start screen and see a lovely blank interface with no VMs. Next, we’ll need to configure networking.
Hyper-V uses virtual switches so you can do all sorts of funky segregation, vlanning and whatnot. In a lab environment this isn’t generally necessary, but you do still need to create a simple virtual switch so you can connect your machines to your home network. Again, open an administrative PowerShell and run the following cmdlet:
PS C:Windowssystem32> Get-NetAdapter
Name InterfaceDescription ifIndex Status MacAddress LinkSpeed ---- -------------------- ------- ------ ---------- --------- Ethernet Realtek PCIe GBE Family Controller 3 Up 50-E5-49-E1-28-C0 1 Gbps
Copy the name of your computer’s NIC (in this case it’s “Ethernet,” but yours could be a wireless connection or something weird), then issue the following cmdlet:
PS C:Windowssystem32> New-VMSwitch "HomeNetwork" -NetAdapterName "Ethernet"
Name SwitchType NetAdapterInterfaceDescription ---- ---------- ------------------------------ HomeNetwork External Realtek PCIe GBE Family Controller
Now when I create a new virtual machine, I can add a network interface which connects to this switch and the machine will be able to see my internal network.
Creating a Virtual Machine Template
Well, it’s not technically a template, I suppose, but the first thing which is necessary in building a repeatable lab is creating a disk with Windows Server 2012R2 installed and sysprepped, which can then be used as the parent for differencing disks for all our other machines. If you were to just copy a virtual disk and try to create a new machine, you’d have troubles starting both the original machine and the cloned machine at the same time because their Security ID (SID) would be identical. Sysprepping allows a full reset of the SID so every new cloned disk (or differencing disk, in our case) will come up as a brand spanking new machine.
You can use Hyper-V Manager and go through the “New Virtual Machine” wizard, creating a dynamically expanding disk and mounting your Windows DVD along the way, or you can run the following commands in your administrative PowerShell. Before you run them, have a look at the paths and make sure that the disk is going to be created where you want it, and the Windows DVD path is correct.
# Define where you want the VHD to be created and create it New-VHD –Path "C:Hyper-VVirtual Hard DisksCoreTemplate.vhdx" -Dynamic -SizeBytes 80Gb # Create the Server and attach the VHD New-VM –Name CoreTemplate –MemoryStartupBytes 512MB -BootDevice VHD -SwitchName HomeNetwork -Generation 2 Add-VMHardDiskDrive -VMName CoreTemplate -path "C:Hyper-VVirtual Hard DisksCoreTemplate.vhdx" -ControllerType SCSI -ControllerNumber 0 -ControllerLocation 0 # Add the DVD drive and mount the ISO - Make sure the path is correct for where your Windows DVD ISO is stored Add-VMDvdDrive -VMName CoreTemplate -Path "C:DVDswin_2012_r2.iso" # Set the DVD drive as the boot device $bootdisk = Get-VMDvdDrive -VMName CoreTemplate Set-VMFirmware -vmname CoreTemplate -BootOrder $bootdisk # Fire it up start-vm -Name CoreTemplate
Once executed, a new machine will appear in Hyper-V manager called “CoreTemplate,” which has 512Mb of memory, an 80Gb dynamically expanding hard disk, has the Windows DVD mounted (and set as the boot device), and is connected to your internal network. It’ll then automatically start itself – double click on the machine in Hyper-V Manager to open a console and mash some keys to boot from the DVD.
The first machine will be a Server Core installation, which may be new for some. If you don’t like the idea of doing a lot of stuff on the command line or in PowerShell, you can skip this and go ahead with a full Windows Server install so you’ve got a GUI to play around in.
Don’t worry about the languages, as when you sysprep the machine it’ll lose this setting anyway. Just hit Next.
Enter your Windows key:
Select Server Core:
Agree to the license terms:
Choose a custom install:
Select your hard disk:
The server will reboot when finished:
You’ll be prompted to change your administrator password:
Type one in and hit enter:
Your password has been accepted:
Welcome to your new Server Core install!
From the administrator command prompt, execute sconfig:
Choose option 6 – “Download and Install Updates”
You’ll be asked if you want to install all updates, or just the recommended ones. I usually choose all, but feel free to stick with the recommended ones if that’s your preference:
All the updates available will be listed. Type A and press enter to download all updates:
The updates will download and install automatically, then prompt you to reboot once they’re complete:
Sysprep the machine to create a parent disk
When the server comes back up after installing updates, log in as administrator and enter the following command:
c:windowssystem32sysprepsysprep.exe /generalize /shutdown /quiet
You’ll get a dialog box to confirm this is what you want to do (which for the life of me I can’t understand, considering I’ve already chosen the generalize and shutdown options, but that’s a job for another day). Check the Generalize box and hit OK.
Once complete, Sysprep will shut the machine down. At this point, you can delete the virtual machine from Hyper-V manager, as we won’t be needing it any more.
In Windows Explorer, browse to the location where you stored the virtual hard disk file, for example C:Hyper-VVirtual Hard Disks, locate the disk we created during the installation, right click and select properties. We’re going to set this base disk as Read Only, as we don’t want any of the differencing disks down the line to make any changes to it, otherwise we’ll be in a world of hurt:
And that’s it for this part of the series. Next time we’ll create a GUI disk, create our first machine and install Active Directory. Maybe WSUS too. We’ll see.