Automatisation of server environment with virtualbox, packer and powershell

Lisa D'hooghe 0 Reputation points
2024-10-10T14:08:12.67+00:00

Hi,

I am currently working on a project and keep having the same error. Does anybody know why I am getting this error(see screenshot)?

User's image

Thanks in advance!

The objective is to automate the setup of a Windows Server 2022 environment using Packer and Vagrant in conjunction with VirtualBox. The setup involves creating and configuring multiple virtual machines (VMs) that fulfill specific roles within a corporate network environment. The assignment requires the following:

Virtual Machines:

Two VMs with Windows Server 2022 Core (no GUI) for infrastructure roles such as Active Directory (AD), DHCP, DNS, Certificate Authority (CA), and Microsoft SQL Server.

One VM with Windows 10 Enterprise as a client for management and testing purposes.
Technical Requirements:


The VMs must use the Vagrant boxes:

    Windows Server 2022: gusztavvargadr/windows-server-2022-standard-core

    Windows 10 Enterprise: gusztavvargadr/windows-10-22h2-enterprise

Each VM has two network interfaces: one NAT for internet access and one Host-Only network interface for internal communication.

The total RAM for all VMs cannot exceed 8 GB.
Automation Details:


Use Vagrant to create and manage the VMs, ensuring the servers are started and snapshots are taken before provisioning the client.

Packer is employed to further customize the VMs, using snapshots taken by VirtualBox as the base state.# Install Active Directory Domain Services Role
# 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

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,561 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 46,896 Reputation points
    2024-10-10T15:12:58.3233333+00:00

    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
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.