Search
StarWind is a hyperconverged (HCI) vendor with focus on Enterprise ROBO, SMB & Edge

Specialize Windows Server Hyper-V guest OS automatically

  • February 6, 2017
  • 16 min read
IT and Virtualization Consultant. Romain is specializing in Microsoft technologies such as Hyper-V, System Center, storage, networking, and MS Azure. He is a Microsoft MVP and MCSE in Server Infrastructure and Private Cloud.
IT and Virtualization Consultant. Romain is specializing in Microsoft technologies such as Hyper-V, System Center, storage, networking, and MS Azure. He is a Microsoft MVP and MCSE in Server Infrastructure and Private Cloud.

Last year I have written a topic on Starwind to create VMs from PowerShell. That enables to automate the creation process without using a GUI, either from Virtual Machine Manager or Hyper-V Manager. But a VM deployment is not finished when the VM is created but when the application is deployed. Before deploying the application, the OS must also be installed and specialized. This topic shows you the method I use to deploy and specialize a VM without a single click.

Specialize OS from unattended file

If you read this documentation (Implicit Answer File Search Order section), you can see that we can specialize the OS from unattended file. This unattended.xml file will be placed in C:\Windows\Panther\Unattend. To prepare the unattended file, I use ADK (Assessment Deployment Kit). When you install the ADK, select Deployment Tools. Then you can open Windows System Image Manager.

Windows System Image Manager

Then select File > Select Windows Image and choose the install.wim of your Windows Server. For this example, I have chosen an install.wim from Windows Server 2016 ISO. Select the image you want and click on OK.

Select an image in the Windows image file

Once the image is loaded, you can create an answer file. Click on File > New Answer file. Below you can find the answer file I use:

Microsoft Windows Shell Setup Properties

This unattended.xml file set the following element:

  • The timezone
  • The product key (AVMA Key)
  • An autologon which can run once time (to run a script automatically). The following script will be run the first time: C:\Windows\Panther\Unattend\ConfigureOS.ps1
  • The system language:
    • Keyboard layout: French
    • UI and system language: en-US
  • The PnP devices are not uninstalled during generalize pass. Because VMs have always the same hardware, I set this setting to save time during specialization

I save this with the following name: unattend.xml

PowerShell script for specific settings

I have written the bellow script which is run on the first logon on the server. Because the unattended file set the autologon once a time, this script is automatically run at the deployment.

This script takes settings from a XML file which I configure for each VM. This script set the following:

  • Rename network adapters (requires device naming enabled on vNIC)
  • Set IP Addresses with the configuration specified in XML file
  • The machine join the domain
  • xml and configuration xml are removed because sensitive information are stored in these files.
  • The VM is rebooted

I copy the above PowerShell script in the VHDX master here: c:\Windows\Panther\Unattend\ConfigureOS.ps1. Below you can find the XML file

As you can see, this file contains a sensitive information. This is why I leave the field password empty and I fill this information when I need it. The above XML file is copied in the VHDX master here: c:\windows\panther\Unattend\ConfigureOS.xml

Prepare the VHDX master

To prepare a VHDX Master, I have deployed a Windows Server 2016 Standard Core VM. Then I have applied the updates. To finish I have run the following command:

Then I have copied the VHDX in a repository. Next, mount the VHDX:

mount the VHDX

To finish, copy the unattend.xml, the ConfigureOS.ps1 and ConfigureOS.xml into the windows\panther\Unattend

ConfigureOS

Now unmount the VHDX and create a VM. You can copy the VHDX and attach it to the VM.

attaching VHDX to the VM

Test the OS deployment

Now the VHDX is attached to the VM, you can run the VM. Below you can find the screenshot of the VM configuration. First the VM apply the unattend.xml file:

Hyper-V Virtual Machine Connection

Then Windows boot and the account Administrator is logged automatically

Hyper-V preparing first boot

Then the Powershell script is automatically executed

PowerShell script for Virtual Machine Connection

At next logon, I’m able to connect with Active Directory account

Virtual Machine Connection view

The IP address, network adapter name and computer name are well set:

 Hyper-V preparing first boot ip configuration

The computer is placed in the right Active Directory OU:

Active Directory Users and Computers

And to finish, the sensitive files are deleted:

Windows commandlet

If I want delete the ConfigureOS.ps1 script after deployment, I can create a GPO to make this. Just create a Files GPP as below:

Group Policy Managment Editor

Conclusion

Thanks to unattended file, you can customize OS and run specific PowerShell scripts. With this feature, you can specialize your OS from the deployment. Thanks to this method, you just have to deploy your application after the VM deployment.  Virtual Machine Manager uses the same mechanism to specialize the OS after the VM creation. The automatic OS configuration is really appreciable to get a consistent environment. So why not use it?

 

 

 

 

 

Found Romain’s article helpful? Looking for a reliable, high-performance, and cost-effective shared storage solution for your production cluster?
Dmytro Malynka
Dmytro Malynka StarWind Virtual SAN Product Manager
We’ve got you covered! StarWind Virtual SAN (VSAN) is specifically designed to provide highly-available shared storage for Hyper-V, vSphere, and KVM clusters. With StarWind VSAN, simplicity is key: utilize the local disks of your hypervisor hosts and create shared HA storage for your VMs. Interested in learning more? Book a short StarWind VSAN demo now and see it in action!