summaryrefslogtreecommitdiff
path: root/ansible/scripts/setup-adcs.ps1
blob: 134b9a98112f07c78689ed1dddcf6659ae075f21 (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]$DomainName = "contoso.com",
    [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("$DomainName\$Username", $p)
$CACommonName = "$($DomainName.Split(".")[0].ToUpper())-CA"

try {
    Install-WindowsFeature -Name AD-Certificate -IncludeAllSubFeature -IncludeManagementTools
    Install-WindowsFeature -Name ADCS-Cert-Authority
    Install-WindowsFeature -Name ADCS-Web-Enrollment
    Install-WindowsFeature -Name RSAT

    Write-Host "[INFO] Installed ADCS Windows Features"
} catch {
    Write-Host "[ERR] Failed to install ADCS Windows Features"
}

try {
    Install-AdcsCertificationAuthority `
        -Credential $c `
        -CAType EnterpriseRootCA `
        -CryptoProviderName "RSA#Microsoft Software Key Storage Provider" `
        -KeyLength 2048 `
        -HashAlgorithmName SHA256 `
        -ValidityPeriod Years `
        -ValidityPeriodUnits 5 `
        -CACommonName $CACommonName `
        -Force

    Write-Host "[INFO] Installed ADCS Certification Authority"
} catch {
    Write-Host "[ERR] Failed to install ADCS Certification Authority"
}

try {
    Install-AdcsWebEnrollment -Force

    Write-Host "[INFO] Installed ADCS Web Enrollment"
} catch {
    Write-Host "[ERR] Failed to install ADCS Web Enrollment"
}
Stop-Transcript