Script Repository


Output the last time a distribution list received mail

June 01, 2020
4879

The script outputs the last time when a distribution list received mail. To use the script with Adaxes, create a Custom Command configured for the Group object type.

Exchange on-premises

Parameters:

  • $exchangeServer - Specifies the fully qualified domain name or IP address of your Exchange Server.
Edit Remove
PowerShell
$exchangeServer = "exchangeserver.domain.com" # TODO: Modify me

try
{
    # Connect to the Exchange Server
    $session = New-PSSession -connectionURI "http://$exchangeServer/powershell" -ConfigurationName Microsoft.Exchange
    Import-PSSession -session $session -AllowClobber -DisableNameChecking

    # Get the message tracking log for the distribution list
    $messageTrackingLog = Get-MessageTrackingLog -Recipients "%mail%" -ResultSize Unlimited | Select-Object sender, timestamp | Sort timestamp -Descending
}
finally
{
    # Close connection to the Exchange Server
    Remove-PSSession -Session $session
}

if ($messageTrackingLog -eq $NULL)
{
    $Context.LogMessage("There were no messages sent to this distribution list", "Information")
    return
}

$sender = $messageTrackingLog[0].Sender
$timeStamp = $messageTrackingLog[0].TimeStamp

$Context.LogMessage("Last message received from '$sender' on '$timeStamp'", "Information")

Exchange Online

The script outputs information about the last email received during the previous 10 days. To obtain the same information for a longer period of time, use the ​Start-HistoricalSearch and ​Get-HistoricalSearch cmdlet.

To run the script, install the EXO V2 PowerShell module on the computer where Adaxes service runs.

There is no possibility to request emails received by distribution lists in Exchange Online more than 90 days ago.
Edit Remove
PowerShell
Import-Module ExchangeOnlineManagement

# Get email address
$recipientAddress = "%mail%"
if ([System.String]::IsNullOrEmpty($recipientAddress))
{
    if ($Context.TargetObject.RecipientLocation -eq "ADM_EXCHANGERECIPIENTLOCATION_NONE")
    {
        $Context.LogMessage("The object does not have an Exchange mailbox", "Error")
        return
    }

    $mailboxParams = $Context.TargetObject.GetMailParameters()
    $emailAddresses = $mailboxParams.EmailAddresses
    for ($i = 0; $i -lt $emailAddresses.Count; $i++)
    {
        $emailAddress = $emailAddresses.GetAddress($i,[ref]"ADS_PROPERTY_NONE")
        if ($emailAddress.Prefix -eq "smtp" -and $emailAddress.IsPrimary)
        {
            $recipientAddress = $emailAddress.Address
            break
        }
    }
}

$today = Get-Date
$tenDaysAgo = $today.AddDays(-10)

# Connect to Exchange Online
Connect-ExchangeOnline -Credential $Context.GetOffice365Credential()

# Get the message trace
try
{
    $messageTrace = Get-MessageTrace -RecipientAddress $recipientAddress -StartDate $tenDaysAgo -EndDate $today -ErrorAction Stop | Sort Received -Descending
}
catch
{
    $Context.LogMessage("An error occurred while getting messages trace. Error: " + $_.Exception.Message, "Error")
    return
}

if ($NULL -eq $messageTrace)
{
    $Context.LogMessage("There were no messages sent to this distribution list", "Information")
    return
}

$senderAddress = $messageTrace[0].SenderAddress
$received = $messageTrace[0].Received

$Context.LogMessage("Last message received from '$senderAddress' on '$received'", "Information")

Comments ( 4 )
avatar
Swapnil joshi
May 30, 2020

How to perform same task in O365

avatar
Support
June 01, 2020

Hello,

We added the script for Exchange Online, please, give it a try.

avatar
Ben Smith
June 03, 2020

Can we modify or add to this script to generate the last 90 days of usage and export that into a report? We are using O365 so that would be nice to have available. I'd prefer to generate a spreadsheet and see the activity for all of my distribution lists.

avatar
Support
June 05, 2020

Hello Ben,

Message trace for more than 10 days cannot be displayed in Adaxes because its generation in Exchange Online takes up to 48 hours and the results can only be sent via email as a CSV file. To receive the report, you can use the Start-HistoricalSearch cmdlet.

Leave a comment