Skip to content

SqlServerDatabaseMail

johlju edited this page Jun 9, 2020 · 1 revision

SqlServerDatabaseMail

Parameters

Parameter Attribute DataType Description Allowed Values
AccountName Key String The name of the Database Mail account.
InstanceName Key String The name of the SQL instance to be configured.
EmailAddress Required String The e-mail address from which mail will originate.
MailServerName Required String The fully qualified domain name of the mail server name to which e-mail are sent.
ProfileName Required String The name of the Database Mail profile.
Ensure Write String Specifies the desired state of the Database Mail. When set to 'Present', the Database Mail will be created. When set to 'Absent', the Database Mail will be removed. Default value is 'Present'. Present, Absent
ServerName Write String The hostname of the SQL Server to be configured. Default value is $env:COMPUTERNAME.
DisplayName Write String The display name of the originating email address. Default value is the same value assigned to the EmailAddress parameter.
ReplyToAddress Write String The e-mail address to which the receiver of e-mails will reply to. Default value is the same e-mail address assigned to parameter EmailAddress.
Description Write String The description for the Database Mail profile and account.
LoggingLevel Write String The logging level that the Database Mail will use. If not specified the default logging level is 'Extended'. Normal, Extended, Verbose
TcpPort Write UInt16 The TCP port used for communication. Default value is port 25.

Description

The SqlDatabaseMail DSC resource manages SQL Server Database Mail.

Note: Database Mail XPs can be enabled using the resource SqlConfiguration.

Requirements

  • Target machine must be running Windows Server 2012 or later.
  • Target machine must be running SQL Server Database Engine 2012 or later.
  • Target machine must be running SQL Server Agent.
  • Target machine must have enabled Database Mail XPs.

Known issues

All issues are not listed here, see here for all open issues.

Examples

Example 1

This example will enable Database Mail on a SQL Server instance and create a mail account with a default public profile.

Configuration Example
{
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.PSCredential]
        $SqlInstallCredential
    )

    Import-DscResource -ModuleName 'SqlServerDsc'

    node localhost
    {
        SqlServerConfiguration 'EnableDatabaseMailXPs'
        {

            ServerName     = $Node.NodeName
            InstanceName   = 'DSCSQLTEST'
            OptionName     = 'Database Mail XPs'
            OptionValue    = 1
            RestartService = $false
        }

        SqlServerDatabaseMail 'EnableDatabaseMail'
        {
            Ensure               = 'Present'
            ServerName           = $Node.NodeName
            InstanceName         = 'DSCSQLTEST'
            AccountName          = 'MyMail'
            ProfileName          = 'MyMailProfile'
            EmailAddress         = 'NoReply@company.local'
            ReplyToAddress       = 'NoReply@company.local'
            DisplayName          = 'mail.company.local'
            MailServerName       = 'mail.company.local'
            Description          = 'Default mail account and profile.'
            LoggingLevel         = 'Normal'
            TcpPort              = 25

            PsDscRunAsCredential = $SqlInstallCredential
        }
    }
}

Example 2

This example will remove the mail profile and the mail account and disable Database Mail on a SQL Server instance.

Configuration Example
{
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.PSCredential]
        $SqlInstallCredential
    )

    Import-DscResource -ModuleName 'SqlServerDsc'

    node localhost {
        SqlServerDatabaseMail 'DisableDatabaseMail'
        {
            Ensure               = 'Absent'
            ServerName           = $Node.NodeName
            InstanceName         = 'DSCSQLTEST'
            AccountName          = 'MyMail'
            ProfileName          = 'MyMailProfile'
            EmailAddress         = 'NoReply@company.local'
            MailServerName       = 'mail.company.local'

            PsDscRunAsCredential = $SqlInstallCredential
        }

        <#
            Don't disable the Database Mail XPs if there are still mail accounts
            left configured.
        #>
        SqlServerConfiguration 'DisableDatabaseMailXPs'
        {

            ServerName     = $Node.NodeName
            InstanceName   = 'DSCSQLTEST'
            OptionName     = 'Database Mail XPs'
            OptionValue    = 0
            RestartService = $false
        }
    }
}
Clone this wiki locally