Skip to content

Commit 026647c

Browse files
author
RandyInMarin
authored
SqlDatabaseMail: Add parameter UseDefaultCredentials (#2001)
- SqlDatabaseMail - Added the parameter `UseDefaultCredentials` to control use of the DatabaseEngine service account for SMTP server authentication.
1 parent 0ffc7ad commit 026647c

File tree

5 files changed

+143
-58
lines changed

5 files changed

+143
-58
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
- SqlServerDsc
1111
- Added build tasks to generate Wiki documentation for public commands.
12+
- SqlDatabaseMail
13+
- Added the parameter `UseDefaultCredentials` to control use of the DatabaseEngine
14+
service account for SMTP server authentication.
1215

1316
### Fixed
1417

source/DSCResources/DSC_SqlDatabaseMail/DSC_SqlDatabaseMail.psm1

+59-14
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,19 @@ function Get-TargetResource
6464
)
6565

6666
$returnValue = @{
67-
Ensure = 'Absent'
68-
ServerName = $ServerName
69-
InstanceName = $InstanceName
70-
AccountName = $null
71-
EmailAddress = $null
72-
MailServerName = $null
73-
LoggingLevel = $null
74-
ProfileName = $null
75-
DisplayName = $null
76-
ReplyToAddress = $null
77-
Description = $null
78-
TcpPort = $null
67+
Ensure = 'Absent'
68+
ServerName = $ServerName
69+
InstanceName = $InstanceName
70+
AccountName = $null
71+
EmailAddress = $null
72+
MailServerName = $null
73+
LoggingLevel = $null
74+
ProfileName = $null
75+
DisplayName = $null
76+
ReplyToAddress = $null
77+
Description = $null
78+
TcpPort = $null
79+
UseDefaultCredentials = $null
7980
}
8081

8182
Write-Verbose -Message (
@@ -145,6 +146,7 @@ function Get-TargetResource
145146
{
146147
$returnValue['MailServerName'] = $mailServer.Name
147148
$returnValue['TcpPort'] = $mailServer.Port
149+
$returnValue['UseDefaultCredentials'] = $mailServer.UseDefaultCredentials
148150
}
149151

150152
$mailProfile = $databaseMail.Profiles |
@@ -233,10 +235,19 @@ function Get-TargetResource
233235
.PARAMETER TcpPort
234236
The TCP port used for communication. Default value is port 25.
235237
238+
.PARAMETER UseDefaultCredentials
239+
Controls use of the DatabaseEngine service account for SMTP server authentication.
240+
If $true, the DatabaseEngine service account is used access the SMTP server.
241+
If $false, DatabaseEngine service account is not used.
242+
236243
.NOTES
237244
Information about the different properties can be found here
238245
https://docs.microsoft.com/en-us/sql/relational-databases/database-mail/configure-database-mail.
239246
247+
"UseDefaultCredentials" corresponds to "Windows Authentication using Database Engine service credentials"
248+
described at the above link. This dsc resource does not yet address setting state for basic or anonymous
249+
SMTP access that's used when UseDefaultCredentials is false.
250+
240251
#>
241252
function Set-TargetResource
242253
{
@@ -293,7 +304,11 @@ function Set-TargetResource
293304

294305
[Parameter()]
295306
[System.UInt16]
296-
$TcpPort = 25
307+
$TcpPort = 25,
308+
309+
[Parameter()]
310+
[System.Boolean]
311+
$UseDefaultCredentials
297312
)
298313

299314
Write-Verbose -Message (
@@ -382,6 +397,11 @@ function Set-TargetResource
382397
$mailServer.Port = $TcpPort
383398
}
384399

400+
if ($PSBoundParameters.ContainsKey('UseDefaultCredentials'))
401+
{
402+
$mailServer.UseDefaultCredentials = $UseDefaultCredentials
403+
}
404+
385405
$mailServer.Alter()
386406
}
387407
}
@@ -483,6 +503,21 @@ function Set-TargetResource
483503
$mailServer.Port = $TcpPort
484504
$mailServer.Alter()
485505
}
506+
507+
$currentUseDefaultCredentials = $mailServer.UseDefaultCredentials
508+
if ($PSBoundParameters.ContainsKey('UseDefaultCredentials') -and $currentUseDefaultCredentials -ne $UseDefaultCredentials)
509+
{
510+
Write-Verbose -Message (
511+
$script:localizedData.UpdatingPropertyOfMailServer -f @(
512+
$currentUseDefaultCredentials
513+
$UseDefaultCredentials
514+
$script:localizedData.MailServerPropertyUseDefaultCredentials
515+
)
516+
)
517+
518+
$mailServer.UseDefaultCredentials = $UseDefaultCredentials
519+
$mailServer.Alter()
520+
}
486521
}
487522

488523
$databaseMailProfile = $databaseMail.Profiles | Where-Object -FilterScript {
@@ -633,6 +668,11 @@ function Set-TargetResource
633668
634669
.PARAMETER TcpPort
635670
The TCP port used for communication. Default value is port 25.
671+
672+
.PARAMETER UseDefaultCredentials
673+
Controls use of the DatabaseEngine service account for SMTP server authentication.
674+
If $true, the DatabaseEngine service account is used access the SMTP server.
675+
If $false, DatabaseEngine service account is not used.
636676
#>
637677
function Test-TargetResource
638678
{
@@ -691,7 +731,11 @@ function Test-TargetResource
691731

692732
[Parameter()]
693733
[System.UInt16]
694-
$TcpPort = 25
734+
$TcpPort = 25,
735+
736+
[Parameter()]
737+
[System.Boolean]
738+
$UseDefaultCredentials
695739
)
696740

697741
$getTargetResourceParameters = @{
@@ -727,6 +771,7 @@ function Test-TargetResource
727771
'DisplayName'
728772
'Description'
729773
'LoggingLevel'
774+
'UseDefaultCredentials'
730775
)
731776
TurnOffTypeChecking = $true
732777
Verbose = $VerbosePreference

source/DSCResources/DSC_SqlDatabaseMail/DSC_SqlDatabaseMail.schema.mof

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ class DSC_SqlDatabaseMail : OMI_BaseResource
1313
[Write, Description("The description for the _Database Mail_ profile and account.")] String Description;
1414
[Write, Description("The logging level that the _Database Mail_ will use. If not specified the default logging level is `'Extended'`."), ValueMap{"Normal","Extended","Verbose"}, Values{"Normal","Extended","Verbose"}] String LoggingLevel;
1515
[Write, Description("The TCP port used for communication. Default value is port `25`.")] UInt16 TcpPort;
16+
[Write, Description("Specifies if the DatabaseEngine credentials are used for SMTP server authentication.")] Boolean UseDefaultCredentials;
1617
};

source/DSCResources/DSC_SqlDatabaseMail/en-US/DSC_SqlDatabaseMail.strings.psd1

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ConvertFrom-StringData @'
2222
MailServerPropertyReplyToEmailAddress = reply to e-mail address
2323
MailServerPropertyServerName = server name
2424
MailServerPropertyTcpPort = TCP port
25+
MailServerPropertyUseDefaultCredentials = Use default credentials
2526
CreatingMailProfile = Creating a public default profile '{0}'.
2627
MailProfileExist = The public default profile '{0}' already exist.
2728
ConfigureSqlAgent = Configure the SQL Agent to use Database Mail.

0 commit comments

Comments
 (0)