Plugins/ManualWithEmail.ps1

function Get-CurrentPluginType { 'dns-01' }

function Add-DnsTxt {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$TxtValue,
        [Parameter(Mandatory)]
        [string]$EmailFrom,
        [Parameter(Mandatory)]
        [string]$EmailTo,
        [Parameter(Mandatory)]
        [string]$EmailSmtpServer,
        [string]$EmailSubject = 'DNS TXT Records from Posh-ACME',
        [int]$EmailSmtpPort = 25,
        [switch]$EmailUseSsl,
        [switch]$ManualNonInteractive,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    Write-Verbose "Saving TXT record to display when Save-DnsTxt is called."
    if (!$script:ManualTxtAdd) { $script:ManualTxtAdd = @() }
    $script:ManualTxtAdd += [pscustomobject]@{Record=$RecordName;TxtValue=$TxtValue}

    <#
    .SYNOPSIS
        Stores the TXT record to display when Save-DnsTxt is called.
 
    .DESCRIPTION
        This plugin requires user interaction and should not be used for any certificates that require automated renewals. Renewal operations will skip these.
 
    .PARAMETER RecordName
        The fully qualified name of the TXT record.
 
    .PARAMETER TxtValue
        The value of the TXT record.
 
    .PARAMETER EmailFrom
        The From address for the email.
 
    .PARAMETER EmailTo
        The To address for the email.
 
    .PARAMETER EmailSmtpServer
        The SMTP server IP address or hostname.
 
    .PARAMETER EmailSubject
        A custom subject line for the email.
 
    .PARAMETER EmailSmtpPort
        A custom SMTP port number. Defaults to 25.
 
    .PARAMETER EmailUseSsl
        If specified, connect to SMTP using SSL/TLS.
 
    .PARAMETER ManualNonInteractive
        If set, prevents user-prompts. Useful for automation scenarios where user input is not possible.
 
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
 
    .EXAMPLE
        Add-DnsTxt '_acme-challenge.example.com' 'txt-value' -EmailFrom me@example.com -EmailTo you@example.com -EmailSmtpServer smtp.example.com
 
        Stores TXT record data for the specified site with the specified value.
    #>

}

function Remove-DnsTxt {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$TxtValue,
        [Parameter(Mandatory)]
        [string]$EmailFrom,
        [Parameter(Mandatory)]
        [string]$EmailTo,
        [Parameter(Mandatory)]
        [string]$EmailSmtpServer,
        [string]$EmailSubject = 'DNS TXT Records from Posh-ACME',
        [int]$EmailSmtpPort = 25,
        [switch]$EmailUseSsl,
        [switch]$ManualNonInteractive,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    Write-Verbose "Saving TXT record to display when Save-DnsTxt is called."
    if (!$script:ManualTxtRemove) { $script:ManualTxtRemove = @() }
    $script:ManualTxtRemove += [pscustomobject]@{Record=$RecordName;TxtValue=$TxtValue}

    <#
    .SYNOPSIS
        Stores the TXT record to display when Save-DnsTxt is called.
 
    .DESCRIPTION
        This plugin requires user interaction and should not be used for any certificates that require automated renewals. Renewal operations will skip these.
 
    .PARAMETER RecordName
        The fully qualified name of the TXT record.
 
    .PARAMETER TxtValue
        The value of the TXT record.
 
    .PARAMETER EmailFrom
        The From address for the email.
 
    .PARAMETER EmailTo
        The To address for the email.
 
    .PARAMETER EmailSmtpServer
        The SMTP server IP address or hostname.
 
    .PARAMETER EmailSubject
        A custom subject line for the email.
 
    .PARAMETER EmailSmtpPort
        A custom SMTP port number. Defaults to 25.
 
    .PARAMETER EmailUseSsl
        If specified, connect to SMTP using SSL/TLS.
 
    .PARAMETER ManualNonInteractive
        If set, prevents user-prompts. Useful for automation scenarios where user input is not possible.
 
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
 
    .EXAMPLE
        Remove-DnsTxt '_acme-challenge.example.com' 'txt-value' -EmailFrom me@example.com -EmailTo you@example.com -EmailSmtpServer smtp.example.com
 
        Stores TXT record data for the specified site with the specified value.
    #>

}

function Save-DnsTxt {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string]$EmailFrom,
        [Parameter(Mandatory)]
        [string]$EmailTo,
        [Parameter(Mandatory)]
        [string]$EmailSmtpServer,
        [string]$EmailSubject = 'DNS TXT Records from Posh-ACME',
        [int]$EmailSmtpPort = 25,
        [switch]$EmailUseSsl,
        [switch]$ManualNonInteractive,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    if ($script:ManualTxtAdd -and $script:ManualTxtAdd.Count -gt 0) {

        $recs = $script:ManualTxtAdd | ForEach-Object {
            "$($_.Record) -> $($_.TxtValue)"
        }
        $recText = $recs -join "`n"

        Invoke-InformUser -Action 'create' -RecordText $recText @PSBoundParameters

        # clear out the variable so we don't notify twice
        Remove-Variable ManualTxtAdd -Scope Script

        if (-not $ManualNonInteractive) {
            Read-Host -Prompt "Press any key to continue." | Out-Null
        }
        else {
            Write-Host "Non-interactive mode, starting wait."
            Write-Host
        }
    }

    if ($script:ManualTxtRemove -and $script:ManualTxtRemove.Count -gt 0) {

        $recs = $script:ManualTxtRemove | ForEach-Object {
            "$($_.Record) -> $($_.TxtValue)"
        }
        $recText = $recs -join "`n"

        Invoke-InformUser -Action 'remove' -RecordText $recText @PSBoundParameters

        # clear out the variable so we don't notify twice
        Remove-Variable ManualTxtRemove -Scope Script
    }

    <#
    .SYNOPSIS
        Displays the TXT records that need to be manually created or removed by the user.
 
    .DESCRIPTION
        This function outputs the pending TXT records to be created and waits for user confirmation to continue.
 
    .PARAMETER EmailFrom
        The From address for the email.
 
    .PARAMETER EmailTo
        The To address for the email.
 
    .PARAMETER EmailSmtpServer
        The SMTP server IP address or hostname.
 
    .PARAMETER EmailSubject
        A custom subject line for the email.
 
    .PARAMETER EmailSmtpPort
        A custom SMTP port number. Defaults to 25.
 
    .PARAMETER EmailUseSsl
        If specified, connect to SMTP using SSL/TLS.
 
    .PARAMETER ManualNonInteractive
        If set, prevents user-prompts. Useful for automation scenarios where user input is not possible.
 
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
    #>

}

function Invoke-InformUser {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string]$Action,
        [Parameter(Mandatory)]
        [string]$RecordText,
        [Parameter(Mandatory)]
        [string]$EmailFrom,
        [Parameter(Mandatory)]
        [string]$EmailTo,
        [Parameter(Mandatory)]
        [string]$EmailSmtpServer,
        [string]$EmailSubject = 'DNS TXT Records from Posh-ACME',
        [int]$EmailSmtpPort = 25,
        [switch]$EmailUseSsl,
        [switch]$ManualNonInteractive,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraInformParams
    )

    $msgTemplate = @'
 
Please {0} the following TXT records:
------------------------------------------
{1}
------------------------------------------
 
'@


    $msg = $msgTemplate -f $Action,$RecordText

    # write to the console
    Write-Host $msg

    # send the email
    $sendParams = @{
        To = $EmailTo
        From = $EmailFrom
        Subject = $EmailSubject
        Body = $msg
        SmtpServer = $EmailSmtpServer
        UseSsl = $EmailUseSsl.IsPresent
    }
    Send-MailMessage @sendParams

}