Skip to content
plataformaindependiente edited this page Jun 27, 2016 · 1 revision

param([string]$vmName) 2 # 3 # Enable-NestedVm.ps1 4 # 5 # Checks VM for nesting comatability and configures if not properly setup. 6 # 7 # Author: Drew Cross 8

9 if([string]::IsNullOrEmpty($vmName)) { 10 Write-Host "No VM name passed" 11 Exit; 12 } 13

14 # Constants 15 $4GB = 4294967296 16

17 # 18 # Need to run elevated. Do that here. 19 # 20

21 # Get the ID and security principal of the current user account 22 $myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent(); 23 $myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID); 24

25 # Get the security principal for the administrator role 26 $adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator; 27

28 # Check to see if we are currently running as an administrator 29 if ($myWindowsPrincipal.IsInRole($adminRole)) { 30 # We are running as an administrator, so change the title and background colour to indicate this 31 $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"; 32

33 } else { 34 # We are not running as an administrator, so relaunch as administrator 35

36 # Create a new process object that starts PowerShell 37 $newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell"; 38

39 # Specify the current script path and name as a parameter with added scope and support for scripts with spaces in it's path 40 $newProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'" 41

42 # Indicate that the process should be elevated 43 $newProcess.Verb = "runas"; 44

45 # Start the new process 46 [System.Diagnostics.Process]::Start($newProcess) | Out-Null; 47

48 # Exit from the current, unelevated, process 49 Exit; 50 } 51

52 # 53 # Get Vm Information 54 # 55

56 $vm = Get-VM -Name $vmName 57

58 $vmInfo = New-Object PSObject 59
60 # VM info 61 Add-Member -InputObject $vmInfo NoteProperty -Name "ExposeVirtualizationExtensions" -Value $false 62 Add-Member -InputObject $vmInfo NoteProperty -Name "DynamicMemoryEnabled" -Value $vm.DynamicMemoryEnabled 63 Add-Member -InputObject $vmInfo NoteProperty -Name "SnapshotEnabled" -Value $false 64 Add-Member -InputObject $vmInfo NoteProperty -Name "State" -Value $vm.State 65 Add-Member -InputObject $vmInfo NoteProperty -Name "MacAddressSpoofing" -Value ((Get-VmNetworkAdapter -VmName $vmName).MacAddressSpoofing) 66 Add-Member -InputObject $vmInfo NoteProperty -Name "MemorySize" -Value (Get-VMMemory -VmName $vmName).Startup 67

68

69 # is nested enabled on this VM? 70 $vmInfo.ExposeVirtualizationExtensions = (Get-VMProcessor -VM $vm).ExposeVirtualizationExtensions 71

72 Write-Host "This script will set the following for $vmName in order to enable nesting:" 73
74 $prompt = $false; 75

76 # Output text for proposed actions 77 if ($vmInfo.State -eq 'Saved') { 78 Write-Host "\tSaved state will be removed" 79 $prompt = $true 80 } 81 if ($vmInfo.State -ne 'Off' -or $vmInfo.State -eq 'Saved') { 82 Write-Host "Vm State:" $vmInfo.State 83 Write-Host " $vmName will be turned off" 84 $prompt = $true
85 } 86 if ($vmInfo.ExposeVirtualizationExtensions -eq $false) { 87 Write-Host " Virtualization extensions will be enabled" 88 $prompt = $true 89 } 90 if ($vmInfo.DynamicMemoryEnabled -eq $true) { 91 Write-Host " Dynamic memory will be disabled" 92 $prompt = $true 93 } 94 if($vmInfo.MacAddressSpoofing -eq 'Off'){ 95 Write-Host " Optionally enable mac address spoofing" 96 $prompt = $true 97 } 98 if($vmInfo.MemorySize -lt $4GB) { 99 Write-Host " Optionally set vm memory to 4GB" 100 $prompt = $true 101 } 102

103 if(-not $prompt) { 104 Write-Host " None, vm is already setup for nesting" 105 Exit; 106 } 107

108 Write-Host "Input Y to accept or N to cancel:" -NoNewline 109

110 $char = Read-Host 111

112 while(-not ($char.StartsWith('Y') -or $char.StartsWith('N'))) { 113 Write-Host "Invalid Input, Y or N"
114 $char = Read-Host 115 } 116

117

118 if($char.StartsWith('Y')) { 119 if ($vmInfo.State -eq 'Saved') { 120 Remove-VMSavedState -VMName $vmName 121 } 122 if ($vmInfo.State -ne 'Off' -or $vmInfo.State -eq 'Saved') { 123 Stop-VM -VMName $vmName 124 } 125 if ($vmInfo.ExposeVirtualizationExtensions -eq $false) { 126 Set-VMProcessor -VMName $vmName -ExposeVirtualizationExtensions $true 127 } 128 if ($vmInfo.DynamicMemoryEnabled -eq $true) { 129 Set-VMMemory -VMName $vmName -DynamicMemoryEnabled $false 130 } 131

132 # Optionally turn on mac spoofing 133 if($vmInfo.MacAddressSpoofing -eq 'Off') { 134 Write-Host "Mac Address Spoofing isn't enabled (nested guests won't have network)." -ForegroundColor Yellow
135 Write-Host "Would you like to enable MAC address spoofing? (Y/N)" -NoNewline 136 $input = Read-Host 137

138 if($input -eq 'Y') { 139 Set-VMNetworkAdapter -VMName $vmName -MacAddressSpoofing on 140 } 141 else { 142 Write-Host "Not enabling Mac address spoofing." 143 } 144

145 } 146

147 if($vmInfo.MemorySize -lt $4GB) { 148 Write-Host "VM memory is set less than 4GB, without 4GB or more, you may not be able to start VMs." -ForegroundColor Yellow 149 Write-Host "Would you like to set Vm memory to 4GB? (Y/N)" -NoNewline 150 $input = Read-Host
151

152 if($input -eq 'Y') { 153 Set-VMMemory -VMName $vmName -StartupBytes $4GB 154 } 155 else { 156 Write-Host "Not setting Vm Memory to 4GB." 157 } 158 } 159 Exit; 160 } 161

162 if($char.StartsWith('N')) { 163 Write-Host "Exiting..." 164 Exit; 165 } 166

167 Write-Host 'Invalid input'

Clone this wiki locally