Send automated messages with this Azure Logic Apps tutorial

While PowerShell can handle a seemingly unlimited number of administrative tasks, messaging isn’t one of its strong suits.

It takes some work to configure and authenticate the right modules to configure notifications in PowerShell. If you want to receive messages from scripts that run in multiple places in your organization, the setup process is even more complicated. For example, you may have scheduled scripts that run on domain controllers and want the script to report success or failure. Or, more simply, perhaps you want an easier way to deploy scripts with a smaller footprint. One option that can help with these specialized automation tasks is to use Azure Logic Apps to accept an HTTP POST and send an email or message in Microsoft Teams.

How to create an automated workflow in Azure Logic Apps

First, sign in to the Azure portal, click on the Create a resource button, search Logic app and click on the Create button.

In the Create a logic app select your resource group or create a new one if needed, set your type appropriately — use Consumption unless you need Standard — and give it a name:

Start creating the Azure Logic App workflow by filling in the automated workflow details.

Add tags and create them. Select Go to resource.

The first page that opens when you create an Azure logic app contains common triggers. For this Azure Logic Apps tutorial, we will use the When an HTTP request is received Request trigger for configuring Microsoft Teams and email.

How to use an Azure logic app to send emails

The power of Azure Logic Apps lies in its ease of use. To illustrate, let’s create a two-step automated workflow. First, it will accept multiple parameters via the When an HTTP request is received action, then it will send an email. This tutorial will use the Office 365 email connector, but there are several other email connectors with a send email action, such as the Gmail connector for Google’s email service. Microsoft provides a full list of its Logic Apps connectors here.

For a more flexible workflow, this Azure logic app should accept values ​​for multiple email header fields, such as to, CC, BCC, subject, and message. To tell Azure Logic App how to pass the data, we format this object in JavaScript Object Notation (JSON) and grab it as an example payload.

{
    "To": "to",
    "CC": "cc",
    "BCC": "bcc",
    "Subject": "subject",
    "Message": "message"
}

The Azure Logic application uses the values ​​you provide to determine the type of the property.

Click on Use an example payload to generate a schema and paste the JSON payload.

Azure logic app HTTP request
Configure Azure Logic App to respond to an HTTP trigger with a sample JSON payload.

Click on Do and you should see that the JSON schema is updated in a format compatible with Azure Logic App.

Add the email action by clicking New step and find your email provider. For the purposes of this tutorial, search for Office 365 Outlook and select the Send Email (V2) stock. If you have not configured authentication for this action, a dialog will prompt you to sign in to an account. If you don’t want to use your email account, sign in with a service account or use the Of parameter or select the Send email from a shared mailbox (V2) and use a shared mailbox.

After configuring authentication, the action will appear on the workflow.

send email action
Use the Office 365 Outlook connector to configure the send email action.

You may notice that the workflow does not have all email header fields. Add them by clicking on the Add a new parameter drop-down and check the parameters to add.

To use our defined settings, click on one of the boxes, such as Toand a popup window will offer you hidden values ​​until you click the icon See more to reveal custom settings.

For each parameter, select the corresponding value from the list. For Body, I used the name Message to avoid any confusion with the other Body assess.

parameters defined by e-mail
Add the custom parameters for the send email action.

Click on to safeguard to verify the Azure logic app.

Test the automated workflow for sending emails

To verify the workflow, we need its HTTP POST URL which invokes the Azure Logic App. Open the When an HTTP request is received action to find it.

HTTP Post URL
Find the HTTP POST URL to test the Azure Logic App workflow.

If there is no value in the HTTP Post URL dialog box, try to save the workflow.

Next, copy the value, open a PowerShell script editor, and assign the URL to a variable. Next, create the parameters to send to the Azure logic app by creating a hash table and using ConvertTo-Json:

$body = @{
    To = '[email protected]'
    CC = '[email protected]'
    Subject="Test Subject"
    Message="

This accepts HTML

And here is the boring paragraph

" }

If you are using Windows PowerShell v5.1, you must specify the Content type of the message manually:

$headers = @{    
'Content-Type' = 'application/json'
}

If you are using PowerShell 7, you can do this with the Content type parameter on the Invoke-RestMethod command.

Next, run the Invoke-RestMethod command using a splat to collect the parameter values.

$splat = @{<
    Uri = $uri
    Method = 'POST'
    Headers = $headers
    Body = $body | ConvertTo-Json
}
Invoke-RestMethod @splat

The finished script looks like this:

$uri = ''
$body = @{
    To = '[email protected]'
    CC = '[email protected]'
    Subject="Test Subject"
    Message="

This accepts HTML

And here is the boring paragraph

" } $headers = @{     'Content-Type' = 'application/json' } $splat = @{     Uri = $uri     Method = 'POST'     Headers = $headers     Body = $body | ConvertTo-Json } Invoke-RestMethod @splat

Take a test by sending yourself an e-mail.

Azure Logic App automated email
If the Azure Logic application is configured correctly, the sample test email should appear.

If the email doesn’t arrive, check the execution history on the Azure Logic App for errors.

How to Send an Automated Message in Microsoft Teams

Microsoft Teams has become one of the most popular communication platforms for businesses, and it’s adaptable enough to be used with Azure Logic Apps for automated messaging.

To get started with this example workflow, configure the When an HTTP request is received action with an example JSON payload:

{
    "Message": "message",
    "Channel": "channel"
}

For the first action, find Microsoft Teams and select the Channel list stock. Then select a team name. This returns the channels associated with this team.

To search for a channel by name and get its ID, click the plus sign to add an action, then scroll down to Choose an operation box. Click it Built-in data operations icon.

Locate Filter Matrix from the list. In the dynamic fields, add Channel list in the Of section and set the Channel name as equal to the Channel HTTP POST action value.

Next, add a for-each loop by scrolling down in the Choose an operation box, then select Control.

Choose the For each stock. For the Select an output from the previous steps parameter, choose the Body filter table option.

You can select Add action inside the for-each loop that will run for each channel matching the name passed. It should be 1 or 0.

for each loop
Add the action to the for-each loop.

From there, select Microsoft Teams and the Post a message in a chat or channel stock.

For the Crew setting, select the same team used in the Channel list stock.

Then for the Channelyou will need to select the Enter a custom value option.

From the dynamic content you should see Channel ID sub-network of filters.

Microsoft Teams Channel ID
Select the channel ID from the filter table as part of the Microsoft Teams automated messaging workflow.

Select this value, then set the message to Message property of the When an HTTP request is received trigger.

Save the workflow to keep your progress. Run a test with a slightly modified version of the script used in the automated email workflow.

$uri = ''
 
$body = @{
    Channel="General"
    Message="This is a message!"
}
$headers = @{
    'Content-Type' = 'application/json'
}
 
$splat = @{
    Uri = $uri
    Method = 'POST'
    Headers = $headers
    Body = $body | ConvertTo-Json
}
Invoke-RestMethod @splat

If successful, a message will appear in the Microsoft Teams channel.

Message from Microsoft Teams
After setup, send a test message to the Microsoft Teams channel.

Cloud-based automation can meet some needs

Microsoft Azure Logic Apps simplifies automated messaging to make it easier to deploy to various places in your infrastructure. You won’t have to worry about setting up additional modules to manage authentication to Exchange Online or Microsoft Teams or enabling SMTP authentication and then explaining yourself to the security team.

The added benefit is that your script can scale to Azure’s capacity, so sending hundreds of emails in a single run won’t overwhelm the system. You’ll have one less worry about your infrastructure and more time to think about the jobs that matter.

Comments are closed.