|
| 1 | +#Requires -Modules @{ModuleName="OZO";ModuleVersion="1.5.1"},@{ModuleName="OZOLogger";ModuleVersion="1.1.0"} -RunAsAdministrator |
| 2 | + |
| 3 | +<#PSScriptInfo |
| 4 | + .VERSION 0.1.0 |
| 5 | + .GUID 63ebd3a1-0d72-4090-9226-10db30d2e82f |
| 6 | + .AUTHOR Andy Lievertz <[email protected]> |
| 7 | + .COMPANYNAME One Zero One |
| 8 | + .COPYRIGHT This script is released under the terms of the GNU General Public License ("GPL") version 2.0. |
| 9 | + .TAGS |
| 10 | + .LICENSEURI https://github.com/onezeroone-dev/OZO-AD-Lab-Implement-Installation-Prerequisites/blob/main/LICENSE |
| 11 | + .PROJECTURI https://github.com/onezeroone-dev/OZO-AD-Lab-Implement-Installation-Prerequisites |
| 12 | + .ICONURI |
| 13 | + .EXTERNALMODULEDEPENDENCIES |
| 14 | + .REQUIREDSCRIPTS |
| 15 | + .EXTERNALSCRIPTDEPENDENCIES |
| 16 | + .RELEASENOTES https://github.com/onezeroone-dev/OZO-AD-Lab-Implement-Installation-Prerequisites/blob/main/CHANGELOG.md |
| 17 | + .PRIVATEDATA |
| 18 | +#> |
| 19 | + |
| 20 | +<# |
| 21 | + .SYNOPSIS |
| 22 | + See description. |
| 23 | + .DESCRIPTION |
| 24 | + Implements the installation prerequisites for the One Zero One AD Lab. |
| 25 | + .EXAMPLE |
| 26 | + ozo-ad-lab-implement-installation-prerequisites |
| 27 | + .LINK |
| 28 | + https://github.com/onezeroone-dev/OZO-AD-Lab-Implement-Installation-Prerequisites/blob/main/README.md |
| 29 | +#> |
| 30 | + |
| 31 | +Class ADLIP { |
| 32 | + # PROPERTIES: Strings |
| 33 | + [String] $currentUser = $null |
| 34 | + [String] $downloadsDir = $null |
| 35 | + [String] $featureName = $null |
| 36 | + [String] $localGroup = $null |
| 37 | + # PROPERTIES: PSCustomObjects |
| 38 | + [PSCustomObject] $ozoLogger = @() |
| 39 | + # METHODS |
| 40 | + # Constructor method |
| 41 | + ADLIP() { |
| 42 | + # Set properties |
| 43 | + $this.currentUser = ([System.Security.Principal.WindowsIdentity]::GetCurrent().Name) |
| 44 | + $this.downloadsDir = (Join-Path -Path $Env:USERPROFILE -ChildPath "Downloads") |
| 45 | + $this.featureName = "Microsoft-Hyper-V-All" |
| 46 | + $this.localGroup = "Hyper-V Administrators" |
| 47 | + # Create a logger object |
| 48 | + $this.ozoLogger = (New-OZOLogger) |
| 49 | + # Declare ourselves to the world |
| 50 | + $this.ozoLogger.Write("Process starting.","Information") |
| 51 | + # Call ValidateEnvironment to determine if we can proceed |
| 52 | + If ($this.ValidateEnvironment() -eq $true) { |
| 53 | + # Environment validates; report |
| 54 | + $this.ozoLogger.Write("Environment validates.","Information") |
| 55 | + # Call ProcessPrerequisites to ...process the prerequisites |
| 56 | + $this.ProcessPrerequisites() |
| 57 | + } Else { |
| 58 | + # Environment did not validate |
| 59 | + $this.ozoLogger.Write("The environment did not validate.","Error") |
| 60 | + } |
| 61 | + # Bid adieu to the world |
| 62 | + $this.ozoLogger.Write("Process complete.","Information") |
| 63 | + } |
| 64 | + # Process prerequisites method |
| 65 | + Hidden [Void] ProcessPrerequisites() { |
| 66 | + # Environment validates; install Hyper-V features |
| 67 | + $this.ozoLogger.Write("Installing Hyper-V features.","Information") |
| 68 | + If ($this.InstallHyperV() -eq $true) { |
| 69 | + # Hyper-V features are installed; determine if a reboot is not required |
| 70 | + $this.ozoLogger.Write("Determining if a restart is required.","Information") |
| 71 | + If ($this.RestartRequired() -eq $false) { |
| 72 | + # Restart is not required; add the local user to the Hyper-V Administrators group |
| 73 | + $this.ozoLogger.Write("Adding user to the local Hyper-V Administrators group.","Information") |
| 74 | + If ($this.ManageLocalHyperVAdministratorsGroup() -eq $true) { |
| 75 | + # Local user is added to the local Hyper-V Administrators group; create the VM switches |
| 76 | + $this.ozoLogger.Write("Creating the Hyper-V VMSwitches.","Information") |
| 77 | + If ($this.CreateVMSwitches() -eq $true) { |
| 78 | + # VM switches are created; report all prerequisites satisfied |
| 79 | + $this.ozoLogger.Write("All prerequisites are satisfied. Please see https://onezeroone.dev/active-directory-lab-customize-the-windows-installer-isos for the next steps.","Information") |
| 80 | + } Else { |
| 81 | + # VMSwitch creation error |
| 82 | + $this.ozoLogger.Write("Error creating the VM switches. Please manually create these switches then run this script again to continue. See https://onezeroone.dev/active-directory-lab-part-ii-customization-prerequisites/ for more information.","Error") |
| 83 | + } |
| 84 | + } Else { |
| 85 | + # Error adding user to local Hyper-V Administrators group |
| 86 | + $this.ozoLogger.Write(("Failure adding user " + $this.currentUser + " to the " + $this.localGroup + " group. Please manually add this user to this group then run this script again to continue. See https://onezeroone.dev/active-directory-lab-part-ii-customization-prerequisites/ for more information."),"Error") |
| 87 | + } |
| 88 | + } Else { |
| 89 | + # Restart is required |
| 90 | + $this.ozoLogger.Write("Please restart to complete the feature installation and then run this script again to continue.","Warning") |
| 91 | + # Get restart decision |
| 92 | + If ((Get-OZOYesNo) -eq "y") { |
| 93 | + # User elects to restart |
| 94 | + Restart-Computer |
| 95 | + } |
| 96 | + } |
| 97 | + } Else { |
| 98 | + # Error installing Hyper-V Feature |
| 99 | + $this.ozoLogger.Write(("Error installing the " + $this.featureName + " feature. Please manually install this feature and then run this script again to continue. See https://onezeroone.dev/active-directory-lab-part-ii-customization-prerequisites/ for more information."),"Error") |
| 100 | + } |
| 101 | + } |
| 102 | + # Environment validation method |
| 103 | + Hidden [Boolean] ValidateEnvironment() { |
| 104 | + # Control variable |
| 105 | + [Boolean] $Return = $true |
| 106 | + # Determine if this a user-interactive session |
| 107 | + If ((Get-OZOUserInteractive) -eq $false) { |
| 108 | + # Session is not user-interactive |
| 109 | + $this.ozoLogger.Write("Please run this script in a user-interactive session.","Error") |
| 110 | + $Return = $false |
| 111 | + } |
| 112 | + # Return |
| 113 | + return $Return |
| 114 | + } |
| 115 | + # Install Hyper-V method |
| 116 | + Hidden [Boolean] InstallHyperV() { |
| 117 | + # Control variable |
| 118 | + [Boolean] $Return = $true |
| 119 | + # Determine if the feature is present |
| 120 | + If ([Boolean](Get-WindowsOptionalFeature -Online -FeatureName $this.featureName) -eq $false) { |
| 121 | + # Feature is not present; try to install it |
| 122 | + Try { |
| 123 | + Enable-WindowsOptionalFeature -Online -FeatureName $this.featureName -ErrorAction Stop |
| 124 | + # Success |
| 125 | + } Catch { |
| 126 | + # Failure |
| 127 | + $Return = $false |
| 128 | + } |
| 129 | + } |
| 130 | + # Return |
| 131 | + return $Return |
| 132 | + } |
| 133 | + # Reboot required method |
| 134 | + Hidden [Boolean] RestartRequired() { |
| 135 | + # Control variable |
| 136 | + [Boolean] $Return = $false |
| 137 | + # Determine if feature is present |
| 138 | + If ((Get-WindowsOptionalFeature -Online -FeatureName $this.featureName).RestartRequired -eq "Required") { |
| 139 | + # Restart is required |
| 140 | + $this.Return = $true |
| 141 | + } |
| 142 | + # Return |
| 143 | + return $Return |
| 144 | + } |
| 145 | + # Manage local Hyper-V Administrators group membership |
| 146 | + Hidden [Boolean] ManageLocalHyperVAdministratorsGroup() { |
| 147 | + # Control variable |
| 148 | + [Boolean] $Return = $true |
| 149 | + # Determine if the current user is a member of the local Hyper-V Administrators group |
| 150 | + If ((Get-LocalGroupMember -Name $this.localGroup).Name -NotContains $this.currentUser) { |
| 151 | + # User is not in the local group; try to add them |
| 152 | + Try { |
| 153 | + Add-LocalGroupMember -Group "Hyper-V Administrators" -Member $this.currentUser |
| 154 | + # Success |
| 155 | + } Catch { |
| 156 | + # Failure |
| 157 | + $Return = $false |
| 158 | + } |
| 159 | + } |
| 160 | + # Return |
| 161 | + return $Return |
| 162 | + } |
| 163 | + # Create VM switches method |
| 164 | + Hidden [Boolean] CreateVMSwitches() { |
| 165 | + # Control variable |
| 166 | + [Boolean] $Return = $true |
| 167 | + [String] $externalAdapter = $null |
| 168 | + # Determine if the private switch already exists |
| 169 | + If ([Boolean](Get-VMSwitch -Name "AD Lab Private") -eq $false) { |
| 170 | + # Private switch does not exist; try to create it |
| 171 | + Try { |
| 172 | + New-VMSwitch -Name "AD Lab Private" -SwitchType Private -ErrorAction Stop |
| 173 | + # Success |
| 174 | + } Catch { |
| 175 | + # Failure |
| 176 | + $Return = $false |
| 177 | + } |
| 178 | + } |
| 179 | + # Determine if the external switch already exists |
| 180 | + If ([Boolean](Get-VMSwitch -Name "AD Lab External") -eq $false) { |
| 181 | + # External switch does not exist; call Get-NetAdapter to display available network connections |
| 182 | + Write-Host (Get-NetAdapter) |
| 183 | + # Prompt the user for the name of the external network connection until they correctly identify an adapter |
| 184 | + Do { |
| 185 | + $externalAdapter = (Read-Host "Above is the output of the Get-NetAdapter command. Type the Name of the network adapter that corresponds with your external network (Internet) connection") |
| 186 | + } Until ((Get-NetAdapter).Name -Contains $externalAdapter) |
| 187 | + # Try to create the external switch |
| 188 | + Try { |
| 189 | + New-VMSwitch -Name "AD Lab External" -NetAdapterName $externalAdapter -ErrorAction Stop |
| 190 | + # Success |
| 191 | + } Catch { |
| 192 | + # Failure |
| 193 | + $Return = $false |
| 194 | + } |
| 195 | + } |
| 196 | + # Return |
| 197 | + return $Return |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +Function Get-OZOYesNo { |
| 202 | + # Prompt the user to restart and return the lowercase of the first letter of their response |
| 203 | + [String]$response = $null |
| 204 | + Do { |
| 205 | + $response = (Read-Host "(Y/N)")[0].ToLower() |
| 206 | + } Until ($response -eq "y" -Or $response -eq "n") |
| 207 | + # Return response |
| 208 | + return $response |
| 209 | +} |
| 210 | + |
| 211 | +# MAIN |
| 212 | +[ADLIP]::new() | Out-Null |
0 commit comments