Send automated messages with this Azure Logic Apps tutorial – TechTarget | Hot Mobile Press

While PowerShell can handle a seemingly unlimited number of administrative tasks, messaging is not one of its strong points.

It takes work to configure and authenticate the right modules to set up notifications in PowerShell. If you want to receive messages from scripts running in multiple locations in your organization, the setup process is even more complicated. For example, you may have scripts scheduled to run on the domain controllers and you want the script to report success or failure. Or, more simply put, you might want an easier way to deploy scripts that takes up less space. One option that can help with these specialized automation jobs 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 the Create a resource button, search logic app and click the Create Button.

In which Create 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 out the automated workflow details.

Add and create any tags. Choose Go to resource.

The first page that opens when creating an Azure Logic App contains some common triggers. For this Azure Logic Apps tutorial, we’ll 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 email

The strength of Azure Logic Apps lies in their ease of use. To illustrate, let’s create a two-step automated workflow. First, it accepts several parameters via the When an HTTP request is received Action and then an email will be sent. This tutorial uses the Office 365 email connector, but there are several other email connectors with an action to send email, e.g. B. the Gmail connector for the email service from Google. Microsoft provides a full list of its Logic Apps connectors here.

For a more flexible workflow, this Azure logic app should accept values ​​for several email header fields, e.g. E.g. To, CC, BCC, Subject and Message. To tell Azure Logic App how to pass the data, we’ll format this object in JavaScript Object Notation (JSON) and provide it as a sample payload.

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

Azure Logic App uses the values ​​you provide to determine what type the property is.

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

Azure Logic App HTTP request
Set up Azure Logic App to respond to an HTTP trigger with a sample JSON payload.

click Finished and you should see the JSON schema is updated to a format compatible with Azure Logic App.

Add the email action by clicking new step and look for your email provider. For the purposes of this tutorial, search for Office 365 Outlook and select that Send email (V2) Action. If you haven’t set up authentication for this action, a dialog box will ask you to sign in to an account. If you don’t want to use your email account, either sign in with a service account or use that Out of parameters or select the Sending an email from a shared mailbox (V2) and use a shared mailbox.

After setting up the authentication, the action will appear in the workflow.

Send Email Action
Use the Office 365 Outlook connector to set up the Send Email action.

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

To use our defined parameters, check one of the boxes, such as toand a pop-up window suggests values ​​that are hidden until you click See more to view the custom parameters.

For each parameter, select the appropriate value from the list. To the Body, I used the name news to avoid confusion with the other Body Value.

Email Defined Parameters
Add the custom parameters for the Send Email action.

click Save on computer to validate the Azure Logic App.

Test the automated workflow for sending emails

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

HTTP POST URL
Locate the HTTP POST URL to test the Azure Logic App workflow.

If there is no value in the HTTP POST URL 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 be sent to Azure Logic App by creating a hash table and using ConvertTo-Json:

$body = @{
    To = '[email protected]'
    CC = '[email protected]'
    Subject="Test Subject"
    Message="<h1>This accepts HTML</h1><p>And here is the boring paragraph</p>"
}

If you are using Windows PowerShell v5.1, you must provide the content type the message manually:

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

If you’re using PowerShell 7, you can do that with the content type -Parameters of the Invoke-RestMethod command.

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

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

The finished script looks like this:

$uri = '<uri from logic app>'
$body = @{
    To = '[email protected]'
    CC = '[email protected]'
    Subject="Test Subject"
    Message="<h1>This accepts HTML</h1><p>And here is the boring paragraph</p>"
}
$headers = @{
    'Content-Type' = 'application/json'
}
$splat = @{
    Uri = $uri
    Method = 'POST'
    Headers = $headers
    Body = $body | ConvertTo-Json
}
Invoke-RestMethod @splat

Take a test by sending an email to yourself.

Automated email from Azure Logic App
If the Azure Logic App is properly configured, you should see the sample test email.

If the email doesn’t arrive, check the Azure Logic App run history 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 is adaptable enough to be used with Azure Logic Apps for automated messaging.

To get started with this sample workflow, set up the When an HTTP request is received Action with a sample JSON payload of:

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

For the first action, search for Microsoft Teams and select that list channels Action. Then choose a team name. This will return the channels associated with that team.

To find a channel by name and get the ID, click the plus sign to add an action, then scroll to Choose an operation Crate. press the Integrated data operations Symbol.

Locate filter array from the list. Add from the dynamic fields channel list in which Out of section and set the channel name than equal to that channel Value from HTTP POST action.

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

Select the For each Action. For the Choose an output from the previous steps parameters, select the Body Option from the filter array.

You can choose Add an action inside the for-each loop that is executed for each channel that matches the given name. This should be either 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 Action.

For the team Parameters, select the same team used in list channels Action.

Then for them channeldo you want to select the Enter a custom value Possibility.

From the dynamic content you should see Channel ID under filter array.

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

Select this value and then set the message to news property from the When an HTTP request is received Deduction.

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 = '<uri from logic app>'
 
$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.

Microsoft Teams message
Once configured, send a test message to the Microsoft Teams channel.

Cloud-based automation can meet specific needs

Microsoft Azure Logic Apps simplifies automated messaging to simplify deployment to various points in your infrastructure. You no longer have to worry about configuring additional modules to handle authentication to Exchange Online or Microsoft Teams, or enabling SMTP authentication and then having to explain yourself to the security team.

The added benefit is that your script can be scaled to the capacity of Azure, so sending hundreds of emails in a single run doesn’t overwhelm the system. You no longer have to worry about your infrastructure and have more time to take care of the important tasks.

Leave a Comment