DSCResources/xSelfSignedCertificate/xSelfSignedCertificate.psm1


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)][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;
            PFXPath       = $PFXPath;
            CERPath       = $CERPath;
            ThumprintPath = $ThumbprintPath;
            Ensure        = "Present";
        }
    }
    else {
        $script:lastResult = @{
            CommonName     = $CommonName;
            FriendlyName   = $FriendlyName;
            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)][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;
        if (![string]::IsNullOrEmpty($PFXPath)) {
            Export-DSCCertPrivateKey -commonName $CommonName -fileName $PFXPath;
        }
        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)][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;