Workshops - ADRecoveryLab - HyperV


$labName = 'ADRecovery'

#setting addMemberServer to $true installes an additional server in the lab
$addMemberServer = $false

#setting createCheckpoint to $true creates a checkpoint of the whole lab after the deployment is finished
$createCheckpoint = $false

#----------------------- CHANGING ANYTHING BEYOND THIS LINE SHOULD NOT BE REQUIRED ----------------------------------
#----------------------- + EXCEPT FOR THE LINES STARTING WITH: REMOVE THE COMMENT TO --------------------------------
#----------------------- + EXCEPT FOR THE LINES CONTAINING A PATH TO AN ISO OR APP   --------------------------------

#create an empty lab template and define where the lab XML files and the VMs will be stored
New-LabDefinition -Name $labName -DefaultVirtualizationEngine HyperV

#make the network definition
Add-LabVirtualNetworkDefinition -Name $labName -AddressSpace

#and the domain definition with the domain admin account
Add-LabDomainDefinition -Name -AdminUser Install -AdminPassword Somepass1
Add-LabDomainDefinition -Name -AdminUser Install -AdminPassword Somepass1

Set-LabInstallationCredential -Username Install -Password Somepass1

#Backup disks
Add-LabDiskDefinition -Name BackupRoot -DiskSizeInGb 40
Add-LabDiskDefinition -Name BackupChild -DiskSizeInGb 40

#Set the parameters that are the same for all machines
$PSDefaultParameterValues = @{
    'Add-LabMachineDefinition:ToolsPath' = "$labSources\Tools"
    'Add-LabMachineDefinition:Network' = $labName
    'Add-LabMachineDefinition:Processors' = 2
    'Add-LabMachineDefinition:Memory' = 768MB
    'Add-LabMachineDefinition:OperatingSystem' = 'Windows Server 2016 Datacenter (Desktop Experience)'

#Defining machines
Add-LabMachineDefinition -Name ContosoDC1 -IpAddress `
    -DnsServer1 -DnsServer2 -DomainName -Roles RootDC

$role = Get-LabMachineRoleDefinition -Role DC
Add-LabMachineDefinition -Name ContosoDC2 -DiskName BackupRoot -IpAddress `
    -DnsServer1 -DnsServer2 -DomainName  -Roles DC

if ($addMemberServer)
    Add-LabMachineDefinition -Name ContosoMember1 -IpAddress -DnsServer1 -DnsServer2 -DomainName

#Defining machines
$role = Get-LabMachineRoleDefinition -Role FirstChildDC -Properties @{ ParentDomain = ''; NewDomain = 'child' }
$postInstallActivity = Get-LabPostInstallationActivity -ScriptFileName 'New-ADLabAccounts 2.0.ps1' -DependencyFolder $labSources\PostInstallationActivities\PrepareFirstChildDomain
Add-LabMachineDefinition -Name ChildDC1 -IpAddress -DnsServer1 -DnsServer2 -DomainName -Roles $role -PostInstallationActivity $postInstallActivity

Add-LabMachineDefinition -Name ChildDC2 -DiskName BackupChild -IpAddress -DnsServer1 -DnsServer2 -DomainName -Roles DC

#Now the actual work begins

#Installs RSAT on ContosoMember1 if the optional machine is part of the lab
if (Get-LabVM -ComputerName ContosoMember1 -ErrorAction SilentlyContinue)
    Install-LabWindowsFeature -ComputerName ContosoMember1 -FeatureName RSAT

#Install the Windows-Server-Backup feature on all DCs
Install-LabWindowsFeature -ComputerName (Get-LabVM | Where-Object { $_.Disks }) -FeatureName Windows-Server-Backup

#Install software to all lab machines
$machines = Get-LabVM
Install-LabSoftwarePackage -ComputerName $machines -Path $labSources\SoftwarePackages\Notepad++.exe -CommandLine /S -AsJob
Install-LabSoftwarePackage -ComputerName $machines -Path $labSources\SoftwarePackages\Winrar.exe -CommandLine /S -AsJob
Get-Job -Name 'Installation of*' | Wait-Job | Out-Null

Invoke-LabCommand -ActivityName ADReplicationTopology -ComputerName (Get-LabVM -Role RootDC) -ScriptBlock {
    $rootDc = Get-ADDomainController -Discover
    $childDc = Get-ADDomainController -DomainName -Discover

    $siteDataCenter = New-ADReplicationSite -Name Datacenter -Server $rootDc -PassThru
    $siteDR = New-ADReplicationSite -Name DR -Server $rootDc -PassThru

    Get-ADDomainController -Filter 'Name -like "*DC1"' | Move-ADDirectoryServer -Site $siteDataCenter -Server $rootDc
    Get-ADDomainController -Filter 'Name -like "*DC2"' | Move-ADDirectoryServer -Site $siteDR -Server $rootDc

    Get-ADDomainController -Filter 'Name -like "*DC1"' -Server $childDc | Move-ADDirectoryServer -Site $siteDataCenter -Server $rootDc
    Get-ADDomainController -Filter 'Name -like "*DC2"' -Server $childDc | Move-ADDirectoryServer -Site $siteDR -Server $rootDc

    New-ADReplicationSiteLink -Name 'Datacenter - DR' -Cost 100 -ReplicationFrequencyInMinutes 15 -SitesIncluded $siteDataCenter, $siteDR -OtherAttributes @{ options = 1 } -Server $rootDc

    Remove-ADReplicationSiteLink -Identity 'DEFAULTIPSITELINK' -Confirm:$false -Server $rootDc
    Remove-ADReplicationSite -Identity 'Default-First-Site-Name' -Confirm:$false -Server $rootDc

Sync-LabActiveDirectory -ComputerName (Get-LabVM -Role RootDC)

if ($createCheckpoint)
    Write-Host 'Creating a snapshot of all machines'
    Checkpoint-LabVM -All -SnapshotName 'AfterSetupComplete'

Show-LabDeploymentSummary -Detailed