Skip to content

Instantly share code, notes, and snippets.

@phillhocking
Last active July 4, 2023 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phillhocking/40d968e5448254a34096c7f35f34137d to your computer and use it in GitHub Desktop.
Save phillhocking/40d968e5448254a34096c7f35f34137d to your computer and use it in GitHub Desktop.
Microsoft Windows Server 2019 Standard - Golden Image
# provision2019DC.ps1
# Flat Server 2019 1909.4 install to use in creating other 2019 images with Boxstarter
# 1.3 - 5/3/2020
# Author: Phillip Hocking phillhocking@gmail.com
#
# Purpose: This script provisions a base install Microsoft Windows Server 2019 VM Gen 2 Hyper-V instance for the purpose
# of further configuring as a PDC (Primary Domain Controller) or AAD DS (Azure Active Directory Domain Services) connector.
#
# Caveats: When the Hyper-V bootloader comes up it will say that boot timed out because the amount of time it takes for the
# Hyper-V connection terminal to connect is longer than the amount of time it gives to you 'press any key to install from cd'
# you just have to give focus to the window and hit space bar or enter for it to actually run the 2019 install
#
############################# Variable Declarations ###################################
$HostName = 'hypervhost.fqdn'
$VMName = 'DC'
$VHDName = '{0}.vhdx' -f $VMName
$vSwitchName = 'vSwitch 1 - My vSwitch'
$vCPUs = '4'
$VMStoragePath = 'H:\Hyper V'
$VHDStoragePath = Join-Path -Path 'H:\Hyper V' -ChildPath $VHDName
$WinServerISOPath = 'H:\ISOs\SW_DVD9_Win_Server_STD_CORE_2019_1909.4_64.iso'
############################# Command Execution Starts Below ###################################
New-VM -Name $VMName -MemoryStartupBytes 8GB -SwitchName $vSwitchName -BootDevice CD -Path $VMStoragePath -Generation 2 -NoVHD
# Generation 2 Hyper-V VMs utilize UEFI that sanity checks install media; Secure Boot must be enabled or ISO will not boot.
# This also ensures image has not been tampered with and should not be disabled
Set-VMFirmware $VMName -EnableSecureBoot On -FirstBootDevice DVDDrive
# Specify the amount of vCPUs to provision
Set-VMProcessor -VMName $VMName -count $vCPUs
# Set Vlan 10 - inside of VM use 10.0.0.1/24
Add-VMNetworkAdapter -VMName $VMName -SwitchName $vSwitchName
Set-VMNetworkAdapterVlan $VMName -Access -VlanID 10
# Provision the Virtual Hard Disk (not really hard then, is it?)
New-VHD -Path $VHDStoragePath -SizeBytes 40GB -Dynamic -BlockSizeBytes 1MB
# While this is dynamic, it won't just grow on its own, however, growing volumes is easier than shrinking
Add-VMHardDiskDrive -VMName $VMName -ControllerType SCSI -ControllerNumber 0 -ControllerLocation 1 -Path $VHDStoragePath
# Mount Windows Server ISO for the DVD drive and start the provisioned VM
Set-VMDvdDrive -VMName $VMName -Path $WinServerISOPath
Start-VM -ComputerName $HostName -VMName $VMName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment