Thor Logo dbatools

Get-DbaDbMailHistory

View Source
Chrissy LeMaire (@cl), netnerds.net
Windows, Linux, macOS

Synopsis

Retrieves Database Mail history from SQL Server’s msdb database for troubleshooting and compliance

Description

Retrieves comprehensive Database Mail history from the msdb.dbo.sysmail_allitems table, including delivery status, recipients, subject lines, and timestamps. This function helps DBAs troubleshoot email delivery issues, audit mail activity for compliance reporting, and monitor Database Mail performance. You can filter results by send date or delivery status (Sent, Failed, Unsent, Retrying) to focus on specific timeframes or problem emails.

Syntax

Get-DbaDbMailHistory
    [[-SqlInstance] <DbaInstanceParameter[]>]
    [[-SqlCredential] <PSCredential>]
    [[-Since] <DateTime>]
    [[-Status] <String>]
    [-EnableException]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Get-DbaDbMailHistory -SqlInstance sql01\sharepoint

Returns the entire DBMail history on sql01\sharepoint

Example: 2
PS C:\> Get-DbaDbMailHistory -SqlInstance sql01\sharepoint | Select-Object *

Returns the entire DBMail history on sql01\sharepoint then return a bunch more columns

Example: 3
PS C:\> $servers = "sql2014","sql2016", "sqlcluster\sharepoint"
PS C:\> $servers | Get-DbaDbMailHistory

Returns the all DBMail history for “sql2014”,“sql2016” and “sqlcluster\sharepoint”

Optional Parameters

-SqlInstance

The target SQL Server instance or instances.

PropertyValue
Alias
RequiredFalse
Pipelinetrue (ByValue)
Default Value
-SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Since

Filters mail history to only include emails sent after the specified date and time.
Use this when troubleshooting recent delivery issues or generating reports for specific time periods.
Accepts standard PowerShell DateTime objects like (Get-Date).AddDays(-7) for the past week.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Status

Filters results to only show emails with the specified delivery status.
Use ‘Failed’ to identify delivery problems, ‘Unsent’ for queued messages, or ‘Retrying’ for current retry attempts.
Accepts multiple values: Unsent, Sent, Failed, and Retrying.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
Accepted ValuesUnsent,Sent,Failed,Retrying
-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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse

Outputs

PSCustomObject

Returns one object per Database Mail message from the MSDB sysmail_allitems table.

Default display properties (via Select-DefaultView):

  • ComputerName: The computer name of the SQL Server instance
  • InstanceName: The SQL Server instance name
  • SqlInstance: The full SQL Server instance name (computer\instance)
  • Profile: The Database Mail profile name associated with this message
  • Recipients: Email addresses of the primary recipients
  • CopyRecipients: Email addresses of the CC recipients
  • BlindCopyRecipients: Email addresses of the BCC recipients
  • Subject: The subject line of the email message
  • Importance: The importance level (Low, Normal, High)
  • Sensitivity: The sensitivity level (Normal, Personal, Private, Confidential)
  • FileAttachments: File attachments included with the message
  • AttachmentEncoding: Character encoding used for attachments
  • SendRequestDate: DateTime when the message was requested to be sent
  • SendRequestUser: Windows or SQL login that initiated the email
  • SentStatus: The delivery status (Unsent, Sent, Failed, Retrying)
  • SentDate: DateTime when the message was actually sent (or failed)

*Additional properties available (via Select-Object ):

  • MailItemId: Unique identifier for this mail message in the sysmail_allitems table
  • ProfileId: Unique identifier of the Database Mail profile
  • Body: The message body text
  • BodyFormat: The body format (HTML or TEXT)
  • Query: T-SQL query that generated query results attached to the message
  • ExecuteQueryDatabase: Database where the query was executed
  • AttachQueryResultAsFile: Whether query results were attached as a file
  • QueryResultHeader: Whether query result headers were included in the attachment
  • QueryResultWidth: Width of the query result output
  • QueryResultSeparator: Character used to separate columns in query results
  • ExcludeQueryOutput: Whether to exclude the query execution output
  • AppendQueryError: Whether to append query errors to the output
  • SentAccountId: Account ID used to send the message
  • LastModDate: DateTime when this mail item record was last modified
  • LastModUser: Login that last modified this mail item record