From d0a94b6e8034a1f62b007b1ad84d94ef2003bc78 Mon Sep 17 00:00:00 2001 From: Ryan McCallum Date: Wed, 13 Oct 2021 15:58:18 -0400 Subject: [PATCH 1/4] If param $gen=2, add attached drive on SCSI controller to Gen2 Hyper-V VM and change boot order to skip network boot --- src/windows/win-enable-nested-hyperv.ps1 | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/windows/win-enable-nested-hyperv.ps1 b/src/windows/win-enable-nested-hyperv.ps1 index d71bc2a..76d5203 100644 --- a/src/windows/win-enable-nested-hyperv.ps1 +++ b/src/windows/win-enable-nested-hyperv.ps1 @@ -9,7 +9,7 @@ $scriptPath = split-path -path $MyInvocation.MyCommand.Path -parent $scriptName = (split-path -path $MyInvocation.MyCommand.Path -leaf).Split('.')[0] $logFile = "$env:PUBLIC\Desktop\$($scriptName).log" -$scriptStartTime | out-file -FilePath $logFile -Append +$scriptStartTime | Out-File -FilePath $logFile -Append $nestedGuestVmName = 'ProblemVM' $batchFile = "$env:allusersprofile\Microsoft\Windows\Start Menu\Programs\StartUp\RunHyperVManagerAndVMConnect.cmd" @@ -72,17 +72,30 @@ if ($hyperv.Installed -and $hypervTools.Installed -and $hypervPowerShell.Install $return = Set-DhcpServerv4OptionValue -DnsServer 168.63.129.16 -Router 192.168.0.1 -ErrorAction Stop # Create the nested guest VM - if(!$gen) - { - $return = new-vm -name $nestedGuestVmName -MemoryStartupBytes 4GB -NoVHD -BootDevice IDE -Generation 1 -ErrorAction Stop + if (!$gen) { + Log-Info 'Creating Gen1 VM with 4GB memory' | Out-File -FilePath $logFile -Append + $return = New-VM -Name $nestedGuestVmName -MemoryStartupBytes 4GB -NoVHD -BootDevice IDE -Generation 1 -ErrorAction Stop } else { - $return = new-vm -name $nestedGuestVmName -MemoryStartupBytes 4GB -NoVHD -BootDevice IDE -Generation $gen -ErrorAction Stop + Log-Info "Creating Gen$($gen) VM with 4GB memory" | Out-File -FilePath $logFile -Append + $return = New-VM -Name $nestedGuestVmName -MemoryStartupBytes 4GB -NoVHD -Generation $gen -ErrorAction Stop } $return = set-vm -name $nestedGuestVmName -ProcessorCount 2 -CheckpointType Disabled -ErrorAction Stop $disk = get-disk -ErrorAction Stop | where {$_.FriendlyName -eq 'Msft Virtual Disk'} $return = $disk | set-disk -IsOffline $true -ErrorAction Stop $return = $disk | Add-VMHardDiskDrive -VMName $nestedGuestVmName -ErrorAction Stop + + if (!$gen) { + Log-Info "Gen1: Adding hard drive to IDE controller" | Out-File -FilePath $logFile -Append + $return = $disk | Add-VMHardDiskDrive -VMName $nestedGuestVmName -ErrorAction Stop + } + else { + Log-Info "Gen$($gen): Adding hard drive to SCSI controller" | Out-File -FilePath $logFile -Append + $return = $disk | Add-VMHardDiskDrive -VMName $nestedGuestVmName -ControllerType SCSI -ControllerNumber 0 -ErrorAction Stop + Log-Info "Gen$($gen): Modifying firmware boot order (we do not need to network boot)" | Out-File -FilePath $logFile -Append + $return = Set-VMFirmware $nestedGuestVmName -FirstBootDevice ((Get-VMFirmware $nestedGuestVmName).BootOrder | Where-Object { $_.BootType -eq "File" })[0] + } + $return = $switch | Connect-VMNetworkAdapter -VMName $nestedGuestVmName -ErrorAction Stop $return = start-vm -Name $nestedGuestVmName -ErrorAction Stop $nestedGuestVmState = (get-vm -Name $nestedGuestVmName -ErrorAction Stop).State From bd1b329278cf70c458a2fd4d34a2f8dd0210e87d Mon Sep 17 00:00:00 2001 From: Ryan McCallum Date: Wed, 13 Oct 2021 16:00:58 -0400 Subject: [PATCH 2/4] Revert unnecessary caps --- src/windows/win-enable-nested-hyperv.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/src/windows/win-enable-nested-hyperv.ps1 b/src/windows/win-enable-nested-hyperv.ps1 index 76d5203..81c7325 100644 --- a/src/windows/win-enable-nested-hyperv.ps1 +++ b/src/windows/win-enable-nested-hyperv.ps1 @@ -10,6 +10,7 @@ $scriptName = (split-path -path $MyInvocation.MyCommand.Path -leaf).Split('.')[0 $logFile = "$env:PUBLIC\Desktop\$($scriptName).log" $scriptStartTime | Out-File -FilePath $logFile -Append +$scriptStartTime | out-file -FilePath $logFile -Append $nestedGuestVmName = 'ProblemVM' $batchFile = "$env:allusersprofile\Microsoft\Windows\Start Menu\Programs\StartUp\RunHyperVManagerAndVMConnect.cmd" From 182bedd3bd7dac6dc0c96df36b5bfca7913e74e2 Mon Sep 17 00:00:00 2001 From: Ryan McCallum Date: Wed, 13 Oct 2021 16:03:02 -0400 Subject: [PATCH 3/4] Remove dupe line --- src/windows/win-enable-nested-hyperv.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/src/windows/win-enable-nested-hyperv.ps1 b/src/windows/win-enable-nested-hyperv.ps1 index 81c7325..401908b 100644 --- a/src/windows/win-enable-nested-hyperv.ps1 +++ b/src/windows/win-enable-nested-hyperv.ps1 @@ -9,7 +9,6 @@ $scriptPath = split-path -path $MyInvocation.MyCommand.Path -parent $scriptName = (split-path -path $MyInvocation.MyCommand.Path -leaf).Split('.')[0] $logFile = "$env:PUBLIC\Desktop\$($scriptName).log" -$scriptStartTime | Out-File -FilePath $logFile -Append $scriptStartTime | out-file -FilePath $logFile -Append $nestedGuestVmName = 'ProblemVM' From 276d6a1fecd9566097866d822c9ab3a4226753ad Mon Sep 17 00:00:00 2001 From: Ryan McCallum Date: Wed, 13 Oct 2021 17:07:43 -0400 Subject: [PATCH 4/4] Change boot order from "File" to "Drive" since efi firmware file is not present on WS2021R2 gen2 OS drive --- src/windows/win-enable-nested-hyperv.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/windows/win-enable-nested-hyperv.ps1 b/src/windows/win-enable-nested-hyperv.ps1 index 401908b..1c464c0 100644 --- a/src/windows/win-enable-nested-hyperv.ps1 +++ b/src/windows/win-enable-nested-hyperv.ps1 @@ -83,7 +83,6 @@ if ($hyperv.Installed -and $hypervTools.Installed -and $hypervPowerShell.Install $return = set-vm -name $nestedGuestVmName -ProcessorCount 2 -CheckpointType Disabled -ErrorAction Stop $disk = get-disk -ErrorAction Stop | where {$_.FriendlyName -eq 'Msft Virtual Disk'} $return = $disk | set-disk -IsOffline $true -ErrorAction Stop - $return = $disk | Add-VMHardDiskDrive -VMName $nestedGuestVmName -ErrorAction Stop if (!$gen) { Log-Info "Gen1: Adding hard drive to IDE controller" | Out-File -FilePath $logFile -Append @@ -93,7 +92,7 @@ if ($hyperv.Installed -and $hypervTools.Installed -and $hypervPowerShell.Install Log-Info "Gen$($gen): Adding hard drive to SCSI controller" | Out-File -FilePath $logFile -Append $return = $disk | Add-VMHardDiskDrive -VMName $nestedGuestVmName -ControllerType SCSI -ControllerNumber 0 -ErrorAction Stop Log-Info "Gen$($gen): Modifying firmware boot order (we do not need to network boot)" | Out-File -FilePath $logFile -Append - $return = Set-VMFirmware $nestedGuestVmName -FirstBootDevice ((Get-VMFirmware $nestedGuestVmName).BootOrder | Where-Object { $_.BootType -eq "File" })[0] + $return = Set-VMFirmware $nestedGuestVmName -FirstBootDevice ((Get-VMFirmware $nestedGuestVmName).BootOrder | Where-Object { $_.BootType -eq "Drive" })[0] } $return = $switch | Connect-VMNetworkAdapter -VMName $nestedGuestVmName -ErrorAction Stop