From 4cbbea43fc67a4e208a8dc221e4dd0aca6a2be90 Mon Sep 17 00:00:00 2001 From: Ian Lanham Date: Thu, 7 Feb 2019 16:34:14 -0500 Subject: [PATCH 1/7] Initial commit of New-DbaDbMailProfile, need to add integration tests --- functions/New-DbaDbMailProfile.ps1 | 101 +++++++++++++++++++++++++++ tests/New-DbaDbMailProfile.Tests.ps1 | 74 ++++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 functions/New-DbaDbMailProfile.ps1 create mode 100644 tests/New-DbaDbMailProfile.Tests.ps1 diff --git a/functions/New-DbaDbMailProfile.ps1 b/functions/New-DbaDbMailProfile.ps1 new file mode 100644 index 0000000000..4512754c7c --- /dev/null +++ b/functions/New-DbaDbMailProfile.ps1 @@ -0,0 +1,101 @@ +#ValidationTags#Messaging,FlowControl,Pipeline,CodeStyle# +function New-DbaDbMailProfile { + <# + .SYNOPSIS + Creates a new database mail profile + + .DESCRIPTION + Creates a new database mail profile, and optionally associates it to a database mail account + + .PARAMETER SqlInstance + The target SQL Server instance or instances. You must have sysadmin access and server version must be SQL Server version 2008 or higher. + + .PARAMETER SqlCredential + Login to the target instance using alternative credentials. Windows and SQL Authentication supported. Accepts credential objects (Get-Credential) + + .PARAMETER Name + The Name of the profile to be created. + + .PARAMETER Description + Sets the description of the purpose of the mail profile. + + .PARAMETER MailAccountName + Associates a db mail account to link to this db mail profile. + + .PARAMETER MailAccountPriority + Sets the priority of the linked db mail account when linking to this db mail profile. + + .PARAMETER WhatIf + If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run. + + .PARAMETER Confirm + If this switch is enabled, you will be prompted for confirmation before executing any operations that change state. + + .PARAMETER EnableException + By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. + This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. + Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch. + + .NOTES + Tags: DbMail + Author: Ian Lanham (@ilanham) + + Website: https://dbatools.io + Copyright: (c) 2018 by dbatools, licensed under MIT + License: MIT https://opensource.org/licenses/MIT + + .LINK + https://dbatools.io/New-DbaDbMailProfile + + .EXAMPLE + PS C:\> $profile = New-DbaDbMailProfile -SqlInstance sql2017 -Name 'The DBA Team' + + Creates a new db mail profile + + #> + [CmdletBinding(SupportsShouldProcess, ConfirmImpact="Low")] + param ( + [parameter(Mandatory, ValueFromPipeline)] + [Alias("ServerInstance", "SqlServer")] + [DbaInstanceParameter[]]$SqlInstance, + [PSCredential]$SqlCredential, + [parameter(Mandatory)] + [string]$Name, + [string]$Description, + [string]$MailAccountName, + [int]$MailAccountPriority, + [switch]$EnableException + ) + process { + foreach ($instance in $SqlInstance) { + try { + $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 11 + } catch { + Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue + } + + if ($Pscmdlet.ShouldProcess($instance, "Creating new db mail profile called $Name")) { + try { + $profile = New-Object Microsoft.SqlServer.Management.SMO.Mail.MailProfile $server.Mail, $Name + if (Test-Bound -ParameterName 'Description') { + $profile.Description = $Description + } + $profile.Create() + if (Test-Bound -ParameterName 'MailAccountName') { + if (!$MailAccountPriority) { + $MailAccountPriority = 1 + } + $profile.AddAccount($MailAccountName,$MailAccountPriority) # sequenceNumber correlates to "Priority" when associating a Mail Account to a Profile + } + Add-Member -Force -InputObject $profile -MemberType NoteProperty -Name ComputerName -value $server.ComputerName + Add-Member -Force -InputObject $profile -MemberType NoteProperty -Name InstanceName -value $server.ServiceName + Add-Member -Force -InputObject $profile -MemberType NoteProperty -Name SqlInstance -value $server.DomainInstanceName + + $profile | Select-DefaultView -Property ComputerName, InstanceName, SqlInstance, Id, Name, Description, IsBusyProfile + } catch { + Stop-Function -Message "Failure" -ErrorRecord $_ -Continue + } + } + } + } +} \ No newline at end of file diff --git a/tests/New-DbaDbMailProfile.Tests.ps1 b/tests/New-DbaDbMailProfile.Tests.ps1 new file mode 100644 index 0000000000..0867ee49d9 --- /dev/null +++ b/tests/New-DbaDbMailProfile.Tests.ps1 @@ -0,0 +1,74 @@ +$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "") +Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan +. "$PSScriptRoot\constants.ps1" + +Describe "$CommandName Unit Tests" -Tag 'UnitTests' { + Context "Validate parameters" { + [object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')} + [object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Name', 'Description', 'AccountName', 'EnableException' + $knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters + It "Should only contain our specific parameters" { + (@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0 + } + } +} + +Describe "$commandname Integration Tests" -Tags "IntegrationTests" { + BeforeAll { + $accountname = "dbatoolsci_test_$(get-random)" + $server = Connect-DbaInstance -SqlInstance $script:instance2 + $description = 'Mail account for email alerts' + $name = 'dbatoolssci@dbatools.io' + } + AfterAll { + $server = Connect-DbaInstance -SqlInstance $script:instance2 + $mailAccountSettings = "EXEC msdb.dbo.sysmail_delete_profile_sp + @account_name = '$accountname';" + $server.query($mailAccountSettings) + } + + Context "Gets DbMail Profile" { + + $splat = @{ + SqlInstance = $script:instance2 + Name = $accountname + Description = $description + DisplayName = $display_name + } + $results = New-DbaDbMailProfile @splat + + It "Gets results" { + $results | Should Not Be $null + } + It "Should have Name of $accounName" { + $results.name | Should Be $accountname + } + It "Should have Desctiption of 'Mail account for email alerts' " { + $results.description | Should Be 'Mail account for email alerts' + } + It -Skip "Should have MailServer of '[smtp.dbatools.io]' " { + $results.MailServers | Should Be '[smtp.dbatools.io]' + } + } + Context "Gets DbMail when using -Account" { + $results = Get-DbaDbMailProfile -SqlInstance $script:instance2 -Account $accountname + It "Gets results" { + $results | Should Not Be $null + } + It "Should have Name of $accounName" { + $results.name | Should Be $accountname + } + It "Should have Desctiption of 'Mail account for email alerts' " { + $results.description | Should Be 'Mail account for email alerts' + } + It "Should have MailServer of '[smtp.dbatools.io]' " { + $results.MailServers | Should Be '[smtp.dbatools.io]' + } + } + Context "Gets no DbMail when using -ExcludeAccount" { + $results = Get-DbaDbMailProfile -SqlInstance $script:instance2 -ExcludeAccount $accountname + It "Gets no results" { + $results | Should Be $null + } + } +} \ No newline at end of file From 64dce29299c620ac75b37242252b6d31a75540e7 Mon Sep 17 00:00:00 2001 From: Ian Lanham Date: Mon, 18 Feb 2019 21:43:33 -0500 Subject: [PATCH 2/7] Clarified comments on sequenceNumber --- functions/New-DbaDbMailProfile.ps1 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/functions/New-DbaDbMailProfile.ps1 b/functions/New-DbaDbMailProfile.ps1 index 4512754c7c..f5a8084d25 100644 --- a/functions/New-DbaDbMailProfile.ps1 +++ b/functions/New-DbaDbMailProfile.ps1 @@ -15,16 +15,16 @@ function New-DbaDbMailProfile { .PARAMETER Name The Name of the profile to be created. - + .PARAMETER Description Sets the description of the purpose of the mail profile. - + .PARAMETER MailAccountName Associates a db mail account to link to this db mail profile. - + .PARAMETER MailAccountPriority Sets the priority of the linked db mail account when linking to this db mail profile. - + .PARAMETER WhatIf If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run. @@ -53,7 +53,7 @@ function New-DbaDbMailProfile { Creates a new db mail profile #> - [CmdletBinding(SupportsShouldProcess, ConfirmImpact="Low")] + [CmdletBinding(SupportsShouldProcess, ConfirmImpact = "Low")] param ( [parameter(Mandatory, ValueFromPipeline)] [Alias("ServerInstance", "SqlServer")] @@ -85,12 +85,12 @@ function New-DbaDbMailProfile { if (!$MailAccountPriority) { $MailAccountPriority = 1 } - $profile.AddAccount($MailAccountName,$MailAccountPriority) # sequenceNumber correlates to "Priority" when associating a Mail Account to a Profile + $profile.AddAccount($MailAccountName, $MailAccountPriority) # sequenceNumber correlates to "Priority" when associating a DbMail Account to a DbMail Profile } Add-Member -Force -InputObject $profile -MemberType NoteProperty -Name ComputerName -value $server.ComputerName Add-Member -Force -InputObject $profile -MemberType NoteProperty -Name InstanceName -value $server.ServiceName Add-Member -Force -InputObject $profile -MemberType NoteProperty -Name SqlInstance -value $server.DomainInstanceName - + $profile | Select-DefaultView -Property ComputerName, InstanceName, SqlInstance, Id, Name, Description, IsBusyProfile } catch { Stop-Function -Message "Failure" -ErrorRecord $_ -Continue From f9927c9747e9a46f51106d165cebf09d8a7d6077 Mon Sep 17 00:00:00 2001 From: Ian Lanham Date: Mon, 18 Feb 2019 21:58:11 -0500 Subject: [PATCH 3/7] Fix naming conventions --- functions/New-DbaDbMailProfile.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/New-DbaDbMailProfile.ps1 b/functions/New-DbaDbMailProfile.ps1 index f5a8084d25..d4052513d0 100644 --- a/functions/New-DbaDbMailProfile.ps1 +++ b/functions/New-DbaDbMailProfile.ps1 @@ -85,7 +85,7 @@ function New-DbaDbMailProfile { if (!$MailAccountPriority) { $MailAccountPriority = 1 } - $profile.AddAccount($MailAccountName, $MailAccountPriority) # sequenceNumber correlates to "Priority" when associating a DbMail Account to a DbMail Profile + $profile.AddAccount($MailAccountName, $MailAccountPriority) # sequenceNumber correlates to "Priority" when associating a db mail Account to a db mail Profile } Add-Member -Force -InputObject $profile -MemberType NoteProperty -Name ComputerName -value $server.ComputerName Add-Member -Force -InputObject $profile -MemberType NoteProperty -Name InstanceName -value $server.ServiceName From 702daba25cb5998382ceaebe03984c9d7496fefb Mon Sep 17 00:00:00 2001 From: Ian Lanham Date: Mon, 18 Feb 2019 21:58:52 -0500 Subject: [PATCH 4/7] Removed unused integration tests. Added test for $priority. --- tests/New-DbaDbMailProfile.Tests.ps1 | 45 ++++++++-------------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/tests/New-DbaDbMailProfile.Tests.ps1 b/tests/New-DbaDbMailProfile.Tests.ps1 index 0867ee49d9..f67f698537 100644 --- a/tests/New-DbaDbMailProfile.Tests.ps1 +++ b/tests/New-DbaDbMailProfile.Tests.ps1 @@ -5,7 +5,7 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan Describe "$CommandName Unit Tests" -Tag 'UnitTests' { Context "Validate parameters" { [object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')} - [object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Name', 'Description', 'AccountName', 'EnableException' + [object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Name', 'Description', 'AccountName', 'Priority', 'EnableException' $knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters It "Should only contain our specific parameters" { (@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0 @@ -19,6 +19,7 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { $server = Connect-DbaInstance -SqlInstance $script:instance2 $description = 'Mail account for email alerts' $name = 'dbatoolssci@dbatools.io' + $priority = 1 } AfterAll { $server = Connect-DbaInstance -SqlInstance $script:instance2 @@ -27,48 +28,28 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { $server.query($mailAccountSettings) } - Context "Gets DbMail Profile" { + Context "Sets DbMail Profile" { $splat = @{ - SqlInstance = $script:instance2 - Name = $accountname - Description = $description - DisplayName = $display_name + SqlInstance = $script:instance2 + Name = $accountname + Description = $description + DisplayName = $display_name } $results = New-DbaDbMailProfile @splat It "Gets results" { $results | Should Not Be $null } - It "Should have Name of $accounName" { + It "Should have Name of $accountname" { $results.name | Should Be $accountname } - It "Should have Desctiption of 'Mail account for email alerts' " { - $results.description | Should Be 'Mail account for email alerts' + It "Should have Description of $description " { + $results.description | Should Be $description } - It -Skip "Should have MailServer of '[smtp.dbatools.io]' " { - $results.MailServers | Should Be '[smtp.dbatools.io]' - } - } - Context "Gets DbMail when using -Account" { - $results = Get-DbaDbMailProfile -SqlInstance $script:instance2 -Account $accountname - It "Gets results" { - $results | Should Not Be $null - } - It "Should have Name of $accounName" { - $results.name | Should Be $accountname - } - It "Should have Desctiption of 'Mail account for email alerts' " { - $results.description | Should Be 'Mail account for email alerts' - } - It "Should have MailServer of '[smtp.dbatools.io]' " { - $results.MailServers | Should Be '[smtp.dbatools.io]' - } - } - Context "Gets no DbMail when using -ExcludeAccount" { - $results = Get-DbaDbMailProfile -SqlInstance $script:instance2 -ExcludeAccount $accountname - It "Gets no results" { - $results | Should Be $null + + It "Shoud have a Priority of $priority" { + $results.description | Should Be $priority } } } \ No newline at end of file From 9d08d0434e5587930558da4726df9221b747d676 Mon Sep 17 00:00:00 2001 From: Ian Lanham Date: Mon, 18 Feb 2019 22:29:32 -0500 Subject: [PATCH 5/7] Fix wrong variable names in tests --- tests/New-DbaDbMailProfile.Tests.ps1 | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/tests/New-DbaDbMailProfile.Tests.ps1 b/tests/New-DbaDbMailProfile.Tests.ps1 index f67f698537..dfb009c0db 100644 --- a/tests/New-DbaDbMailProfile.Tests.ps1 +++ b/tests/New-DbaDbMailProfile.Tests.ps1 @@ -5,7 +5,7 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan Describe "$CommandName Unit Tests" -Tag 'UnitTests' { Context "Validate parameters" { [object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')} - [object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Name', 'Description', 'AccountName', 'Priority', 'EnableException' + [object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Name', 'Description', 'MailAccountName', 'MailAccountPriority', 'EnableException' $knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters It "Should only contain our specific parameters" { (@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0 @@ -18,8 +18,8 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { $accountname = "dbatoolsci_test_$(get-random)" $server = Connect-DbaInstance -SqlInstance $script:instance2 $description = 'Mail account for email alerts' - $name = 'dbatoolssci@dbatools.io' - $priority = 1 + $mailaccountname = 'dbatoolssci@dbatools.io' + $mailaccountpriority = 1 } AfterAll { $server = Connect-DbaInstance -SqlInstance $script:instance2 @@ -31,10 +31,11 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { Context "Sets DbMail Profile" { $splat = @{ - SqlInstance = $script:instance2 - Name = $accountname - Description = $description - DisplayName = $display_name + SqlInstance = $script:instance2 + Name = $accountname + Description = $description + MailAccountName = $mailaccountname + MailAccountPriority = $mailaccountpriority } $results = New-DbaDbMailProfile @splat @@ -47,9 +48,11 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { It "Should have Description of $description " { $results.description | Should Be $description } - - It "Shoud have a Priority of $priority" { - $results.description | Should Be $priority + It "Should have MailAccountName of $mailaccountname " { + $results.mailaccountname | Should Be $mailaccountname + } + It "Shoud have a Priority of $mailaccountpriority" { + $results.mailaccountpriority | Should Be $mailaccountpriority } } } \ No newline at end of file From ecc54ea93502ea9b4c896124ef46ea106aec1549 Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Tue, 19 Feb 2019 23:21:10 +0100 Subject: [PATCH 6/7] change bom --- functions/New-DbaDbMailProfile.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/New-DbaDbMailProfile.ps1 b/functions/New-DbaDbMailProfile.ps1 index d4052513d0..bd0c70db86 100644 --- a/functions/New-DbaDbMailProfile.ps1 +++ b/functions/New-DbaDbMailProfile.ps1 @@ -1,4 +1,4 @@ -#ValidationTags#Messaging,FlowControl,Pipeline,CodeStyle# +#ValidationTags#Messaging,FlowControl,Pipeline,CodeStyle# function New-DbaDbMailProfile { <# .SYNOPSIS From abbd83aa2fbf38ae919c0b2489a8710006120c8a Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Wed, 20 Feb 2019 00:23:28 +0100 Subject: [PATCH 7/7] added account, removed tests for properties that do not exist --- tests/New-DbaDbMailProfile.Tests.ps1 | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tests/New-DbaDbMailProfile.Tests.ps1 b/tests/New-DbaDbMailProfile.Tests.ps1 index dfb009c0db..318ada3e8f 100644 --- a/tests/New-DbaDbMailProfile.Tests.ps1 +++ b/tests/New-DbaDbMailProfile.Tests.ps1 @@ -15,24 +15,33 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' { Describe "$commandname Integration Tests" -Tags "IntegrationTests" { BeforeAll { - $accountname = "dbatoolsci_test_$(get-random)" + $profilename = "dbatoolsci_test_$(get-random)" $server = Connect-DbaInstance -SqlInstance $script:instance2 $description = 'Mail account for email alerts' $mailaccountname = 'dbatoolssci@dbatools.io' $mailaccountpriority = 1 + + $sql = "EXECUTE msdb.dbo.sysmail_add_account_sp + @account_name = '$mailaccountname', + @description = 'Mail account for administrative e-mail.', + @email_address = 'dba@ad.local', + @display_name = 'Automated Mailer', + @mailserver_name = 'smtp.ad.local'" + $server.Query($sql) } AfterAll { $server = Connect-DbaInstance -SqlInstance $script:instance2 - $mailAccountSettings = "EXEC msdb.dbo.sysmail_delete_profile_sp - @account_name = '$accountname';" + $mailAccountSettings = "EXEC msdb.dbo.sysmail_delete_profile_sp @profile_name = '$profilename';" $server.query($mailAccountSettings) + $regularaccountsettings = "EXEC msdb.dbo.sysmail_delete_account_sp @account_name = '$mailaccountname';" + $server.query($regularaccountsettings) } Context "Sets DbMail Profile" { $splat = @{ SqlInstance = $script:instance2 - Name = $accountname + Name = $profilename Description = $description MailAccountName = $mailaccountname MailAccountPriority = $mailaccountpriority @@ -42,17 +51,11 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { It "Gets results" { $results | Should Not Be $null } - It "Should have Name of $accountname" { - $results.name | Should Be $accountname + It "Should have Name of $profilename" { + $results.name | Should Be $profilename } It "Should have Description of $description " { $results.description | Should Be $description } - It "Should have MailAccountName of $mailaccountname " { - $results.mailaccountname | Should Be $mailaccountname - } - It "Shoud have a Priority of $mailaccountpriority" { - $results.mailaccountpriority | Should Be $mailaccountpriority - } } } \ No newline at end of file