Skip to content

Commit

Permalink
New-DbaDbMailAccount, New-DbaDbMailProfile - renaming parameter name …
Browse files Browse the repository at this point in the history
…to Account, Profile respectively (dataplat#7835)
  • Loading branch information
MikeyBronowski authored Oct 6, 2021
1 parent 0e135dd commit 88e70dd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 41 deletions.
45 changes: 23 additions & 22 deletions functions/New-DbaDbMailAccount.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function New-DbaDbMailAccount {
For MFA support, please use Connect-DbaInstance.
.PARAMETER Name
The Name of the account to be created.
.PARAMETER Account
The name of the account to be created.
.PARAMETER DisplayName
Sets the name of the mail account that is displayed in messages.
Expand Down Expand Up @@ -49,7 +49,7 @@ function New-DbaDbMailAccount {
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
.NOTES
Tags: DbMail
Tags: DatabaseMail, DbMail, Mail
Author: Chrissy LeMaire (@cl), netnerds.net
Website: https://dbatools.io
Expand All @@ -60,9 +60,9 @@ function New-DbaDbMailAccount {
https://dbatools.io/New-DbaDbMailAccount
.EXAMPLE
PS C:\> $account = New-DbaDbMailAccount -SqlInstance sql2017 -Name 'The DBA Team' -EmailAddress admin@ad.local -MailServer smtp.ad.local
PS C:\> $account = New-DbaDbMailAccount -SqlInstance sql2017 -Account 'The DBA Team' -EmailAddress admin@ad.local -MailServer smtp.ad.local
Creates a new db mail account with the email address admin@ad.local on sql2017 named "The DBA Team" using the smtp.ad.local mail server
Creates a new database mail account with the email address admin@ad.local on sql2017 named "The DBA Team" using the smtp.ad.local mail server.
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = "Low")]
Expand All @@ -71,8 +71,9 @@ function New-DbaDbMailAccount {
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[parameter(Mandatory)]
[string]$Name,
[string]$DisplayName = $Name,
[Alias("Name")]
[string]$Account,
[string]$DisplayName = $Account,
[string]$Description,
[parameter(Mandatory)]
[string]$EmailAddress,
Expand All @@ -95,26 +96,26 @@ function New-DbaDbMailAccount {
}
}

if ($Pscmdlet.ShouldProcess($instance, "Creating new db mail account called $Name")) {
if ($Pscmdlet.ShouldProcess($instance, "Creating new db mail account called $Account")) {
try {
$account = New-Object Microsoft.SqlServer.Management.SMO.Mail.MailAccount $server.Mail, $Name
$account.DisplayName = $DisplayName
$account.Description = $Description
$account.EmailAddress = $EmailAddress
$account.ReplyToAddress = $ReplyToAddress
$account.Create()
$accountObj = New-Object Microsoft.SqlServer.Management.SMO.Mail.MailAccount $server.Mail, $Account
$accountObj.DisplayName = $DisplayName
$accountObj.Description = $Description
$accountObj.EmailAddress = $EmailAddress
$accountObj.ReplyToAddress = $ReplyToAddress
$accountObj.Create()
} catch {
Stop-Function -Message "Failure creating db mail account" -Target $Name -ErrorRecord $_ -Continue
Stop-Function -Message "Failure creating db mail account" -Target $Account -ErrorRecord $_ -Continue
}

try {
$account.MailServers.Item($($server.DomainInstanceName)).Rename($MailServer)
$account.Alter()
$account.Refresh()
Add-Member -Force -InputObject $account -MemberType NoteProperty -Name ComputerName -value $server.ComputerName
Add-Member -Force -InputObject $account -MemberType NoteProperty -Name InstanceName -value $server.ServiceName
Add-Member -Force -InputObject $account -MemberType NoteProperty -Name SqlInstance -value $server.DomainInstanceName
$account | Select-DefaultView -Property ComputerName, InstanceName, SqlInstance, Id, Name, DisplayName, Description, EmailAddress, ReplyToAddress, IsBusyAccount, MailServers
$accountObj.MailServers.Item($($server.DomainInstanceName)).Rename($MailServer)
$accountObj.Alter()
$accountObj.Refresh()
Add-Member -Force -InputObject $accountObj -MemberType NoteProperty -Name ComputerName -value $server.ComputerName
Add-Member -Force -InputObject $accountObj -MemberType NoteProperty -Name InstanceName -value $server.ServiceName
Add-Member -Force -InputObject $accountObj -MemberType NoteProperty -Name SqlInstance -value $server.DomainInstanceName
$accountObj | Select-DefaultView -Property ComputerName, InstanceName, SqlInstance, Id, Name, DisplayName, Description, EmailAddress, ReplyToAddress, IsBusyAccount, MailServers
} catch {
Stop-Function -Message "Failure returning output" -ErrorRecord $_ -Continue
}
Expand Down
31 changes: 16 additions & 15 deletions functions/New-DbaDbMailProfile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function New-DbaDbMailProfile {
For MFA support, please use Connect-DbaInstance.
.PARAMETER Name
The Name of the profile to be created.
.PARAMETER Profile
The name of the profile to be created.
.PARAMETER Description
Sets the description of the purpose of the mail profile.
Expand All @@ -40,7 +40,7 @@ function New-DbaDbMailProfile {
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
.NOTES
Tags: DbMail
Tags: DatabaseMail, DbMail, Mail
Author: Ian Lanham (@ilanham)
Website: https://dbatools.io
Expand All @@ -51,9 +51,9 @@ function New-DbaDbMailProfile {
https://dbatools.io/New-DbaDbMailProfile
.EXAMPLE
PS C:\> $profile = New-DbaDbMailProfile -SqlInstance sql2017 -Name 'The DBA Team'
PS C:\> $profile = New-DbaDbMailProfile -SqlInstance sql2017 -Profile 'The DBA Team'
Creates a new db mail profile
Creates a new database mail profile.
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = "Low")]
Expand All @@ -62,7 +62,8 @@ function New-DbaDbMailProfile {
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[parameter(Mandatory)]
[string]$Name,
[Alias("Name")]
[string]$Profile,
[string]$Description,
[string]$MailAccountName,
[int]$MailAccountPriority,
Expand All @@ -76,24 +77,24 @@ function New-DbaDbMailProfile {
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
}

if ($Pscmdlet.ShouldProcess($instance, "Creating new db mail profile called $Name")) {
if ($Pscmdlet.ShouldProcess($instance, "Creating new db mail profile called $Profile")) {
try {
$profile = New-Object Microsoft.SqlServer.Management.SMO.Mail.MailProfile $server.Mail, $Name
$profileObj = New-Object Microsoft.SqlServer.Management.SMO.Mail.MailProfile $server.Mail, $Profile
if (Test-Bound -ParameterName 'Description') {
$profile.Description = $Description
$profileObj.Description = $Description
}
$profile.Create()
$profileObj.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
$profileObj.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
Add-Member -Force -InputObject $profileObj -MemberType NoteProperty -Name ComputerName -value $server.ComputerName
Add-Member -Force -InputObject $profileObj -MemberType NoteProperty -Name InstanceName -value $server.ServiceName
Add-Member -Force -InputObject $profileObj -MemberType NoteProperty -Name SqlInstance -value $server.DomainInstanceName

$profile | Select-DefaultView -Property ComputerName, InstanceName, SqlInstance, Id, Name, Description, IsBusyProfile
$profileObj | Select-DefaultView -Property ComputerName, InstanceName, SqlInstance, Id, Name, Description, IsBusyProfile
} catch {
Stop-Function -Message "Failure" -ErrorRecord $_ -Continue
}
Expand Down
4 changes: 2 additions & 2 deletions tests/New-DbaDbMailAccount.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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', 'DisplayName', 'Description', 'EmailAddress', 'ReplyToAddress', 'MailServer', 'Force', 'EnableException'
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Account', 'DisplayName', 'Description', 'EmailAddress', 'ReplyToAddress', 'MailServer', 'Force', '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
Expand Down Expand Up @@ -37,7 +37,7 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {

$splat = @{
SqlInstance = $script:instance2
Name = $accountName
Account = $accountName
Description = $description
EmailAddress = $email_address
DisplayName = $display_name
Expand Down
4 changes: 2 additions & 2 deletions tests/New-DbaDbMailProfile.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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', 'MailAccountName', 'MailAccountPriority', 'EnableException'
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Profile', '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
Expand Down Expand Up @@ -41,7 +41,7 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {

$splat = @{
SqlInstance = $script:instance2
Name = $profilename
Profile = $profilename
Description = $description
MailAccountName = $mailaccountname
MailAccountPriority = $mailaccountpriority
Expand Down

0 comments on commit 88e70dd

Please sign in to comment.