It's not helpful to say that you get "the same error over and over again, even when changes are made" and then not say what the error is or what changes you made.
Also, when posting PowerShell code, please use the code editor -- it's the icon that's 7th from the left on the option bar (the on with white "<>" on a dark background.
Here's what I think is in the code you posted:
#Install Active Directory Domain Services Role
Install-WindowsFeature -Name AD-Domain-Services -IncludeManagementTools
Import-Module ADDSDeployment
#Define domain name
$domainName = "ws2-2425-lisa.hogent"
#Install AD DS Forest
Install-ADDSForest -DomainName $domainName -ForestMode 7 -DomainMode 7
#-InstallDNS
#Manually trigger a restart
Write-Host "Restarting the server to complete AD DS installation..."
Restart-Computer -Force
#Wait for the VM to come back online and AD to be fully functional
Write-Host "Waiting for the server to reboot and become available..."
$maxRetries = 15
$retryCount = 0
$waitTime = 60 # Wait time in seconds between checks
while ($retryCount -lt $maxRetries) {
Start-Sleep -Seconds $waitTime
# Check if the server is back online by testing network connectivity or service availability
try {
$domainController = [ADSI]"LDAP://$domainName"
if ($domainController) {
Write-Host "Active Directory is available."
break
}
}
catch {
Write-Host "Waiting for Active Directory to become available..."
}
$retryCount++
}
if ($retryCount -eq $maxRetries) {
Write-Host "Active Directory is not reachable after multiple attempts."
exit 1 # Exit with a non-zero status if AD is still not available
}
#Install DHCP Role
Install-WindowsFeature -Name DHCP -IncludeManagementTools
#Retry the DHCP authorization if it fails initially
$dhcpAuthorized = $false
$retryCount = 0
while (-not $dhcpAuthorized -and $retryCount -lt 5) {
try {
Add-DhcpServerInDC -DnsName $domainName -IPAddress "192.168.24.99"
$dhcpAuthorized = $true
Write-Host "DHCP Server authorized successfully."
}
catch {
Write-Host "Failed to authorize DHCP Server. Retrying in 30 seconds..."
Start-Sleep -Seconds 30
$retryCount++
}
}
if (-not $dhcpAuthorized) {
Write-Host "Failed to authorize DHCP Server after multiple attempts."
exit 1
}
#Create DHCP Scope
$subnet = "192.168.24.0"
$mask = 24
$startRange = "192.168.24.101"
$endRange = "192.168.24.200"
$router = "192.168.24.10"
$dns = "192.168.24.10"
try {
Add-DhcpServerv4Scope -Name "Scope1" -StartRange $startRange -EndRange $endRange -SubnetMask $mask -State Active
Write-Host "DHCP Scope created successfully."
}
catch {
Write-Host "Failed to add DHCP Scope. Error: $_"
exit 1
}
#Set DHCP Options
try {
Set-DhcpServerv4OptionValue -ScopeId $subnet -Router $router -DnsServer $dns
Write-Host "DHCP Options set successfully."
}
catch {
Write-Host "Failed to set DHCP Options. Error: $_"
exit 1
}
#Optional: Display DHCP scope information
Get-DhcpServerv4Scope