Skip to content

Deploying a HyperV VM, with a Windows Server 2019, with a custom ISO

Notifications You must be signed in to change notification settings

Captn138/hyperv-terraform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hyperv-terraform

Deploying a HyperV VM, with a Windows Server 2019, with a custom ISO

Table of Contents

Prerequisite

Execution policy and Hyper-V

This will allow us to run PowerShell scripts.
Open an Administrator PowerShell, then type in

# Allow execution of PowerShell scripts
Set-ExecutionPolicy Unrestricted

# Install all Hyper-V additional features
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All

# Enable remote control for Hyper-V
Enable-PSRemoting -SkipNetworkProfileCheck -Force
Set-WSManInstance WinRM/Config/WinRS -ValueSet @{MaxMemoryPerShellMB = 1024}
Set-WSManInstance WinRM/Config -ValueSet @{MaxTimeoutms=1800000}
Set-WSManInstance WinRM/Config/Client -ValueSet @{TrustedHosts="*"}
Set-WSManInstance WinRM/Config/Service/Auth -ValueSet @{Negotiate = $true}

Select All scripts.
Wait for the installation of the Hyper-V modules. You will probably need to reboot after that.

Windows Server 2019 stock ISO

You can download it from here : https://www.microsoft.com/en-us/evalcenter/evaluate-windows-server-2019
Direct download link (fr-fr) : https://software-download.microsoft.com/download/pr/17763.737.190906-2324.rs5_release_svc_refresh_SERVER_EVAL_x64FRE_fr-fr_1.iso

Terraform software

This will install Chocolatey and Terraform.
Open an Administrator PowerShell, then type in

# Install Chocolatey
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

# Install Terraform
choco install Terraform -y

MSMG Toolkit

You can download it from here : https://www.majorgeeks.com/files/details/msmg_toolkit.html
Extract it using 7zip.

Windows Assessment and Deployment Kit

You can download it from here : https://docs.microsoft.com/en-us/windows-hardware/get-started/adk-install
Direct download link : https://go.microsoft.com/fwlink/?linkid=2120254
Execute adksetup.exe and unselect everything unless Deployment tools. Install.

Customize

Customize ISO

Auto Unattended XML file

An Auto Unattended XML file allows installing Windows with predefined values. In my case, I wanted a fully automated installation of Windows, as well as some custom predefined values.

  1. Place your ISO file in the ISO folder in MSMG Toolkit folder.
  2. Execute Start.cmd in the MSMGT folder (Administrator privileges required).
  3. Accept the EULA by pressing A.
  4. Extract the ISO file : Go to Source > Extract source from DVD ISO Image by typing 1 3 and type in the name of your ISO file.
  5. You can close MSMGT by typing X. Open your startup menu, scroll to Windows Kits and open Windows System Image Manager.
  6. Click on File > Select Windows Image, navigate to MSMGT > DVD > sources > install.wim. Select your version of Windows to customize. It will ask to build a catalog. Accept (it is a long process, Administrator privileges required).
  7. Click on File > New response file.
  8. Customize your response file. You can find mine in this git repo (Administrator's password : p@ssword1234) or you can check some documentation here : https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/automate-windows-setup or https://www.windowscentral.com/how-create-unattended-media-do-automated-installation-windows-10 .
  9. Save your response file as autounattend.xml and place it in MSMGT > DVD.
  10. Close the Windows system image manager and delete the MSMGT > DVD > sources > Install_Windows XXX.clg file, or move it elsewhere to avoid rebuilding it.

Setup Complete CMD script

  1. Navigate to MSMG > DVD > sources.
  2. Create the $OEM > $$ > Setup > Files and $OEM > $$ > Setup > Scripts folders.
  3. Create a new SetupComplete.cmd file in the Scripts folder.
  4. Fill it using the following template :
@echo off
<YOUR_COMMANDS_HERE>
rd /q /s "%WINDIR%\Setup\Files"
del /q /f "%0"
  1. Replace the second line by your custom cmd commands. I suggest creating a PowerShell script file in the Files folder and calling it using powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass "%WINDIR%\Setup\Files\myscript.ps1". I have added my post.ps1 script in this git repo. It automatically creates an AD forest and domain controller, as well as fixing the IP address. I strongly advise you to only use this file as an example, as I use custom values for an IP network and AD forest.

Skip user input on boot time

By default, when we boot on the Windows ISO image, we will be asked to Press any key to boot from CD or DVD. We can skip that by going to MSMGT > DVD > efi > microsoft > boot and delete efisys.bin and cdboot.bin, then rename both their counterparts (efisys_noprompt.bin and cdboot_noprompt.bin) as the files we just deleted.

Building the new ISO

  1. Open MSMGT : MSMGT > Start.cmd (Administrator privileges required).
  2. Accept the EULA by pressing A.
  3. Build the new ISO file : Go to Target > Make a DVD ISO Image by typing 6 1 and type in the label and name of your ISO file to be created.
  4. Your ISO file will be located in MSMG > ISO.

Customize Terrfaorm variables

To modify the default variables values, create a .tfvars file and set new varaibles values. See https://www.terraform.io/docs/language/values/variables.html.

Deploy

To run the script, (no Administrator privilege is required) run

.\run.ps1

Destroy

To destroy the VM, run

.\destroy.ps1

About

Deploying a HyperV VM, with a Windows Server 2019, with a custom ISO

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published