-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-host.ps1
More file actions
77 lines (65 loc) · 2.88 KB
/
Copy pathprepare-host.ps1
File metadata and controls
77 lines (65 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#Requires -RunAsAdministrator
param(
[string]$User = "$env:USERDOMAIN\$env:USERNAME"
)
$ErrorActionPreference = "Stop"
function AssertFirmwareVirtualization {
$system = Get-CimInstance Win32_ComputerSystem -ErrorAction SilentlyContinue
if ($system -and $system.HypervisorPresent) { return }
$processors = @(Get-CimInstance Win32_Processor -ErrorAction SilentlyContinue)
if (-not $processors) { return }
$firmwareStates = @($processors | ForEach-Object { $_.VirtualizationFirmwareEnabled })
if (($firmwareStates.Count -gt 0) -and ($firmwareStates -contains $false)) {
throw "Hardware virtualization is disabled in BIOS/UEFI. Enable Intel VT-x, AMD-V or SVM, then restart Windows."
}
if (@($processors | ForEach-Object { $_.VMMonitorModeExtensions }) -contains $false) {
throw "CPU does not report VM monitor extensions required by Hyper-V."
}
if (@($processors | ForEach-Object { $_.SecondLevelAddressTranslationExtensions }) -contains $false) {
throw "CPU does not report SLAT support required by Hyper-V."
}
}
function EnableFeature($name) {
$feature = Get-WindowsOptionalFeature -Online -FeatureName $name -ErrorAction SilentlyContinue
if (-not $feature) {
if (Get-Module -ListAvailable -Name Hyper-V) { return }
throw "Windows optional feature '$name' was not found. This Windows edition may not support Hyper-V."
}
if ($feature.State -ne "Enabled") {
Enable-WindowsOptionalFeature -Online -FeatureName $name -All -NoRestart | Out-Null
$script:restartNeeded = $true
}
}
function EnableCapability($pattern) {
$capability = Get-WindowsCapability -Online |
Where-Object Name -like $pattern |
Select-Object -First 1
if ($capability -and $capability.State -ne "Installed") {
Add-WindowsCapability -Online -Name $capability.Name | Out-Null
}
}
function HyperVGroup {
$sid = [Security.Principal.SecurityIdentifier]"S-1-5-32-578"
$sid.Translate([Security.Principal.NTAccount]).Value.Split("\")[-1]
}
$restartNeeded = $false
$groupChanged = $false
AssertFirmwareVirtualization
EnableFeature "Microsoft-Hyper-V-All"
EnableCapability "OpenSSH.Client*"
& bcdedit.exe /set hypervisorlaunchtype auto | Out-Null
Set-VMHost -EnableEnhancedSessionMode $true
$group = HyperVGroup
$members = Get-LocalGroupMember -Group $group -ErrorAction SilentlyContinue |
ForEach-Object { $_.Name.ToLowerInvariant() }
if ($members -notcontains $User.ToLowerInvariant()) {
Add-LocalGroupMember -Group $group -Member $User
$groupChanged = $true
}
if ($restartNeeded) {
"Host prepared. Restart Windows before creating the VM."
} elseif ($groupChanged) {
"Host prepared. Restart Windows before creating the VM. A sign out and sign in is usually enough for group membership, but a restart is recommended."
} else {
"Host is ready. Use normal PowerShell to create the VM."
}