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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<#
    .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
    }
}