Skip to content

Commit 480e72a

Browse files
New-DbaDbMailProfile - Fix to allow multiple accounts per profile (#10214)
1 parent 95e3aa2 commit 480e72a

2 files changed

Lines changed: 85 additions & 17 deletions

File tree

public/New-DbaDbMailProfile.ps1

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,28 +101,54 @@ function New-DbaDbMailProfile {
101101
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
102102
}
103103

104-
if ($Pscmdlet.ShouldProcess($instance, "Creating new db mail profile called $Profile")) {
105-
try {
106-
$profileObj = New-Object Microsoft.SqlServer.Management.SMO.Mail.MailProfile $server.Mail, $Profile
107-
if (Test-Bound -ParameterName 'Description') {
108-
$profileObj.Description = $Description
104+
# Check if profile already exists
105+
$existingProfile = $server.Mail.Profiles | Where-Object Name -eq $Profile
106+
107+
if ($existingProfile) {
108+
# Profile exists, just add the account if specified
109+
if (Test-Bound -ParameterName 'MailAccountName') {
110+
if ($Pscmdlet.ShouldProcess($instance, "Adding account $MailAccountName to existing db mail profile $Profile")) {
111+
try {
112+
if (!$MailAccountPriority) {
113+
$MailAccountPriority = 1
114+
}
115+
$existingProfile.AddAccount($MailAccountName, $MailAccountPriority)
116+
$profileObj = $existingProfile
117+
} catch {
118+
Stop-Function -Message "Failure" -ErrorRecord $_ -Continue
119+
}
109120
}
110-
$profileObj.Create()
111-
if (Test-Bound -ParameterName 'MailAccountName') {
112-
if (!$MailAccountPriority) {
113-
$MailAccountPriority = 1
121+
} else {
122+
Stop-Function -Message "Profile $Profile already exists on $instance. Use MailAccountName parameter to add an account to the existing profile." -Continue
123+
}
124+
} else {
125+
# Profile doesn't exist, create it
126+
if ($Pscmdlet.ShouldProcess($instance, "Creating new db mail profile called $Profile")) {
127+
try {
128+
$profileObj = New-Object Microsoft.SqlServer.Management.SMO.Mail.MailProfile $server.Mail, $Profile
129+
if (Test-Bound -ParameterName 'Description') {
130+
$profileObj.Description = $Description
114131
}
115-
$profileObj.AddAccount($MailAccountName, $MailAccountPriority) # sequenceNumber correlates to "Priority" when associating a db mail Account to a db mail Profile
132+
$profileObj.Create()
133+
if (Test-Bound -ParameterName 'MailAccountName') {
134+
if (!$MailAccountPriority) {
135+
$MailAccountPriority = 1
136+
}
137+
$profileObj.AddAccount($MailAccountName, $MailAccountPriority) # sequenceNumber correlates to "Priority" when associating a db mail Account to a db mail Profile
138+
}
139+
} catch {
140+
Stop-Function -Message "Failure" -ErrorRecord $_ -Continue
116141
}
117-
Add-Member -Force -InputObject $profileObj -MemberType NoteProperty -Name ComputerName -value $server.ComputerName
118-
Add-Member -Force -InputObject $profileObj -MemberType NoteProperty -Name InstanceName -value $server.ServiceName
119-
Add-Member -Force -InputObject $profileObj -MemberType NoteProperty -Name SqlInstance -value $server.DomainInstanceName
120-
121-
$profileObj | Select-DefaultView -Property ComputerName, InstanceName, SqlInstance, Id, Name, Description, IsBusyProfile
122-
} catch {
123-
Stop-Function -Message "Failure" -ErrorRecord $_ -Continue
124142
}
125143
}
144+
145+
if ($profileObj) {
146+
Add-Member -Force -InputObject $profileObj -MemberType NoteProperty -Name ComputerName -value $server.ComputerName
147+
Add-Member -Force -InputObject $profileObj -MemberType NoteProperty -Name InstanceName -value $server.ServiceName
148+
Add-Member -Force -InputObject $profileObj -MemberType NoteProperty -Name SqlInstance -value $server.DomainInstanceName
149+
150+
$profileObj | Select-DefaultView -Property ComputerName, InstanceName, SqlInstance, Id, Name, Description, IsBusyProfile
151+
}
126152
}
127153
}
128154
}

tests/New-DbaDbMailProfile.Tests.ps1

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Describe $CommandName -Tag IntegrationTests {
3333
$server = Connect-DbaInstance -SqlInstance $TestConfig.InstanceSingle
3434
$description = "Mail account for email alerts"
3535
$mailaccountname = "dbatoolssci@dbatools.io"
36+
$mailaccountname2 = "dbatoolssci2@dbatools.io"
3637
$mailaccountpriority = 1
3738

3839
$sql = "EXECUTE msdb.dbo.sysmail_add_account_sp
@@ -43,6 +44,14 @@ Describe $CommandName -Tag IntegrationTests {
4344
@mailserver_name = 'smtp.ad.local'"
4445
$server.Query($sql)
4546

47+
$sql2 = "EXECUTE msdb.dbo.sysmail_add_account_sp
48+
@account_name = '$mailaccountname2',
49+
@description = 'Second mail account for administrative e-mail.',
50+
@email_address = 'dba2@ad.local',
51+
@display_name = 'Automated Mailer 2',
52+
@mailserver_name = 'smtp.ad.local'"
53+
$server.Query($sql2)
54+
4655
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
4756
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
4857
}
@@ -56,6 +65,8 @@ Describe $CommandName -Tag IntegrationTests {
5665
$server.query($mailAccountSettings)
5766
$regularaccountsettings = "EXEC msdb.dbo.sysmail_delete_account_sp @account_name = '$mailaccountname';"
5867
$server.query($regularaccountsettings)
68+
$regularaccountsettings2 = "EXEC msdb.dbo.sysmail_delete_account_sp @account_name = '$mailaccountname2';"
69+
$server.query($regularaccountsettings2)
5970

6071
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
6172
}
@@ -82,4 +93,35 @@ Describe $CommandName -Tag IntegrationTests {
8293
$results.description | Should -Be $description
8394
}
8495
}
96+
97+
Context "Adds multiple accounts to the same profile" {
98+
It "Should allow adding a second account to existing profile without error" {
99+
$splatProfile2 = @{
100+
SqlInstance = $TestConfig.InstanceSingle
101+
Profile = $profilename
102+
MailAccountName = $mailaccountname2
103+
MailAccountPriority = 2
104+
}
105+
$results = New-DbaDbMailProfile @splatProfile2
106+
$results | Should -Not -BeNullOrEmpty
107+
$WarnVar | Should -BeNullOrEmpty
108+
}
109+
110+
It "Should have both accounts associated with the profile" {
111+
$profile = Get-DbaDbMailProfile -SqlInstance $TestConfig.InstanceSingle -Profile $profilename
112+
$accounts = $profile.EnumAccounts()
113+
$accounts | Should -HaveCount 2
114+
}
115+
116+
It "Should fail with clear message when trying to create duplicate profile without MailAccountName" {
117+
$splatDuplicate = @{
118+
SqlInstance = $TestConfig.InstanceSingle
119+
Profile = $profilename
120+
Description = "Duplicate attempt"
121+
}
122+
$results = New-DbaDbMailProfile @splatDuplicate
123+
$results | Should -BeNullOrEmpty
124+
$WarnVar | Should -Match "Profile .* already exists"
125+
}
126+
}
85127
}

0 commit comments

Comments
 (0)