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(
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [String]

        [Parameter(Mandatory = $true, Position = 1)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Server,

        [Parameter(Mandatory = $false, Position = 2)]
        [ValidateNotNullOrEmpty()]
        [int]
        $Port = 25,

        [Parameter(Mandatory = $true, Position = 3)]
        [ValidateNotNullOrEmpty()]
        [String]
        $SenderAddress,

        [Parameter(Mandatory = $true, Position = 4)]
        [ValidateNotNullOrEmpty()]
        [String]
        $FromName,

        [Parameter(Mandatory = $true, Position = 5)]
        [ValidateNotNullOrEmpty()]
        [String]
        $FromEmail,

        [Parameter(Mandatory = $true, Position = 6)]
        [ValidateNotNullOrEmpty()]
        [String]
        $ToName,

        [Parameter(Mandatory = $true, Position = 7)]
        [ValidateNotNullOrEmpty()]
        [String]
        $ToEmail,

        [Parameter(Mandatory = $false, Position = 8)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Path = "./email-config.xml",

        [Parameter(Mandatory = $false, Position = 9)]
        [String]
        $Username,

        [Parameter(Mandatory = $false, Position = 10)]
        [String]
        $Password
    )
    begin {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
    }

    end {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
    }

    process {
        Write-Debug "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"
        $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
    }
}