Mail Flow Round Trip test using Azure Automation and Azure Log Analytics

Every once in a while a customer asks if we can create a mail flow between different email systems and measure its roundtrip time.
So, this reference highlights one solution based on a PowerShell script that connects to one mailbox hosted in Exchange, sends a message to another mailbox hosted in Gmail, waits for its return and tracks the roundtrip time.

We can definitely run such a script from SCOM, but for this example I wanted to use as much of the new world ( Azure Management Services ) as I could.

  • The test will run in Azure Automation ( required to run the script )
  • Trigger in an interval by an Azure Logic App ( Although we can use a schedule in Azure Automation, with logic Apps we can have an interval as low as 1 minute )
  • The result will be sent to a Log Analytics Workspace ( required to store the test results )
  • Use Power BI report to have appealing visuals of the solution ( not required but cool )

Let´s start !!

Configure the test accounts:
I´ve just use my Microsoft account and a new Gmail test mailbox that I´ve configure to forward all incoming emails back to my account.

Configure script dependencies:
The script makes use of Microsoft Exchange Web Services Managed API so we need to make this available in the environment where the script will run.
Because we´ll be running in Azure Automation and Bartek Bielawski was so kind in providing a wrapper around Microsoft.Exchange.WebServices library, we only need to add the PowerShell module to an existing Azure Automation account.
Then, import the module and we can use simple commands such as Connect-EWSService or New-EWSMessage to connect to the mailbox and send or retrieve messages.
Finally, add the credential and the Log Analytics Workspace ID and Key variables to the automation account assets library so that they can be securely retrieved during runtime:

$cred = Get-AutomationPSCredential -Name "<credentialName>"
$customerId = Get-AutomationVariable -Name "<workspaceID>"
$SharedKey = Get-AutomationVariable -Name "<workspaceKey>"

Run the flow test script:
Create a new Azure automation PowerShell Runbook, paste the script and adapt the variables to your own environment:


The script simple creates a new guid to be sent as the message subject and tracks the round trip time with a Diagnostics.Stopwatch


If the message is sent, we then wait for it to come back by searching the inbox with the previous created guid as a filter.
If we don't time out, then a "Result:Success" log will be created in the Log Analytics service:


Create a schedule in Azure Logic Apps

If you haven't play around with Logic Apps then you don't know what you're missing :)
There’s already a huge amount of triggers and actions available in the service that can help to integrate different systems and create  simple “playbooks”.

One of those triggers is a recurrence that we can configure to trigger an action of type Create Job in Azure automation:



Querying the output:
After a couple a minutes, we'll be able to query entries of MailRoundTrip_CL Type ( assuming that the log type wasn’t changed in the script ).

This example plots the roundtrip time as a timechart:

| where TimeGenerated > ago(6h)
| extend FromLocation = case(Location_s == "", "Not Available", Location_s), Domains = split(To_s, "@")
| project FromLocation , RoundTripToDomain = Domains[1], TimeGenerated, Result_s, RoundTripTime_d
| extend test= strcat(FromLocation, " <-> ", RoundTripToDomain)
| summarize avg(RoundTripTime_d) by test, bin( TimeGenerated, 10m)
| render timechart

And this one will get the last test result for by location:

| where TimeGenerated > ago(4h)
| extend FromLocation = case(Location_s == "", "Not Available", Location_s), Domains = split(To_s, "@")
| project FromLocation , RoundTripToDomain = Domains[1], TimeGenerated, Result_s, RoundTripTime_d
| summarize TestTime = arg_max(TimeGenerated, Result = Result_s) by FromLocation, tostring(RoundTripToDomain)

Creating the Power BI Dashboard:
Now that the data is being created, we just need to click the Power BI query in the export options:


Then, open Power BI desktop, click Get Data, choose blank query, click advanced editor and paste the contents from the download file.
After authenticating, we should be able to get a dataset that can be used to create appealing visuals:

image Refer to this repo ( ) for the complete script.

Happy Dashboarding,