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
function Get-PAOrder {
    [CmdletBinding()]
    [OutputType('PoshACME.PAOrder')]
    param(
        [Parameter(ParameterSetName='Specific',Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [string]$MainDomain,
        [Parameter(ParameterSetName='Specific',ValueFromPipelineByPropertyName)]
        [ValidateScript({Test-ValidFriendlyName $_ -ThrowOnFail})]
        [string]$Name,
        [Parameter(ParameterSetName='List',Mandatory)]
        [switch]$List,
        [switch]$Refresh
    )

    Begin {
        # Make sure we have an account configured
        if (-not ($acct = Get-PAAccount)) {
            try { throw "No ACME account configured. Run Set-PAAccount or New-PAAccount first." }
            catch { $PSCmdlet.ThrowTerminatingError($_) }
        }
    }

    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"
            $orders = Get-ChildItem (Join-Path $acct.Folder '\*\order.json') | ForEach-Object {

                # parse the json
                $order = $_ | Get-Content -Raw | ConvertFrom-Json

                # 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

                # rename pre-4.x DnsPlugin parameter to Plugin
                if ('DnsPlugin' -in $order.PSObject.Properties.Name) {
                    $order | Add-Member 'Plugin' $order.DnsPlugin -Force
                    $order.PSObject.Properties.Remove('DnsPlugin')
                }

                # de-obfuscate PfxPassB64U
                if ('PfxPassB64U' -in $order.PSObject.Properties.Name) {
                    $order | Add-Member 'PfxPass' ($order.PfxPassB64U | ConvertFrom-Base64Url) -Force
                    $order.PSObject.Properties.Remove('PfxPassB64U')
                }

                # add the dynamic Name and Folder property
                $order | Add-Member 'Name' $_.Directory.Name -Force
                $order | Add-Member 'Folder' $_.Directory.FullName -Force

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

            return ($orders | Sort-Object MainDomain)

        # Specific mode
        } else {

            if ($MainDomain) {

                # filter the normal list output
                $matchingOrders = Get-PAOrder -List | Sort-Object -Descending expires |
                    Where-Object { $_.MainDomain -eq $MainDomain }

                # if Name was also specified, there should only ever be 0 or 1 match, otherwise
                # pick the first match to retain script compatibility with 4.6 and earlier.
                if ($Name) {
                    $order = $matchingOrders | Where-Object { $_.Name -eq $Name }
                } else {
                    $order = $matchingOrders | Select-Object -First 1
                }

            } elseif ($Name) {
                # try to match the name which should be unique
                $order = Get-PAOrder -List | Where-Object { $_.Name -eq $Name }

            } 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
                Get-PAOrder -List | Where-Object { $_.Name -eq $order.Name }

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

        }
    }
}