We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script repository

Output the last time a mailbox received mail

October 31, 2023 Views: 22406

The script outputs the last time when a mailbox received mail. To use the script with Adaxes, create a custom command configured for the required 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 mailbox", "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.

There is no possibility to request emails received in Exchange Online more than 90 days ago.
Edit Remove
PowerShell
# 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
$Context.CloudServices.ConnectExchangeOnline()

# 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 Jun 01, 2020

Hello,

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

avatar
Ben Smith Jun 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 Jun 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
Loading...

Got questions?

Support Questions & Answers