Public/Get-PAServer.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
function Get-PAServer {
    [CmdletBinding()]
    [OutputType('PoshACME.PAServer')]
    param(
        [Parameter(ParameterSetName='Specific',Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [ValidateScript({Test-ValidDirUrl $_ -ThrowOnFail})]
        [Alias('location')]
        [string]$DirectoryUrl,
        [Parameter(ParameterSetName='List',Mandatory)]
        [switch]$List,
        [switch]$Refresh
    )

    Process {

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

            # update from the server first if requested
            if ($Refresh) {
                Get-PAServer -List | Update-PAServer
            }

            # read the contents of each server's dir.json
            Write-Debug "Loading PAServer list from disk"
            $rawFiles = Get-ChildItem (Join-Path (Get-ConfigRoot) '\*\dir.json') | Get-Content -Raw
            $rawFiles | ConvertFrom-Json | Sort-Object location | ForEach-Object {

                    # insert the type name so it displays properly
                    $_.PSObject.TypeNames.Insert(0,'PoshACME.PAServer')

                    # send the result to the pipeline
                    Write-Output $_
            }

        # Specific mode
        } else {

            if ($DirectoryUrl) {

                # convert WellKnown names to their associated Url
                if ($DirectoryUrl -notlike 'https://*') {
                    $DirectoryUrl = $script:WellKnownDirs.$DirectoryUrl
                }

                # build the path to dir.json
                $dirFolder = ConvertTo-DirFolder $DirectoryUrl
                $dirFile = Join-Path $dirFolder 'dir.json'

                # check if it exists
                if (Test-Path $dirFile -PathType Leaf) {
                    Write-Debug "Loading PAServer from disk"
                    $dir = Get-ChildItem $dirFile | Get-Content -Raw | ConvertFrom-Json
                    $dir.PSObject.TypeNames.Insert(0,'PoshACME.PAServer')
                } else {
                    Write-Warning "Unable to find cached PAServer info for $DirectoryUrl. Try using Set-PAServer first."
                    return $null
                }

            } else {
                # just use the current one
                $dir = $script:Dir
            }

            if ($dir -and $Refresh) {

                # update and then recurse to return the updated data
                Update-PAServer $dir.location
                return (Get-PAServer $dir.location)

            } else {

                # just return whatever we've got
                return $dir
            }
        }
    }





    <#
    .SYNOPSIS
        Get ACME server details.
 
    .DESCRIPTION
        The primary use for this function is checking which ACME server is currently configured. New Account and Cert requests will be directed to this server. It may also be used to refresh server details and list additional servers that have previously been used.
 
    .PARAMETER DirectoryUrl
        Either the URL to an ACME server's "directory" endpoint or one of the supported short names. Currently supported short names include LE_PROD (LetsEncrypt Production v2) and LE_STAGE (LetsEncrypt Staging v2).
 
    .PARAMETER List
        If specified, the details for all previously used servers will be returned.
 
    .PARAMETER Refresh
        If specified, any server details returned will be freshly queried from the ACME server. Otherwise, cached details will be returned.
 
    .EXAMPLE
        Get-PAServer
 
        Get cached ACME server details for the currently selected server.
 
    .EXAMPLE
        Get-PAServer -DirectoryUrl LE_PROD
 
        Get cached LetsEncrypt production server details using the short name.
 
    .EXAMPLE
        Get-PAServer -List
 
        Get all cached ACME server details.
 
    .EXAMPLE
        Get-PAServer -DirectoryUrl https://myacme.example.com/directory
 
        Get cached ACME server details for the specified directory URL.
 
    .EXAMPLE
        Get-PAServer -Refresh
 
        Get fresh ACME server details for the currently selected server.
 
    .EXAMPLE
        Get-PAServer -List -Refresh
 
        Get fresh ACME server details for all previously used servers.
 
    .LINK
        Project: https://github.com/rmbolger/Posh-ACME
 
    .LINK
        Set-PAServer
 
    #>

}