Public/Export-PAAccountKey.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
function Export-PAAccountKey {
    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Position=0)]
        [string]$ID,
        [Parameter(Mandatory)]
        [string]$OutputFile,
        [switch]$Force
    )

    try {

        # make sure we have a server configured
        if (-not (Get-PAServer)) {
            throw "No ACME server configured. Run Set-PAServer first."
        }

        # throw an error if there's no current account and no ID passed in
        if (-not $ID -and -not ($acct = Get-PAAccount)) {
            throw "No ACME account configured. Run New-PAAccount or specify an account ID."
        }

        # make sure the ID is valid if specified
        if ($ID -and -not ($acct = Get-PAAccount $ID)) {
            throw "Invalid account ID: $ID"
        }

        # confirm overwrite unless -Force was specified
        if (-not $Force -and (Test-Path $OutputFile -PathType Leaf) -and
            -not $PSCmdlet.ShouldContinue("Overwrite?","$OutputFile already exists.")
        ) {
            Write-Verbose "Export account key aborted."
            return
        }

        Write-Verbose "Exporting account $($acct.id) ($($acct.KeyLength)) to $OutputFile"

        # convert the JWK to a BC keypair
        $keypair = $acct.key | ConvertFrom-Jwk -AsBC

        # export it
        Export-Pem $keypair $OutputFile

    }
    catch {
        $PSCmdlet.ThrowTerminatingError($_)
    }


    <#
    .SYNOPSIS
        Get ACME order details.
 
    .DESCRIPTION
        Returns details such as Domains, key length, expiration, and status for one or more ACME orders previously created.
 
    .PARAMETER ID
        The ACME account ID value.
 
    .PARAMETER OutputFile
        The path to the file to write the key data to.
 
    .PARAMETER Force
        If specified and the output file already exists, it will be overwritten. Without the switch, a confirmation prompt will be presented.
 
    .EXAMPLE
        Export-PAAccountKey -OutputFile .\mykey.pem
 
        Exports the current ACME account's key to the specified file.
 
    .EXAMPLE
        Export-PAAccountKey 12345 -OutputFile .\mykey.pem -Force
 
        Exports the specified ACME account's key to the specified file and overwrites it if necessary.
 
    .EXAMPLE
        $fldr = Join-Path ([Environment]::GetFolderPath('Desktop')) 'AcmeAccountKeys'
        PS C:\>New-Item -ItemType Directory -Force -Path $fldr | Out-Null
        PS C:\>Get-PAAccount -List | %{
        PS C:\> Export-PAAccountKey $_.ID -OutputFile "$fldr\$($_.ID).key" -Force
        PS C:\>}
 
        Backup all account keys for this ACME server to a folder on the desktop.
 
    .LINK
        Project: https://github.com/rmbolger/Posh-ACME
 
    .LINK
        Get-PAAccount
 
    #>

}