functions/Get-DbaDbMailLog.ps1

function Get-DbaDbMailLog {
    <#
        .SYNOPSIS
            Gets the DBMail log from a SQL instance
 
        .DESCRIPTION
            Gets the DBMail log from a SQL instance
 
        .PARAMETER SqlInstance
            The SQL Server instance, or instances.
 
        .PARAMETER SqlCredential
            Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.
 
        .PARAMETER Since
            Datetime object used to narrow the results to the send request date
 
        .PARAMETER Type
            Narrow the results by type. Valid values include Error, Warning, Success, Information, Internal
 
        .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: DatabaseMail, DBMail, Mail
            Author: Chrissy LeMaire (@cl), netnerds.net
 
            Website: https://dbatools.io
            Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
            License: MIT https://opensource.org/licenses/MIT
 
        .LINK
            https://dbatools.io/Get-DbaDbMailLog
 
        .EXAMPLE
            PS C:\> Get-DbaDbMailLog -SqlInstance sql01\sharepoint
 
            Returns the entire DBMail log on sql01\sharepoint
 
        .EXAMPLE
            PS C:\> Get-DbaDbMailLog -SqlInstance sql01\sharepoint | Select *
 
            Returns the entire DBMail log on sql01\sharepoint, includes all returned information.
 
        .EXAMPLE
            PS C:\> $servers = "sql2014","sql2016", "sqlcluster\sharepoint"
            PS C:\> $servers | Get-DbaDbMailLog -Type Error, Information
 
            Returns only the Error and Information DBMail log for "sql2014","sql2016" and "sqlcluster\sharepoint"
    #>

    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline)]
        [Alias("ServerInstance", "SqlServer")]
        [DbaInstanceParameter[]]$SqlInstance,
        [Alias("Credential")]
        [PSCredential]
        $SqlCredential,
        [DateTime]$Since,
        [ValidateSet('Error', 'Warning', 'Success', 'Information', 'Internal')]
        [string[]]$Type,
        [Alias('Silent')]
        [switch]$EnableException
    )
    process {
        foreach ($instance in $SqlInstance) {
            Write-Message -Level Verbose -Message "Connecting to $instance"

            try {
                $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential
            }
            catch {
                Stop-Function -Message "Failure" -Category Connectiondbmail -dbmailRecord $_ -Target $instance -Continue
            }

            $sql = "SELECT SERVERPROPERTY('MachineName') AS ComputerName,
            ISNULL(SERVERPROPERTY('InstanceName'), 'MSSQLSERVER') AS InstanceName,
            SERVERPROPERTY('ServerName') AS SqlInstance,
            log_id as LogId,
            CASE event_type
            WHEN 'error' THEN 'Error'
            WHEN 'warning' THEN 'Warning'
            WHEN 'information' THEN 'Information'
            WHEN 'success' THEN 'Success'
            WHEN 'internal' THEN 'Internal'
            ELSE event_type
            END as EventType,
            log_date as LogDate,
            REPLACE(description, CHAR(10)+')', '') as Description,
            process_id as ProcessId,
            mailitem_id as MailItemId,
            account_id as AccountId,
            last_mod_date as LastModDate,
            last_mod_user as LastModUser,
            last_mod_user as [Login]
            FROM msdb.dbo.sysmail_event_log"


            if ($Since -or $Type) {
                $wherearray = @()

                if ($Since) {
                    $wherearray += "log_date >= '$($Since.ToString("yyyy-MM-ddTHH:mm:ss"))'"
                }

                if ($Type) {
                    $combinedtype = $Type -join "', '"
                    $wherearray += "event_type in ('$combinedtype')"
                }

                $wherearray = $wherearray -join ' and '
                $where = "where $wherearray"
                $sql = "$sql $where"
            }

            Write-Message -Level Debug -Message $sql

            try {
                $server.Query($sql) | Select-DefaultView -Property ComputerName, InstanceName, SqlInstance, LogDate, EventType, Description, Login
            }
            catch {
                Stop-Function -Message "Failure" -InnerErrorRecord $_ -Continue
            }
        }
    }
}