Functions/Admin/Set-RsEmailSettings.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Copyright (c) 2016 Microsoft Corporation. All Rights Reserved.
# Licensed under the MIT License (MIT)

function Set-RsEmailSettings
{
    <#
        .SYNOPSIS
            This script configures the Email Settings for SQL Server Reporting Services.
        
        .DESCRIPTION
            This script configures the Email Settings for SQL Server Reporting Services.
        
        .PARAMETER Authentication
            Default: Ntlm
            The authentication mode the Report Server should be using when sending email.
            
            Note: When using Basic Authentication, the 'EmailCredential' parameter becomes mandatory.
            Note2: When using one of the three predefined authentication-specific aliases without explicitly specifying this parameter, the default Authentication type will be changed to the one contained in the Alias name.
        
        .PARAMETER SmtpServer
            Specify the SMTP Server address.
        
        .PARAMETER SenderAddress
            Specify sender email address for the email.
        
        .PARAMETER EmailCredential
            Specify credentials to specify when connecting to the SMTP server using Basic Authentication.
        
        .PARAMETER ReportServerInstance
            Specify the name of the SQL Server Reporting Services Instance.
            Use the "Connect-RsReportServer" function to set/update a default value.
        
        .PARAMETER ReportServerVersion
            Specify the version of the SQL Server Reporting Services Instance.
            Use the "Connect-RsReportServer" function to set/update a default value.
        
        .PARAMETER ComputerName
            The Report Server to target.
            Use the "Connect-RsReportServer" function to set/update a default value.
        
        .PARAMETER Credential
            Specify the credentials to use when connecting to the Report Server.
            Use the "Connect-RsReportServer" function to set/update a default value.
        
        .EXAMPLE
            Set-RsEmailSettings -SmtpServer doe.com -SenderAddress john@doe.com
            Description
            -----------
            This command will configure the Report Server to use NTLM Authentication with the specified SMTP Server and Sender Address when sending emails.
        
        .EXAMPLE
            Set-RsEmailSettings -ReportServerInstance 'SQL2012' -ReportServerVersion '11' -SmtpServer doe.com -SenderAddress john@doe.com
            Description
            -----------
            This command will configure the named instance (SQL2012) from SQL Server 2012 Reporting Services to use NTLM Authentication with the specified SMTP Server and Sender Address when sending emails.
    
        .EXAMPLE
            Set-RsEmailSettings -Authentication None -SmtpServer foo.nordwind.fiction -SenderAddress Jane@nordwind.fiction
            Description
            -----------
            This command will configure the Report Server to anonymously send emails using the smtp server foo.nordwind.fiction, sending as jane@nordwind.fiction.
    
        .EXAMPLE
            Set-RsEmailSettings -Authentication Basic -SmtpServer foo.nordwind.fiction -SenderAddress Jane@nordwind.fiction -EmailCredential $cred
            Description
            -----------
            This command will configure the Report Server to send emails using the smtp server foo.nordwind.fiction, sending as jane@nordwind.fiction.
            The Report Server will use basic authentication using the specified credentials to complete this process.
        
        .NOTES
            This function unifies the three authentication specific functions that were present in the module until v 0.0.0.8. (Set-RsEmailSettingsAsNTLMAuth, Set-RsEmailSettingsAsNoAuth and Set-RsEmailSettingsAsBasicAuth).
            In order to maintain backwards compatibility, aliases with their respective name were created.
            These then affect the default value for the new 'Authentication' parameter, which was introduced in this function.
            The result should be, that all scripts using the old functions should remain functional, as the same effective outcome with the old call should persist.
    #>

    
    [cmdletbinding()]
    param
    (
        [Parameter(Mandatory = $True)]
        [Microsoft.ReportingServicesTools.SmtpAuthentication]
        $Authentication = "Ntlm",
        
        [Parameter(Mandatory = $True)]
        [string]
        $SmtpServer,
        
        [Parameter(Mandatory = $True)]
        [string]
        $SenderAddress,
        
        [Alias('EmailCredentials')]
        [System.Management.Automation.PSCredential]
        $EmailCredential,
        
        [Alias('SqlServerInstance')]
        [string]
        $ReportServerInstance,
        
        [Alias('SqlServerVersion')]
        [Microsoft.ReportingServicesTools.SqlServerVersion]
        $ReportServerVersion,
        
        [string]
        $ComputerName,
        
        [System.Management.Automation.PSCredential]
        $Credential
    )
    
    $rsWmiObject = New-RsConfigurationSettingObjectHelper -BoundParameters $PSBoundParameters
    
    #region Backwards compatibility
    if (-not $PSBoundParameters.ContainsKey("Authentication"))
    {
        $CallName = (Get-PSCallStack)[0].InvocationInfo.InvocationName
        
        if ($CallName -eq "Set-RsEmailSettingsAsNoAuth")
        {
            $Authentication = "None"
        }
        if ($CallName -eq "Set-RsEmailSettingsAsBasicAuth")
        {
            $Authentication = "Basic"
        }
        if ($CallName -eq "Set-RsEmailSettingsAsNTLMAuth")
        {
            $Authentication = "Ntlm"
        }
    }
    #endregion Backwards compatibility
    
    if (($Authentication -like "Basic") -and (-not $EmailCredential))
    {
        throw (New-Object System.Management.Automation.PSArgumentException("Basic authentication requires passing credentials using the 'EmailCredential' parameter!"))
    }
    
    #region Handle Credentials on basic authentication
    $UserName = ''
    $Password = ''
    
    if ($Authentication -like "Basic")
    {
        $UserName = $EmailCredential.UserName
        $Password = $EmailCredential.GetNetworkCredential().Password
    }
    #endregion Handle Credentials on basic authentication
    
    try
    {
        $result = $rsWmiObject.SetAuthenticatedEmailConfiguration($true, $SmtpServer, $SenderAddress, $UserName, $Password, $Authentication.Value__, $true)
    }
    catch
    {
        throw (New-Object System.Exception("Failed to update email settings: $($_.Exception.Message)", $_.Exception))
    }
    
    if ($result.HRESULT -ne 0)
    {
        throw "Failed to update email settings. Errocode: $($result.HRESULT)"
    }
}

# Backwards compatiblity
New-Alias -Name "Set-RsEmailSettingsAsNTLMAuth" -Value Set-RsEmailSettings -Scope Global
New-Alias -Name "Set-RsEmailSettingsAsNoAuth" -Value Set-RsEmailSettings -Scope Global
New-Alias -Name "Set-RsEmailSettingsAsBasicAuth" -Value Set-RsEmailSettings -Scope Global