The script retrieves message trace of an Exchange Online mailbox for the specified number of days and emails it as a CSV file. To execute the script, create a custom command with a parameter that will be used to specify the number of days for the trace.
Parameters:
- $daysNumberParameter - Specifies the name of the parameter used to specify the number of days for retrieving message trace. The parameter name should be specified with the param- prefix. The parameter values must not exceed 10.
- $csvFilePath - Specifies a path to the CSV file that will be created by the script.
- $removeCsvFile - Specifies whether to remove the CSV file after sending.
- $recipient- Specifies a recipient of the email notification.
- $subject - Specifies the email notification subject.
- $from - Specifies the email address from which the notification will be sent.
- $message - Specifies the email notification text.
- $smtpServer - Specifies the SMTP server to use for sending the report.
PowerShell
$daysNumberParameter = "param-MyParam" # TODO: modify me
# CSV file settings
$csvFilePath = "C:\Scripts\Report.csv" # TODO: modify me
$removeCsvFile = $True # TODO: modify me
# E-mail settings
$recipient = "recipient@domain.com" # TODO: modify me
$from = "noreply@domain.com" # TODO: modify me
$subject = "My Subject" # TODO: modify me
$message = "My Message" # TODO: modify me
$smtpServer = "mail.domain.com" # TODO: Modify me
# Get parameter value
$daysNumber = $Context.GetParameterValue($daysNumberParameter)
# Get email address
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
}
}
# Get trace end date
$today = Get-Date
$tenDaysAgo = $today.AddDays(- $daysNumber)
# 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.", "Information")
return
}
$records = New-Object System.Collections.ArrayList
foreach ($item in $messageTrace)
{
$record = New-Object PSObject -Property @{
"SenderAddress" = $item.SenderAddress
"Received" = $item.Received
"Subject" = $item.Subject
}
$records.Add($record)
}
$records.ToArray() | Export-Csv -Path $csvFilePath -NoTypeInformation
# Send mail
Send-MailMessage -to $recipient -From $from -Subject $subject -Body $message -SmtpServer $smtpServer -Attachments $csvFilePath
if ($removeCSVFile)
{
# Remove temporary file
Remove-Item $csvFilePath -Force
}
You cannot call a method on a null-valued expression.
At line:41 char:1
+ $Context.CloudServices.ConnectExchangeOnline()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
when I run it on my local machine it works fine. Am I missing something on the adaxes side?
For troubleshooting purposes, please, send us (support@adaxes.com) a screenshot of the Multi-server environment dialog. The dialog displays how many Adaxes services you have and what their versions are. For information on how to view it, see https://www.adaxes.com/help/MultiServerEnvironment.