|
| 1 | +# Enforce best practices in Powershell |
| 2 | +Set-StrictMode -Version 1.0 |
| 3 | +# Exit if a cmdlet fails |
| 4 | +$ErrorActionPreference = "Stop" |
| 5 | + |
| 6 | +# Configuration |
| 7 | +$domain = "demo.local" |
| 8 | +$plaintextPassword = "vagrant" |
| 9 | + |
| 10 | +################################################################################## |
| 11 | +# Password policy configuration |
| 12 | +################################################################################## |
| 13 | + |
| 14 | +Write-Host -fore green $ 'Running password policy logic' |
| 15 | + |
| 16 | +# Ensure passwords never expire |
| 17 | +net accounts /maxpwage:unlimited |
| 18 | + |
| 19 | +# Disable automatic machine account password changes |
| 20 | +Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NetLogon\Parameters' -Name DisablePasswordChange -Value 1 |
| 21 | + |
| 22 | +# Allow weak passwords |
| 23 | +secedit /export /cfg c:\secpol.cfg |
| 24 | +(Get-Content C:\secpol.cfg).replace("PasswordComplexity = 1", "PasswordComplexity = 0") | Out-File C:\secpol.cfg |
| 25 | +secedit /configure /db c:\windows\security\local.sdb /cfg c:\secpol.cfg /areas SECURITYPOLICY |
| 26 | +rm -force c:\secpol.cfg -confirm:$false |
| 27 | + |
| 28 | +################################################################################## |
| 29 | +# Disable Antivirus |
| 30 | +################################################################################## |
| 31 | + |
| 32 | +if (Get-Module -ListAvailable -Name Defender) { |
| 33 | + Set-MpPreference -DisableRealtimeMonitoring $true |
| 34 | + New-ItemProperty -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender" -Name DisableAntiSpyware -Value 1 -PropertyType DWORD -Force |
| 35 | +} |
| 36 | + |
| 37 | +##################################################################################### |
| 38 | +# Forest installation |
| 39 | +##################################################################################### |
| 40 | + |
| 41 | +Write-Host -fore green $ 'Running forest installation' |
| 42 | + |
| 43 | +$safeModeAdministratorPassword = ConvertTo-SecureString $plaintextPassword -AsPlainText -Force |
| 44 | + |
| 45 | +# Set local Administrator account password to stop the error: |
| 46 | +# "The new domain cannot be created DC01: because the local Administrator account password does not meet requirements." |
| 47 | +Write-Host -fore green $ 'Setting local administrator password' |
| 48 | +Set-LocalUser ` |
| 49 | + -Name Administrator ` |
| 50 | + -AccountNeverExpires ` |
| 51 | + -Password $safeModeAdministratorPassword ` |
| 52 | + -PasswordNeverExpires:$true ` |
| 53 | + -UserMayChangePassword:$true |
| 54 | + |
| 55 | +Install-WindowsFeature AD-Domain-Services,RSAT-AD-AdminCenter,RSAT-ADDS-Tools -IncludeManagementTools -Verbose |
| 56 | + |
| 57 | +# |
| 58 | +# Install the Active Directory Domain Services (AD DS) environment |
| 59 | +# |
| 60 | + |
| 61 | +# Win32_operatingSystem ProductType |
| 62 | +# Work Station (1) |
| 63 | +# Domain Controller (2) |
| 64 | +# Server (3) |
| 65 | +# https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-operatingsystem |
| 66 | +$isDomainController = (Get-WmiObject -Class Win32_operatingSystem).ProductType -Eq 2 |
| 67 | +Write-Host -fore green $ 'IsDomainController='$isDomainController |
| 68 | +if (!$isDomainController) { |
| 69 | + Write-Host -fore green $ 'Installing ADDS' |
| 70 | + $netbios = $domain.split('.')[0].ToUpperInvariant() |
| 71 | + Install-ADDSForest ` |
| 72 | + -CreateDnsDelegation:$false ` |
| 73 | + -DatabasePath "C:\Windows\NTDS" ` |
| 74 | + -DomainMode "Win2012R2" ` |
| 75 | + -DomainName $domain ` |
| 76 | + -DomainNetbiosName $netbios ` |
| 77 | + -ForestMode "Win2012R2" ` |
| 78 | + -InstallDns:$true ` |
| 79 | + -LogPath "C:\Windows\NTDS" ` |
| 80 | + -NoRebootOnCompletion:$false ` |
| 81 | + -SysvolPath "C:\Windows\SYSVOL" ` |
| 82 | + -Force:$true ` |
| 83 | + -SafeModeAdministratorPassword $safeModeAdministratorPassword ` |
| 84 | + -Verbose |
| 85 | +} |
| 86 | + |
| 87 | +Write-Host -fore green $ 'Finished forest installation' |
0 commit comments