DSCResources/COMMUNITY_ADCSTemplate/COMMUNITY_ADCSTemplate.psm1

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $Displayname
    )

    If (Get-ADCSTemplate -DisplayName $DisplayName) {
        Return @{DisplayName = $Displayname}
    } Else {
        Return @{DisplayName = $null}
    }
}


function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $Displayname,

        [System.String]
        $JSON,

        [System.String[]]
        $Identity,

        [System.Boolean]
        $Publish,

        [System.Boolean]
        $AutoEnroll,

        [ValidateSet("Present","Absent")]
        [System.String]
        $Ensure
    )

    If ($Ensure -eq 'Present') {

        Write-Verbose "[ADCS] Creating template $DisplayName"
        $PSBoundParameters.Remove('Ensure')
        $PSBoundParameters.Remove('Verbose')
        New-ADCSTemplate @PSBoundParameters

    } Else {

        Write-Verbose "[ADCS] Removing template $DisplayName"
        Remove-ADCSTemplate $DisplayName -Confirm:$false

    }
}


function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $Displayname,

        [parameter(Mandatory = $true)]
        [System.String]
        $JSON,

        [System.String[]]
        $Identity,

        [System.Boolean]
        $Publish,

        [System.Boolean]
        $AutoEnroll,

        [ValidateSet("Present","Absent")]
        [System.String]
        $Ensure
    )

    # Simple test for existence. Does not validate all template settings or permissions or publishing.
    If (Get-ADCSTemplate -DisplayName $DisplayName) {
        Write-Verbose "[ADCS] Template $DisplayName Present. Should be $Ensure."
        If ($ensure -eq 'Present') {Return $true} Else {Return $false}
    } Else {
        Write-Verbose "[ADCS] Template $DisplayName Absent. Should be $Ensure."
        If ($ensure -eq 'Present') {Return $false} Else {Return $true}
    }

}


Export-ModuleMember -Function *-TargetResource