Creating virtual machines
You are all set, and it's time to create some virtual machines. To do a rapid deployment of virtual machines, we will rely on PowerShell.
Creating a new virtual machine with PowerShell is easy; just open an elevated PowerShell prompt, and run the following command:
New-VM
Without any additional parameters, this will create a new virtual machine with the default parameters. To create a new Generation 2 VM, run the following command:
New-VM -Generation 2
To create a new virtual machine with a specified name, a custom path to store the VM files, and a memory configuration, run the following command:
New-VM -Name VM01 -Path C:\VM01 -Memorystartupbytes 1024MB
Your newly created virtual machine doesn't have a hard disk yet. Create a new VHDX file by running the following command:
New-VHD -Path C:\vms\vm01\vm01_c.vhdx -SizeBytes 60GB -Dynamic
The VHD cmdlet
The created VHDX is not yet attached to a virtual machine. Do this by running the following command:
Add-VMHardDiskDrive -VMName VM01 -Path C:\vms\vm01\vm01_c.vhdx
To add a network adapter to our virtual machine, run the following command:
Add-VMNetworkAdapter -vmname "VM01" -switchname "external"
Then, start the VM by running the following command:
Start-VM -Name VM01
You will recognize that the virtual machine now has all the basic hardware parameters but fails to boot due to a missing operating system. There are multiple ways to create an operating system for a standard VM. The most granular way to achieve this is using Virtual Machine Manager templates (refer to Chapter 8, Management with System Center and Azure, for details), but there are great capabilities already included in Windows Server 2016. The approach that is seen most often is to manually install the first virtual machine and include everything you want in each of your virtual machines, such as operating system, updates, and backup agents. Then, sysprep the virtual machine by executing sysprep.exe
present at C:\Windows\System32\sysprep\
with the Generalize and OOBE options and shut down the virtual machine. Copy it to a Template
folder and mark this as read only. As of Windows Server 2016, you can even copy and export running virtual machines.
If you need a new virtual machine, just copy the Template
folder, rename it with your machine name and a preinstalled operating system with all your previous created configurations are still available.
If you don't like patching all your images and archived VMs manually, you can use a solution to update these VHD/VHDx files offline with Apply-WindowsUpdate.ps1
-just another gem from the TechNet Gallery (download this from the shortlink, http://bit.ly/1o4sczI).
As you have seen in this chapter, I have mainly used Generation 2 VMs. If your guest operating systems are Windows Server 2012 and higher, this should be your default option. Generation 2 VMs allow faster booting, better stability, and smaller attack surface through a greatly reduced set of legacy hardware.