Deploying a HyperV VM, with a Windows Server 2019, with a custom ISO
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.
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
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
You can download it from here : https://www.majorgeeks.com/files/details/msmg_toolkit.html
Extract it using 7zip.
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.
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.
- Place your ISO file in the
ISO
folder in MSMG Toolkit folder. - Execute
Start.cmd
in the MSMGT folder (Administrator privileges required). - Accept the EULA by pressing
A
. - Extract the ISO file : Go to
Source > Extract source from DVD ISO Image
by typing1 3
and type in the name of your ISO file. - You can close MSMGT by typing
X
. Open your startup menu, scroll toWindows Kits
and openWindows System Image Manager
. - Click on
File > Select Windows Image
, navigate toMSMGT > 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). - Click on
File > New response file
. - 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 . - Save your response file as
autounattend.xml
and place it inMSMGT > DVD
. - 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.
- Navigate to
MSMG > DVD > sources
. - Create the
$OEM > $$ > Setup > Files
and$OEM > $$ > Setup > Scripts
folders. - Create a new
SetupComplete.cmd
file in theScripts
folder. - Fill it using the following template :
@echo off
<YOUR_COMMANDS_HERE>
rd /q /s "%WINDIR%\Setup\Files"
del /q /f "%0"
- Replace the second line by your custom cmd commands. I suggest creating a PowerShell script file in the
Files
folder and calling it usingpowershell -NoProfile -NonInteractive -ExecutionPolicy Bypass "%WINDIR%\Setup\Files\myscript.ps1"
. I have added mypost.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.
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.
- Open MSMGT :
MSMGT > Start.cmd
(Administrator privileges required). - Accept the EULA by pressing
A
. - Build the new ISO file : Go to
Target > Make a DVD ISO Image
by typing6 1
and type in the label and name of your ISO file to be created. - Your ISO file will be located in
MSMG > ISO
.
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.
To run the script, (no Administrator privilege is required) run
.\run.ps1
To destroy the VM, run
.\destroy.ps1