functions/import-d365rsatselfservicecertificates.ps1

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

<#
    .SYNOPSIS
        Import certificates for RSAT
         
    .DESCRIPTION
        Import the certificates for RSAT into the correct stores and display the thumbprint
         
        When working with self-service environments you need to download a zip file from LCS. The zip file needs to be unblocked and then extracted into a folder, with only the .cer and the .pxf files inside
         
    .PARAMETER Path
        Path to the folder where the .cer and .pxf files are located
         
        The files needs to be extracted from the zip archive
         
    .PARAMETER Password
        Password for the .pxf file
         
        Working with self-service environments, the password will be displayed during the download of the zip archive
         
    .EXAMPLE
        PS C:\> Import-D365RsatSelfServiceCertificates -Path "C:\Temp\UAT" -Password "123456789"
         
        This will import the .cer and .pxf files into the correct stored, bases on the files located in "C:\Temp\UAT".
        After import it will display the thumbprint for both certificates.
         
        Sample output:
        [23:43:05][Import-D365RsatSelfServiceCertificates] Pfx Thumbprint: B4D6921321434235463463414312343253523A05
        [23:43:05][Import-D365RsatSelfServiceCertificates] Cert Thumbprint: B4D6921321434235463463414312343253523A05
         
    .NOTES
        Author: Mötz Jensen (@Splaxi)
#>

function Import-D365RsatSelfServiceCertificates {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "")]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "")]
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        $Path,

        [Parameter(Mandatory = $true)]
        $Password
    )
    
    begin {
        [Security.SecureString] $PasswordSecure = (ConvertTo-SecureString -String $Password -Force -AsPlainText)

        if (-not (Test-PathExists -Path $Path -Type Container)) { return }
    }
    
    process {
        if (Test-PSFFunctionInterrupt) { return }

        $pathCertFile = (Get-ChildItem -Path "$Path\*.cer" | Select-Object -First 1).FullName
        $pathPfxFile = (Get-ChildItem -Path "$Path\*.pfx" | Select-Object -First 1).FullName

        if (-not $pathCertFile -or -not $pathPfxFile) {
            $messageString = "One of the certificate files are <c='em'>missing</c>. Make sure that the path you supplied contains a set of <c='em'>.cer</c> and <c='em'>.pxf</c> certificate files."
            Write-PSFMessage -Level Host -Message $messageString
            Stop-PSFFunction -Message "Stopping because an generic error message." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', '')))
            return
        }

        $pxfCert = Import-PfxCertificate -FilePath $pathPfxFile -CertStoreLocation "Cert:\LocalMachine\Root" -Password $PasswordSecure
        Import-PfxCertificate -FilePath $pathPfxFile -CertStoreLocation "Cert:\LocalMachine\My" -Password $PasswordSecure > $null
        $cert = Import-Certificate -FilePath $pathCertFile -CertStoreLocation "Cert:\LocalMachine\Root"
        Import-Certificate -FilePath $pathCertFile -CertStoreLocation "Cert:\LocalMachine\My" > $null

        Write-PSFMessage -Level Host -Message "Pfx Thumbprint: <c='em'>$($pxfCert.Thumbprint)</c>"
        Write-PSFMessage -Level Host -Message "Cert Thumbprint: <c='em'>$($cert.Thumbprint)</c>"

    }
    
    end {
        
    }
}