Public/Get-PAOrder.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
function Get-PAOrder {
    [CmdletBinding()]
    [OutputType('PoshACME.PAOrder')]
    param(
        [Parameter(ParameterSetName='Specific',Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [string]$MainDomain,
        [Parameter(ParameterSetName='List',Mandatory)]
        [switch]$List,
        [switch]$Refresh
    )

    Begin {
        # Make sure we have an account configured
        if (!(Get-PAAccount)) {
            throw "No ACME account configured. Run Set-PAAccount or New-PAAccount first."
        }
    }

    Process {

        # List mode
        if ('List' -eq $PSCmdlet.ParameterSetName) {

            # update from the server first if requested
            if ($Refresh) {
                Write-Debug "Refreshing orders"
                Get-PAOrder -List  | Update-PAOrder
            }

            # read the contents of each order's order.json
            Write-Debug "Loading PAOrder list from disk"
            $rawOrders = Get-ChildItem "$($script:AcctFolder)\*\order.json" | Get-Content -Raw
            $orders = $rawOrders | ConvertFrom-Json | Sort-Object MainDomain | ForEach-Object {

                # fix any dates that may have been parsed by PSCore's JSON serializer
                $_.expires = Repair-ISODate $_.expires
                $_.CertExpires = Repair-ISODate $_.CertExpires
                $_.RenewAfter = Repair-ISODate $_.RenewAfter

                # insert the type name and send the results to the pipeline
                $_.PSObject.TypeNames.Insert(0,'PoshACME.PAOrder')
                $_
            }

            return $orders

        # Specific mode
        } else {

            if ($MainDomain) {

                # build the path to order.json
                $domainFolder = Join-Path $script:AcctFolder $MainDomain.Replace('*','!')
                $orderFile =  Join-Path $domainFolder 'order.json'

                # check for an order.json
                if (Test-Path $orderFile -PathType Leaf) {

                    Write-Debug "Loading PAOrder from disk"
                    $order = Get-ChildItem $orderFile | Get-Content -Raw | ConvertFrom-Json
                    $order.PSObject.TypeNames.Insert(0,'PoshACME.PAOrder')

                    # fix any dates that may have been parsed by PSCore's JSON serializer
                    $order.expires = Repair-ISODate $order.expires
                    $order.CertExpires = Repair-ISODate $order.CertExpires
                    $order.RenewAfter = Repair-ISODate $order.RenewAfter

                } else {
                    return $null
                }

            } else {
                # just use the current one
                $order = $script:Order
            }

            if ($order -and $Refresh) {

                # update and then recurse to return the updated data
                Update-PAOrder $order.MainDomain
                return (Get-PAOrder $order.MainDomain)

            } else {
                # just return whatever we've got
                return $order
            }

        }
    }





    <#
    .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 MainDomain
        The primary domain associated with the order. This is the domain that goes in the certificate's subject.
 
    .PARAMETER List
        If specified, the details for all orders will be returned.
 
    .PARAMETER Refresh
        If specified, any order details returned will be freshly queried from the ACME server. Otherwise, cached details will be returned.
 
    .EXAMPLE
        Get-PAOrder
 
        Get cached ACME order details for the currently selected order.
 
    .EXAMPLE
        Get-PAOrder site.example.com
 
        Get cached ACME order details for the specified domain.
 
    .EXAMPLE
        Get-PAOrder -List
 
        Get all cached ACME order details.
 
    .EXAMPLE
        Get-PAOrder -Refresh
 
        Get fresh ACME order details for the currently selected order.
 
    .EXAMPLE
        Get-PAOrder -List -Refresh
 
        Get fresh ACME order details for all orders.
 
    .LINK
        Project: https://github.com/rmbolger/Posh-ACME
 
    .LINK
        Set-PAOrder
 
    .LINK
        New-PAOrder
 
    #>

}