Public/Remove-PAAccount.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
function Remove-PAAccount {
    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [ValidateScript({Test-ValidFriendlyName $_ -ThrowOnFail})]
        [Alias('Name')]
        [string]$ID,
        [switch]$Deactivate,
        [switch]$Force
    )

    Begin {
        # make sure we have a server configured
        if (-not ($server = Get-PAServer)) {
            try { throw "No ACME server configured. Run Set-PAServer first." }
            catch { $PSCmdlet.ThrowTerminatingError($_) }
        }
    }

    Process {
        # grab a copy of the account
        if (-not ($acct = Get-PAAccount -ID $ID)) {
            Write-Warning "Specified account ID ($ID) was not found."
            return
        }

        # deactivate first, if asked
        if ($Deactivate -and $acct.status -eq 'valid') {
            $acct | Set-PAAccount -Deactivate -NoSwitch -Force:$Force.IsPresent
        }

        # confirm deletion unless -Force was used
        if (-not $Force) {
            $msg = "Deleting an account will also delete all local copies of orders and certificates. But they may still exist on the ACME server."
            $question = "Are you sure you wish to delete account $($acct.id)?"
            if (-not $PSCmdlet.ShouldContinue($question,$msg)) {
                Write-Verbose "Deletion aborted for account $($acct.id)."
                return
            }
        }

        Write-Verbose "Deleting account $($acct.id)"

        # delete the account's folder
        $acctFolder = Join-Path $server.Folder $acct.id
        Remove-Item $acctFolder -Force -Recurse

        # unset the current account if it was this one
        if ($script:Acct -and $script:Acct.id -eq $acct.id) {
            $acct = $null
            Remove-Item (Join-Path $server.Folder 'current-account.txt') -Force
            Import-PAConfig -Level 'Account'
        }

    }
}