Prepare Hyper-V template of Windows Server 2016/2019
Deploying a new server is always fun! Well, at least the part after ISO download, installation, patching. Having said that, why not create a template so that you jump right into the fun?
So we are essentially going to create a preconfigured VHDX file that can be attached to the newly created VM, and it will take you straight to the login screen.
- Windows server or Windows 10 with Hyper-V role and management tools enabled
- Windows Assessment and Deployment Toolkit (ADK) – I always pick the latest version available, at the moment of writing this – Windows 10, version 1903 – is latest.
- ISO with the source – I’ve used Server for 2016/2019 from my.visualstudio.com (you need an MSDN subscription for this), or from trial center
Open Windows PowerShell as admin and paste this command:
Install-WindowsFeature -Name Hyper-V -IncludeAllSubFeature -Verbose
You will need to reboot your computer for hyper-v to become available
Install External switch
If you want to allow your template VM to connect to the Internet e.g. To download and install patches, you need an external switch set up on Hyper-V.
Open Hyper-V Manage and choose “Virtual Switch Manager” from the right-hand side pane,
Make sure you highlight the external switch and click Create Virtual Switch
On the next page give it a name, specify which network card connect it to and confirm.
Or use Powershell (it will attach VM switch to the first network adapter):
$A = Get-NetAdapter New-VMSwitch -Name "ext1" -NetAdapterName $a.name
Go to the link and download the latest ADK. When installing, make sure “Deployment tools” is selected – we are after Windows System Image Manager (SIM).
Files extraction from ISO
We need to extract install.wim file from the installation media. The file is a compressed Windows Image and is being used by SIM to create the answer file.
Double click on the ISO so that it mounts, go to Mounted ISO\Sources and copy install.wim (it has a few gigabytes in size, biggest file in the folder) to your work directory.
You can unmount the ISO now, by right-clicking on it and choosing Unmount.
At this stage, we have everything we need to work.
Create VM and install from ISO
Head to the Hyper-V Manager and choose to create a new VM, and options you choose:
Name: Something meaningful like e.g. Template – Server 2019 Standard GUI
Generation: Generation 2
Assign Memory: Anything above 2048MB should be enough
Configure networking: If you’ve created the external switch, pick it here
Virtual Hard Disk: You can change default location and size. By default it will be dynamically expanding, that means it will only take as much space as needed.
Installation Options: point to the ISO
On summary click finish.
What you can do at the stage is to give it more CPUs, 2 at least. It’s all about speed.
Start your VM and prepare it as you wish
Now you can customise your server installation as you wish. Since it’s a server, I have tendency to only install updates, with the principal the less the better. But you can do whatever you’re fancy.
Once you’re finished, take a checkpoint, as we are going to test if syspreping works.
Press Windows + R and run:
This will launch the System Preparation tool, aka sysprep.
Configure it as:
IF you haven’t taken the checkpoint when I’ve asked last time, do it now.
Checkpoint taken? Great, press ok and let the system reboot.
What will happen now is Windows will remove any unique identifiers, enabled drivers etc. Making it neutral.
Once your sysprep is completed, you’ll see a standard installation questions like language and administrator password – this is expected. At this stage, we are only testing if whatever changes you’ve made so far, don’t interfere with the sysprep process.
The successful test is considered when you can log in to Windows.
Roll back the VM to the checkpoint you’ve taken before trying sysprep.
Prepare the answer file
Now we can move to a so-called answer/unattend file. The purpose of the file is to “answer” the installation questions for you, thus if the installer can find all answers, it will simply get you to the login screen after the first run.
Open SIM, choose File > New Answer File > Point it to the install.wim file you’ve copied earlier
In the window say Yes
Choose the Windows edition you’d like to install and in following, windows say Yes
SIM will now start creating a catalogue with all possible options for the image, this process can take good 10 minutes. Just let it work.
When completed, you will see windows like that:
And we are after to panes here: Answer File and Windows image.
- Windows Image is where you choose what to add to and to which phase of OS deployment
- The answer file is where you configure specific answers
For start you’d like to add components:
- amd64_Microsoft-Windows-International-Core, to Pass 4 Specialize and Pass 7 OOBE system
- amd64_Microsoft-Windows-International-Core, to Pass 7 OOBE System
You do this by right-clicking the component Windows Image pane and choose the phase you’d like to add. There are many very similarly named records, thus pay attention.
Once added, you will see three “empty boxes” in the Answer File section.
Let’s configure amd64_Microsoft-Windows-International-Core first, in both phases:
By selecting one, you enter its values on the right-hand side pane Properties.
You’d like to fill three fields: InputLocale, SystemLocale, UILanguage – in my example below you can see set up for British language. How can you find out all possible combinations? When you right-click the components in Answer File and choose Help it will take you to the website which enlists all values/language you can insert into these fields.
Time for amd64_Microsoft-Windows-Shell-Setup in here we need to values:
In the root\OOBE amend the record of HideEULAPage to True
And in root\UserAccounts\AdministratorPassword enter the password for the Administrator account.
After finishing it should look like this:
Press Save > Save Answer File to save the file. We are going to use it for sysprep.
Sysprep with unattended file
Copy the answer file to the desktop, and take another checkpoint of your VM.
Open shell as administrator and issue command (My username is Administrator and file is called Unattend.xml):
Select OOBE, Generalize and Reboot
What will happen now the system will reboot and after a few minutes you should be welcomed by a login prompt (thus no language, licence, admin password questions) – if so we’re good to export the template for future deployment.
If any questions appear during the deployment phase, double-check your answer file if you’ve included all answers as suggested above.
All right, revert your VM to the last checkpoint. You can rename that check as “Before sysprep” – so that you can always comeback to it and make amendments to your image, or install new updates.
Issue same command once again:
But this time choose to shutdown the VM.
When the VM is shutdown, take another snapshot and call it something like “Export this” and export this checkpoint!
Exporting a checkpoint will effectively create a VM with merged VHDX file, and it’s VHDX file we need. All you need to do now is to go to the directory where you’ve exported the VM, and copy VHDX file from it to some place where you store your templates.
Now whenever you create a new VM, rather than pointing it to the ISO and creating a blank hard disk, simply make a copy of your template and attach that disk to VM.
I’ve got my template, what now?
Since you’ve got the checkpoint of VM before syspreping, you can always bring install new updates, software etc.
You can use scripting to create new VMs, with the use of the VHDx, thus making it the fully automatic solution.
If the post-deployment configuration is required, but you can’t have this on the golden image (e.g. AV software, management agent) you can use setupcomplete.cmd file.
If you’re interested in creating Windows 10 image, read this article.