Skip to content

Commit

Permalink
Merge pull request #5099 from ilanham/development
Browse files Browse the repository at this point in the history
Adding New-DbaDbMailProfile
  • Loading branch information
potatoqualitee authored Feb 19, 2019
2 parents edf70a8 + abbd83a commit 8261ba8
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
101 changes: 101 additions & 0 deletions functions/New-DbaDbMailProfile.ps1
Original file line number Diff line number Diff line change
@@ -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 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
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
}
}
}
}
}
61 changes: 61 additions & 0 deletions tests/New-DbaDbMailProfile.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
$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', '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
}
}
}

Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
BeforeAll {
$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 @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 = $profilename
Description = $description
MailAccountName = $mailaccountname
MailAccountPriority = $mailaccountpriority
}
$results = New-DbaDbMailProfile @splat

It "Gets results" {
$results | Should Not Be $null
}
It "Should have Name of $profilename" {
$results.name | Should Be $profilename
}
It "Should have Description of $description " {
$results.description | Should Be $description
}
}
}

0 comments on commit 8261ba8

Please sign in to comment.