Public/New-EmailConfig.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
<#
    .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
}