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.
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.
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")
Hello,
We added the script for Exchange Online, please, give it a try.
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.