Azure PowerShell: Setting Up Alerts to Monitor Disabled Logic Apps

Introduction

Logic Apps are a piece of integration workflow hosted on Azure which are used to create scale-able integrations between various systems. These are very easy to design and provide connectivity between various disparate systems using many out of the box connectors as well as with the facility to design custom connectors for specific purposes. This makes integration easier than ever as the design aspect of the earlier complex integrations is made easy with minimum steps required to get a workflow in place and get it running.

↑Back To Top


Problem Scenario

In some cases it may happen that one or many of the logic apps running under a certain resource group get disabled or some one disables them accidentally, then in such cases it is necessary that an alert be sent out to the administrator that the Logic App is disabled and if it is genuine case then okay, else the administrator can act on it and enable the logic apps.

↑Back To Top


Possible Solutions

Activity Log Based Alerts

One of the possible solutions to the issue is to use the Azure Monitoring Feature and create Alerts so that whenever Workflow(Logic App) is successfully disabled, then an email alert should be sent out with the necessary details. In order to understand how to configure the Activity Log based Alerts, please refer the See Also section. In this approach, the subscriber will have to pay up a samll amount of fees based upon how much they make the use of the Log Analytics feature.

Azure PowerShell Modules

Another solution to this monitoring issue is to use Azure PowerShell modules and mail the necessary details to the target audience. This way does not involve any monetary levy and is free. 

This article will explore the PowerShell way of setting up the monitoring Alerts

↑Back To Top


Implementation

Installing Azure PowerShell Module and Save the UserContext

Azure Powershell Module can be downloaded and installed from the PowerShell gallery using following command. 

Install-Module -Name AzureRM -AllowClobber

The PowerShell console needs to be run as administrator.

In order to learn more about the installation of multiple versions of Azure PowerShell on the same machine, refer to Install Azure PowerShell on Windows with PowerShellGet

In case we want to use any cmdlets from the Azure RM module we need to log in first using the Azure portal credentials so the PowerShell cmdlets can use the credentials to do the necessary tasks. The only problem with this is that we need to log in every time whenever we exit the current session and re run the commands in the new sessions. In order to get over this hassle, starting from AzureRM module 6.3 onward, we have the ability to save the user context using the Enable-AzureRmContextAutoSave command. So whenever we are using the Azure cmdlets for first time, then we run the Enable-SaveAzureRmContext cmdlet to save the context. Once this step is done, the credentials are available in the next session also and the PowerShell scripts can be run in the background jobs as well as using schedulers.

To read more about how to save the context, please refer to the Persist user credentials across PowerShell sessions 

PowerShell Script

Once the ground work is done, only task remains is to create a PowerShell Script which can be used to set up the necessary alerts. The PowerShell script is as below.

Function GetDisabledAzureLogicAppsAlerts
{
    param
    (
        # The Resource Group Name
        [Parameter(Position=0,Mandatory=$true,HelpMessage="Resource Group Name is Mandatory")]
        [string]$ResourceGroupName
    )
     
    # Get all the Logic Apps in the particular Resource Group
    $logicApps= Get-AzureRmResource -ResourceType 'Microsoft.Logic/workflows' -ResourceGroupName $ResourceGroupName
    [string]$disabledLogicAppsName
    foreach($logicApp in $logicApps)
    {
        #Obtain the state of the Logic app using the Get-AzureRmLogicApp Command
        $la = Get-AzureRmLogicApp -ResourceGroupName $ResourceGroupName -Name $logicApp.Name
         
        if($la.State -eq 'Disabled')
        {
            $disabledLogicAppsName = $disabledLogicAppsName + '<p>' + $la.Name + '</p>'
        }
         
         
    }
     
    $emailBody = '<p>Hi User,</p>
    <p>Following Logic Apps in your subscription in the <strong><span style="color: #000080;">Resources Group : AzureStudyGroup</span></strong>
     are in <strong><span style="color: #ff0000;">disabled </span></strong>state. Your immediate attention is required.</p>
    <p> </p>{0}
    <p>Regards,</p>
    <p>AzureSupportTeam,</p>
    <p> </p>'
 
    $emailBody = [string]::Format($emailBody, $disabledLogicAppsName)
 
    if([string]:: IsNullOrEmpty($disabledLogicAppsName) -eq $false)
    {
        Send-MailMessage -SmtpServer 'localhost' -Port 25 -From "abc@pqr.com" -To 'xyz@abc.com' -Subject 'Logic App Disabled- `Attenttion Required' -Priority High -Body $emailBody -BodyAsHtml 
    }
 
}

The details about the cmdlets used in above scripts are found in the reference section. It is suggested that the readers familiarize themselves with the cmdlets. Above script can be scheduled to run at ever few minutes on using windows task scheduler.

↑Back To Top


Testing

Following is sample test mail received using above PowerShell script.

↑Back To Top


Conclusion

It can be concluded from the testing results that it is very easy to use the PowerShelll script with Azure RM module to set up the alerts for the Disabled Logic Apps
↑Back To Top


See Also

Further reading can be done using following links

↑Back To Top


References

Following articles were referred while writing this article

↑Back To Top