From 3a74f99c674ff11f3d6021b3aa70e177e03947a7 Mon Sep 17 00:00:00 2001 From: haagha <64601174+haagha@users.noreply.github.com> Date: Thu, 7 Jan 2021 09:27:39 -0500 Subject: [PATCH] Update win-enable-nested-hyperV.ps1 --- src/windows/win-enable-nested-hyperV.ps1 | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/windows/win-enable-nested-hyperV.ps1 b/src/windows/win-enable-nested-hyperV.ps1 index d82e77d..5f60d3b 100644 --- a/src/windows/win-enable-nested-hyperV.ps1 +++ b/src/windows/win-enable-nested-hyperV.ps1 @@ -1,6 +1,11 @@ +. .\src\windows\common\setup\init.ps1 + +Log-Info 'Running Script Enable-NestedHyperV' + $scriptStartTime = get-date -f yyyyMMddHHmmss $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 @@ -20,7 +25,7 @@ $rsatDhcp = $features | where Name -eq 'RSAT-DHCP' if ($hyperv.Installed -and $hypervTools.Installed -and $hypervPowerShell.Installed) { - "START: Creating nested guest VM" | out-file -FilePath $logFile -Append + Log-Info 'START: Creating nested guest VM' | out-file -FilePath $logFile -Append # Sets "Do not start Server Manager automatically at logon" $return = New-ItemProperty -Path HKLM:\Software\Microsoft\ServerManager -Name DoNotOpenServerManagerAtLogon -PropertyType DWORD -Value 1 -force -ErrorAction SilentlyContinue $return = New-ItemProperty -Path HKLM:\Software\Microsoft\ServerManager\Oobe -Name DoNotOpenInitialConfigurationTasksAtLogon -PropertyType DWORD -Value 1 -force -ErrorAction SilentlyContinue @@ -33,6 +38,7 @@ if ($hyperv.Installed -and $hypervTools.Installed -and $hypervPowerShell.Install if (!$switch) { $switch = New-VMSwitch -Name Internal -SwitchType Internal -ErrorAction Stop + Log-Info 'New VMSwitch Successfully created' | out-file -FilePath $logFile -Append } $adapter = Get-NetAdapter -Name 'vEthernet (Internal)' -ErrorAction Stop @@ -40,18 +46,21 @@ if ($hyperv.Installed -and $hypervTools.Installed -and $hypervPowerShell.Install if (!$ip) { $return = New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex $adapter.ifIndex -ErrorAction Stop + Log-Info 'New NetIPAddress Successfully created' | out-file -FilePath $logFile -Append } $nat = Get-NetNat -Name InternalNAT -ErrorAction SilentlyContinue | select -first 1 if (!$nat) { $return = New-NetNat -Name InternalNAT -InternalIPInterfaceAddressPrefix 192.168.0.0/24 -ErrorAction Stop + Log-Info 'New NetNat Successfully created' | out-file -FilePath $logFile -Append } # Configure DHCP server service so nested guest can get an IP from DHCP and will use 168.63.129.16 for DNS and 192.168.0.1 as default gateway if ($dhcp.Installed -eq $false -or $rsatDhcp.Installed -eq $false) { $return = Install-WindowsFeature -Name DHCP -IncludeManagementTools -ErrorAction Stop + Log-Info 'New NetIPAddress Successfully created' | out-file -FilePath $logFile -Append } $scope = Get-DhcpServerv4Scope -ErrorAction SilentlyContinue | where Name -eq Scope1 | select -first 1 if (!$scope) @@ -79,7 +88,7 @@ if ($hyperv.Installed -and $hypervTools.Installed -and $hypervPowerShell.Install } catch { throw $_ - exit 1 + return $STATUS_ERROR } # Returns the nested guest VM status to the calling script - "Running" if all went well. @@ -94,12 +103,12 @@ else } catch { throw $_ - exit 1 + return $STATUS_ERROR } "END: Installing Hyper-V" | out-file -FilePath $logFile -Append $return.ExitCode write-host $return.ExitCode - return $return.ExitCode + return $STATUS_SUCCESS } $scriptEndTime = get-date -f yyyyMMddHHmmss