Update win-enable-nested-hyperV.ps1

This commit is contained in:
haagha 2021-01-07 09:27:39 -05:00 коммит произвёл GitHub
Родитель 30b476141c
Коммит 3a74f99c67
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 13 добавлений и 4 удалений

Просмотреть файл

@ -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