AutomatedLab Setup and Basic AD Lab

Overview

AutomatedLab is a PowerShell module that builds lab environments on Hyper-V or Azure from a definition script. This post covers installing it and deploying a small Active Directory lab: one domain controller, two member servers and one Windows client, with outbound internet access through NAT on the Hyper-V host.

AutomatedLab is a different project from PSAutoLab. PSAutoLab is built on the Lability module, runs only in Windows PowerShell 5.1 and uses its own commands (Setup-Host, Setup-Lab). Nothing in this post applies to it.

To download the MSI installer please use this link - https://automatedlab.org/en/latest/

VMOperating systemRoleIP address
DC01Windows Server 2022 Standard (Desktop Experience)Root domain controller and DNS192.168.10.10
Srv01Windows Server 2022 Standard (Desktop Experience)Member server192.168.10.21
Srv02Windows Server 2022 Standard (Desktop Experience)Member server192.168.10.22
Client1Windows 10 ProDomain-joined client192.168.10.100
Hyper-V host-NAT gateway192.168.10.1

Domain lab.local, network 192.168.10.0/24.

Requirements

ItemRequirement
Host OSWindows 8.1 / Windows Server 2012 R2 or later with Hyper-V (Windows client: Pro, Enterprise or Education)
PowerShellWindows PowerShell 5.1 (.NET Framework 4.7.1) or PowerShell 7
RightsLocal administrator on the host
MediaAn ISO for each operating system in the lab, en-US recommended
MemoryFour VMs at 2 GB need 8 GB for the VMs alone, plus the host’s own use
StorageSSD recommended; AutomatedLab reports problems on slow spinning disks

Enable Hyper-V if it is not already on, then restart:

# Windows 10/11
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All

# Windows Server
Install-WindowsFeature -Name Hyper-V -IncludeManagementTools -Restart

Install AutomatedLab

Run every command in this post from an elevated PowerShell session.

The project documents this as the only way to install AutomatedLab for PowerShell 7.

Install-PackageProvider -Name NuGet -Force
Install-Module -Name AutomatedLab -SkipPublisherCheck -AllowClobber -Force

-SkipPublisherCheck is needed because AutomatedLab’s test module requires Pester 5, and installs fail on systems that only have Pester 3 (the version included with Windows).

Pre-answer the telemetry prompt, enable host remoting and create the LabSources folder:

[Environment]::SetEnvironmentVariable('AUTOMATEDLAB_TELEMETRY_OPTIN', 'false', 'Machine')
$env:AUTOMATEDLAB_TELEMETRY_OPTIN = 'false'
Enable-LabHostRemoting -Force
New-LabSourcesFolder -DriveLetter C

New-LabSourcesFolder creates C:\LabSources with its subfolders (ISOs, tools, scripts). Use another drive letter to keep ISOs on a larger disk.

Option 2: MSI Installer

Download AutomatedLab.msi from the project’s GitHub releases page and choose Typical or Complete. Both install the modules to C:\Program Files\WindowsPowerShell\Modules and create C:\LabSources. Custom lets LabSources go on another disk; the project advises leaving the module location at its default.

Use the Gallery instead if the lab will be run from PowerShell 7 (pwsh).

Confirm either install:

Get-Module AutomatedLab -ListAvailable | Select-Object Name, Version, ModuleBase

Updating

Installed withUpdate method
Install-ModuleUpdate-Module -Name AutomatedLab
MSIInstall the newer MSI from GitHub releases

Update-Module only updates modules that were installed with Install-Module, so it cannot update an MSI install. ModuleBase from the command above shows where the module is installed.

Host Configuration

Copy the ISOs into C:\LabSources\ISOs, then list the operating systems AutomatedLab finds. The names used in the lab script must match this output exactly.

Get-LabAvailableOperatingSystem | Sort-Object OperatingSystemName | Format-Table OperatingSystemName, Version

Set the VM storage path. If the ISOs are not en-US (en-GB, for example), also stop AutomatedLab skipping them. The -FullName ... | Register-PSFConfig form keeps the settings across sessions and module updates.

Set-PSFConfig -FullName AutomatedLab.VmPath -Value '<VM_PATH>' -PassThru | Register-PSFConfig
Set-PSFConfig -FullName AutomatedLab.DoNotSkipNonNonEnglishIso -Value $true -PassThru | Register-PSFConfig
Get-PSFConfig -Module AutomatedLab | Where-Object Name -in 'VmPath', 'DoNotSkipNonNonEnglishIso' | Format-Table Name, Value

The setting name really is DoNotSkipNonNonEnglishIso, with “Non” twice. Its default is False.

Lab Definition Script

Save as Deploy-BasicLab.ps1 and replace <ADMIN_PASSWORD>. The password sits in plain text in the script, so use it for the lab only.

AutomatedLab has no NAT definition cmdlet, so the script builds an internal network and internet access is added on the host afterwards (Internet Access). $PSDefaultParameterValues applies the network, domain and memory to every Add-LabMachineDefinition call.

#Requires -Modules AutomatedLab
#Requires -RunAsAdministrator

# Lab settings
$LabName       = 'BasicLab'
$DomainName    = 'lab.local'
$AddressSpace  = '192.168.10.0/24'
$AdminPassword = '<ADMIN_PASSWORD>'

# Must match Get-LabAvailableOperatingSystem output exactly
$ServerOS = 'Windows Server 2022 Standard (Desktop Experience)'
$ClientOS = 'Windows 10 Pro'

# Lab, credentials, network and domain
New-LabDefinition -Name $LabName -DefaultVirtualizationEngine HyperV
Set-LabInstallationCredential -Username Administrator -Password $AdminPassword
Add-LabVirtualNetworkDefinition -Name $LabName -AddressSpace $AddressSpace
Add-LabDomainDefinition -Name $DomainName -AdminUser Administrator -AdminPassword $AdminPassword

# Defaults for every machine
$PSDefaultParameterValues = @{
    'Add-LabMachineDefinition:Network'    = $LabName
    'Add-LabMachineDefinition:DomainName' = $DomainName
    'Add-LabMachineDefinition:Memory'     = 2GB
}

# Machines
Add-LabMachineDefinition -Name DC01    -OperatingSystem $ServerOS -Roles RootDC -IpAddress '192.168.10.10'
Add-LabMachineDefinition -Name Srv01   -OperatingSystem $ServerOS -IpAddress '192.168.10.21'
Add-LabMachineDefinition -Name Srv02   -OperatingSystem $ServerOS -IpAddress '192.168.10.22'
Add-LabMachineDefinition -Name Client1 -OperatingSystem $ClientOS -IpAddress '192.168.10.100'

# Deploy
Install-Lab
Show-LabDeploymentSummary

Deploy and Verify

& 'C:\<SCRIPT_FOLDER>\Deploy-BasicLab.ps1'

Install-Lab creates the switch and VMs, installs the operating systems, promotes DC01 and joins the other machines to the domain. When it finishes:

Get-LabVM | Format-Table Name, OperatingSystem, DomainName
Get-LabVMStatus

Expected: four machines listed, all Started.

Internet Access

Host NAT

Windows NAT (WinNAT) on the host gives the lab subnet outbound access through the host’s own connection. Run these on the Hyper-V host after Install-Lab.

1. Check the host’s address on the lab switch. The switch takes the network definition’s name, and Hyper-V names the host’s adapter on it vEthernet (<switch name>). The NAT gateway is the host’s address on this adapter, inside the lab subnet.

$LabName = 'BasicLab'
Get-VMSwitch | Format-Table Name, SwitchType
Get-NetIPAddress -InterfaceAlias "vEthernet ($LabName)" -AddressFamily IPv4 | Format-Table IPAddress, PrefixLength

If 192.168.10.1 is not listed, add it:

New-NetIPAddress -InterfaceAlias "vEthernet ($LabName)" -IPAddress '192.168.10.1' -PrefixLength 24

2. Create the NAT. A host supports only one NAT network, so check for an existing one first and remove it if it belongs to an old lab.

Get-NetNat
New-NetNat -Name "$($LabName)NAT" -InternalIPInterfaceAddressPrefix '192.168.10.0/24'

3. Set the gateway on the VMs and DNS forwarders on the DC. WinNAT does not assign addresses, gateways or DNS to guests.

Import-Lab -Name $LabName

Invoke-LabCommand -ComputerName (Get-LabVM).Name -ScriptBlock {
    $nic = Get-NetAdapter | Where-Object Status -eq 'Up' | Select-Object -First 1
    Remove-NetRoute -DestinationPrefix '0.0.0.0/0' -Confirm:$false -ErrorAction SilentlyContinue
    New-NetRoute -DestinationPrefix '0.0.0.0/0' -InterfaceIndex $nic.ifIndex -NextHop '192.168.10.1'
}

Invoke-LabCommand -ComputerName DC01 -ScriptBlock {
    Add-DnsServerForwarder -IPAddress 1.1.1.1, 8.8.8.8 -ErrorAction SilentlyContinue
}

4. Test.

Invoke-LabCommand -ComputerName DC01 -ScriptBlock {
    Test-NetConnection -ComputerName 8.8.8.8 -Port 443
    Resolve-DnsName www.microsoft.com -Type A
} -PassThru

Expected: TcpTestSucceeded : True and A records returned.

Other Network Options

OptionInternetDefinition
Internal onlyNoneAdd-LabVirtualNetworkDefinition -Name $LabName -AddressSpace $AddressSpace (switch type defaults to Internal)
Internal plus host NATOutboundInternal network, then Host NAT
ExternalBridged to the physical LANAdd -HyperVProperties @{ SwitchType = 'External'; AdapterName = '<HOST_ADAPTER>' }
Routing VMThrough the VMA machine with the Routing role on both the lab network and an external network

Routing VM example. AutomatedLab handles routing for a network that contains a machine with the Routing role:

Add-LabVirtualNetworkDefinition -Name 'External' -HyperVProperties @{ SwitchType = 'External'; AdapterName = '<HOST_ADAPTER>' }

$nics = @(
    New-LabNetworkAdapterDefinition -VirtualSwitch $LabName -Ipv4Address '192.168.10.1'
    New-LabNetworkAdapterDefinition -VirtualSwitch 'External' -UseDhcp
)
Add-LabMachineDefinition -Name Router01 -OperatingSystem $ServerOS -Roles Routing -NetworkAdapter $nics

Get-NetAdapter | Where-Object Status -eq 'Up' lists the host adapter names.

Working With the Lab

A new PowerShell session has no lab loaded. Run Import-Lab before any other lab command.

TaskCommand
Load the labImport-Lab -Name BasicLab
VM stateGet-LabVMStatus
Console (VMConnect)Connect-LabVM -ComputerName DC01
Remote sessionEnter-LabPSSession -ComputerName DC01
Run a command and see outputInvoke-LabCommand -ComputerName DC01 -ScriptBlock { hostname } -PassThru
Copy a file to a VMCopy-LabFileItem -Path C:\Scripts\Test.ps1 -ComputerName DC01 -DestinationFolderPath C:\Scripts
Start or stop all VMsStart-LabVM -All / Stop-LabVM -All
Checkpoint all VMsCheckpoint-LabVM -All -SnapshotName 'Initial-Deploy'
Restore all VMsRestore-LabVMSnapshot -All -SnapshotName 'Initial-Deploy'
List checkpointsGet-LabVMSnapshot

Hyper-V’s own file copy (Copy-VMFile) needs the Guest Service Interface integration service, the only one that is off by default on Windows guests:

Get-LabVM | ForEach-Object { Enable-VMIntegrationService -VMName $_.Name -Name 'Guest Service Interface' }
Copy-VMFile -VMName DC01 -SourcePath 'C:\Scripts\Test.ps1' -DestinationPath 'C:\Scripts\Test.ps1' -FileSource Host -CreateFullPath

Modifying the Lab

ChangeEdit
Lab name$LabName (also names the network, switch and NAT)
Domain$DomainName
Subnet$AddressSpace and every -IpAddress; each address must be inside the subnet
Memory-Memory on one machine, or the default in $PSDefaultParameterValues
Operating system$ServerOS / $ClientOS, copied from Get-LabAvailableOperatingSystem
Add a VMAnother Add-LabMachineDefinition line before Install-Lab
Remove a deployed VMRemove-LabVM -Name Srv02, then delete its line from the script

Roles go in -Roles as a comma-separated list, for example -Roles CaRoot, FileServer. A machine can have none, one or several; some are mutually exclusive, such as RootDC and FirstChildDC. Common roles:

RolePurpose
RootDCFirst DC of a new forest
FirstChildDCFirst DC of a child domain
DCAdditional domain controller
CaRoot / CaSubordinateCertification authority
DHCPDHCP server
FileServerFile server
WebServerIIS
RoutingRouting between lab networks
SQLServer2019 / SQLServer2022SQL Server

The full list is in the AutomatedLab roles documentation.

Removing the Lab

Import-Lab -Name BasicLab
Remove-Lab -Name BasicLab -Confirm:$false
Get-NetNat | Where-Object Name -eq 'BasicLabNAT' | Remove-NetNat -Confirm:$false

The NAT was created outside AutomatedLab, so it is removed separately. Afterwards, check the VmPath folder for anything left behind.

Troubleshooting

SymptomCauseFix
Operating system not found when the script runsName differs from the ISO, or a non-en-US ISO was skippedCopy the exact name from Get-LabAvailableOperatingSystem; set DoNotSkipNonNonEnglishIso
IP address not in the network rangeAn -IpAddress falls outside $AddressSpaceUse host addresses inside the subnet
Add-LabNatDefinition is not recognisedAutomatedLab has no such cmdletUse Host NAT
New-NetNat fails or NAT behaves unpredictablyA NAT already exists; WinNAT supports one internal prefix per hostGet-NetNat, remove the old NAT, retry
VMs have no internetNo default route on the VMs, or no gateway address on the host adapterHost NAT steps 1 and 3
Lab commands return nothing in a new sessionLab not loadedImport-Lab -Name BasicLab
Invoke-LabCommand shows no outputOutput is returned only with -PassThruAdd -PassThru
Copy-VMFile failsGuest Service Interface is offEnable it (see Working With the Lab)
Update-Module: “no valid module was found in the module directory”Module not installed with Install-Module, or not in a module folder this PowerShell searchesGet-Module AutomatedLab -ListAvailable; update an MSI install with the newer MSI
Re-running the script after a failed deployment failsPartial lab left behindRemove-Lab, then re-run

References