Public/New-EmailConfig.ps1

<#
    .SYNOPSIS
    Create a new XML file that contains the email configuration.

    .DESCRIPTION
    Supports encryptiong of server credentaials using Export-Clixml.

    .INPUTS
    None. You cannot pipe objects to New-EmailConfig.

    .OUTPUTS
    None.

    .PARAMETER Server
    .PARAMETER Port
    .PARAMETER SenderAddress
    .PARAMETER FromName
    .PARAMETER FromEmail
    .PARAMETER ToName
    .PARAMETER ToEmail
    .PARAMETER Username
    .PARAMETER Password

    .EXAMPLE

    .LINK

    .NOTES
#>

function New-EmailConfig {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword', '', Justification='needed to collect')]
    param(
        [ValidateNotNullOrEmpty()]
        [String]$Server,
        [ValidateNotNullOrEmpty()]
        [int]$Port = 25,
        [ValidateNotNullOrEmpty()]
        [String]$SenderAddress,
        [ValidateNotNullOrEmpty()]
        [String]$FromName,
        [ValidateNotNullOrEmpty()]
        [String]$FromEmail,
        [ValidateNotNullOrEmpty()]
        [String]$ToName,
        [ValidateNotNullOrEmpty()]
        [String]$ToEmail,
        [ValidateNotNullOrEmpty()]
        [String]
        $Path = "./email-config.xml",
        [String]$Username,
        [String]$Password
    )
    $p = @{
        Server = $Server
        Port = $Port
        SenderAddress = $SenderAddress
        FromName = $FromName
        FromEmail = $FromEmail
        ToName = $ToName
        ToEmail = $ToEmail
    }
    if ($PSBoundParameters.ContainsKey('Username') -and $PSBoundParameters.ContainsKey('Password')) {
        $p["Credentials"] = New-Object System.Management.Automation.PSCredential ($Username, (ConvertTo-SecureString -String $Password -AsPlainText -Force))
    }
    ([PSCustomObject]$p) | Export-Clixml -Path $Path | Out-Null
}