DSCResources/xSelfSignedCertificate/xSelfSignedCertificate.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

Import-Module "$PSScriptRoot\..\..\bin\DSCHelper.psm1" -Force;

Function Get-TargetResource {
    Param(
        [Parameter(Mandatory = $true)][System.String]$CommonName,
        [Parameter(Mandatory = $true)][System.String]$FriendlyName,
        [Parameter(Mandatory = $false)][Switch]$IsDSCCert,
        [Parameter(Mandatory = $false)][System.String]$Password,
        [Parameter(Mandatory = $false)][System.String]$PFXPath,
        [Parameter(Mandatory = $false)][System.String]$CERPath,
        [Parameter(Mandatory = $false)][System.String]$ThumbprintPath,
        [Parameter(Mandatory = $true)][ValidateSet("Present", "Absent")][System.String]$Ensure);
    $script:lastResult = $null;
    if (Test-DSCCert -commonName $CommonName -friendlyName $FriendlyName) {
        $script:lastResult = @{
            CommonName    = $CommonName;
            FriendlyName  = $FriendlyName;
            Password      = "Redacted";
            IsDSCCert     = $IsDSCCert;
            PFXPath       = $PFXPath;
            CERPath       = $CERPath;
            ThumprintPath = $ThumbprintPath;
            Ensure        = "Present";
        }
    }
    else {
        $script:lastResult = @{
            CommonName     = $CommonName;
            FriendlyName   = $FriendlyName;
            Password       = "Redacted";
            IsDSCCert      = $IsDSCCert;
            PFXPath        = $PFXPath;
            CERPath        = $CERPath;
            ThumbprintPath = $ThumbprintPath;
            Ensure         = "Absent";
        }
    }
    return $script:lastResult;
}

Function Set-TargetResource {
    Param(
        [Parameter(Mandatory = $true)][System.String]$CommonName,
        [Parameter(Mandatory = $true)][System.String]$FriendlyName,
        [Parameter(Mandatory = $false)][Switch]$IsDSCCert,
        [Parameter(Mandatory = $false)][System.String]$Password,
        [Parameter(Mandatory = $false)][System.String]$PFXPath,
        [Parameter(Mandatory = $false)][System.String]$CERPath,
        [Parameter(Mandatory = $false)][System.String]$ThumbprintPath,
        [Parameter(Mandatory = $true)][ValidateSet("Present", "Absent")][System.String]$Ensure);
    if ($Ensure -ieq "Present") {
        New-DSCCert -commonName $CommonName -friendlyName $FriendlyName -IsDSCCert:$ISDSCCert;
        if (![string]::IsNullOrEmpty($PFXPath)) {
            Export-DSCCertPrivateKey -commonName $CommonName -fileName $PFXPath -Password $password;
        }
        if (![string]::IsNullOrEmpty($CERPath)) {
            Export-DSCCertPublicKey -commonName $CommonName -fileName $CERPath -thumbprintPath $ThumbprintPath;
        }
    }
    else {
        Remove-DSCCert -commonName $CommonName -PFXPath $PFXPath -CERPath $CERPath;
    }
}

Function Test-TargetResource {
    Param(
        [Parameter(Mandatory = $true)][System.String]$CommonName,
        [Parameter(Mandatory = $true)][System.String]$FriendlyName,
        [Parameter(Mandatory = $false)][Switch]$IsDSCCert,
        [Parameter(Mandatory = $false)][System.String]$Password,
        [Parameter(Mandatory = $false)][System.String]$PFXPath,
        [Parameter(Mandatory = $false)][System.String]$CERPath,
        [Parameter(Mandatory = $false)][System.String]$ThumbprintPath,
        [Parameter(Mandatory = $true)][ValidateSet("Present", "Absent")][System.String]$Ensure);
    $script:lastResult = (Test-DSCCert -commonName $CommonName -PFXPath $PFXPath -CERPath $CERPath -ThumbprintPath $ThumbprintPath);
    if ($Ensure -ieq "Present") {
        return $script:lastResult;
    }
    else {
        return !$script:lastResult;
    }
}

Export-ModuleMember -Function *-TargetResource;