New-VM Template: Gen2 SecureBoot Linux

The following is the code we use to add a Secure Boot based Linux virtual machine to one of our Azure Stack HCI/Storage Spaces Direct clusters.

All $Variables are able to be tweaked for a given setting. In this case, we are setting up and deploying to a Azure Stack HCI/Storage Spaces Direct cluster in Hyper-V.

Make sure to tweak the path for Cluster Shared Volume or standalone drive and folder structure.

NOTE: # TODO <– Visual Studio Code Extension: Better Comments

# New-VM $VMName
$VMName = "Our-Ubuntu"
$VMMemory = 4GB
$vCPUCount = 4
$VMDiskSize0 = 40GB
$vSwitch = "SETvSwitch"
$DriveLetter = "C:"
$CSVFolder = "CSV-99 Ubuntu"
$ISO = "\\MgmtServer\ISOs\ubuntu-18.04.4-live-server-amd64.iso"
$AutoMaticStartDelay = "120"
$VLANID = 11

# TODO Create the VM
New-VHD -Path "$($DriveLetter)\ClusterStorage\$($CSVFolder)\$($VMName)_D0.VHDX" -Dynamic -SizeBytes $VMDiskSize0
New-VM -Name $VMName -Generation 2 -Memory $VMMemory –SwitchName "$vSwitch"
Set-VMProcessor $VMName -Count $vCPUCount
Add-VMHardDiskDrive -VMName $VMName -Path "$($DriveLetter)\ClusterStorage\$($CSVFolder)\$($VMName)_D0.VHDX"
Add-VMDvdDrive -VMName $VMName -ControllerNumber 0 -ControllerLocation 63
Set-VMDvdDrive -VMName $VMName –Path $ISO
Set-VM –Name $VMName –AutomaticStartAction Start -AutomaticStartDelay $AutoMaticStartDelay -AutomaticStopAction ShutDown

# Add to a Hyper-V/S2D Cluster
Add-ClusterVirtualMachineRole -VirtualMachine $VMName

# TODO Linux VM SecureBoot
Set-VMFirmware -VMName $VMName -EnableSecureBoot On -SecureBootTemplate 'MicrosoftUEFICertificateAuthority'

# Set the VM's VLAN ID
Set-VMNetworkAdapterVlan –VMName $VMName –Access –VlanId $VLANID

# Remove .ISOs from all VMs on a given Host
Set-VMDvdDrive -VMName * –Path ""