summaryrefslogtreecommitdiff
path: root/ansible/scripts/setup-tree-domain.ps1
blob: 0f661a146e1e5a287d59d0bc717c3d1797de001a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
param
(
    [string]$ParentForestRootDomain = "contoso.com",
    [string]$NewTreeDomainName      = "msp.org",
    [string]$SafeModePassword       = "P4ssw0rd1234!",
    [string]$Username               = "Administrator",
    [string]$Password               = "packer"
)

$scriptName = $MyInvocation.MyCommand.Name
$logFile = "C:\Logs\${scriptName}_log.txt"
Start-Transcript -Path $logFile -Append

$p = ConvertTo-SecureString $Password -AsPlainText -Force
$c = New-Object System.Management.Automation.PSCredential("$ParentForestRootDomain\$Username", $p)

Write-Host "[INFO] Setting Administrator password"
$computerName = $env:COMPUTERNAME
$adminPassword = $Password
$adminUser = [ADSI] "WinNT://$computerName/Administrator,User"
$adminUser.SetPassword($adminPassword)

Write-Host "[INFO] Installing AD-Domain-Services feature"
Install-WindowsFeature AD-Domain-Services -IncludeAllSubFeature -IncludeManagementTools

Write-Host "[INFO] Importing ADDSDeployment module"
Import-Module ADDSDeployment

try {
    Write-Host "[INFO] Installing New Tree Domain in Existing Forest"
    Install-ADDSDomain `
        -InstallDns `
        -ParentDomainName $ParentForestRootDomain `
        -NewDomainName $NewTreeDomainName `
        -DomainType TreeDomain `
        -DatabasePath "C:\Windows\NTDS" `
        -LogPath "C:\Windows\NTDS" `
        -SysvolPath "C:\Windows\SYSVOL" `
        -NoRebootOnCompletion `
        -Force `
        -Credential $c `
        -SafeModeAdministratorPassword (ConvertTo-SecureString -AsPlainText -Force "$SafeModePassword")

    Write-Host "[INFO] Successfully added new tree domain: $NewTreeDomainName"
} catch {
    Write-Host "[ERR] Failed to add new tree domain: $NewTreeDomainName"
    Write-Host $_.Exception.Message
}

Stop-Transcript