Programmatically monitoring your Power Automate Flows

In my workflow session at Directions EMEA, we talked about how to overcome the limitations of Power Automate for creating enterprise-level workflows. Power Automate is a great solution for creating user-centric workflows, but for enterprise orchestrations or for highly available workflows, there’s more 🙂

One of the question received post session was how to monitor a Power Automate flow remotely. I have two possible solutions for this:

  • Implement a TRY-CATCH-FINALLY pattern and then send data to Azure Application Insights
  • Use CLI for Microsoft 365 to check the status of your flows

The first solution is the most complex one. Unfortunately Power Autonate has not a built-in way for sending telemetry to Applicartion Insights and for doing that you need to implement the TRY-CATCH-FINALLY pattern in your flow by using scopes and in the CATCH block you can call a custom Azure Function that send telemetry data to Application Insights.

The flow will be something like the following:

The Run after conditions for the CATCH and FINALLY scopes are the following:

In the CATCH block we should call the Azure Function that logs the exceptions to Application Insights. Full implementation is not provided here but the schema is the following:

In this way you can have a Power Automate flow that logs exceptions (and everything you want) to Azure Application Insights and then you can monitor telemetry signals with all the standard tools provided by Azure Monitor.

But what about the second solution in the list on top of this post?

Sometimes you don’t need the full power of telemetry, but you need only a way to programmatically discover if your Power Automate flows are running or not in a given period of time. This is where the Microsoft 365 CLI comes in help.

CLI for Microsoft 365 is an open-source project driven by the Microsoft 365 Patterns and Practices initiative. The project is built and managed publicly on GitHub at https://github.com/pnp/cli-microsoft365 and accepts community contributions. This CLI permits you to have a set of Powershell commands foe monitoring Microsoft 365 Applications.

The CLI for Microsoft 365 is distributed as an NPM package. To use it, install it globally using the following command:

npm i -g @pnp/cli-microsoft365

The first thing to do is login to your Microsoft 365 environment and you can do that by executing the following command:

m365 login

Then, you can connect to a specific environment:

Write-Output "Connecting to the environment"
$environment = $(m365 flow environment list --query "[?displayName == 'YOUR_ENVIRONMENT_NAME']" -o json | ConvertFrom-Json).Name

You can retrieve the list of Power Automate flows in your selected environment:

$flows = m365 flow list --environment $environment --asAdmin --output json | ConvertFrom-Json

and then for each flow you can retrieve statistics in a given period.

Here is the script that retrieves the statistics for all the flows in the selected environment in the last 7 days:

$currentDayDate = Get-Date
$previousDayDate = (Get-Date).AddDays(-7)

$flows = m365 flow list --environment $environment --asAdmin --output json | ConvertFrom-Json

foreach ($flow in $flows) 
{
    $flowRuns = m365 flow run list --environment $environment --flow $flow.name --output json
    $flowRuns = $flowRuns | ConvertFrom-Json

    $displayName = $flow.displayName
    $id = $flow.name

    $todayRuns = $flowRuns.Where({[DateTime]$_.properties.endTime -le $currentDayDate -and [DateTime]$_.properties.endTime -gt $previousDayDate})

    $todayRunsCount = 0
    $todaySuccessRunsCount = 0
    $todayFailedRunsCount = 0
    if($todayRuns.Count -gt 0)
    {
        $todaySuccessRuns = $todayRuns.Where({$_.status -eq 'Succeeded'})
        $todaySuccessRunsCount = $todaySuccessRuns.Count

        $todayFailedRuns = $todayRuns.Where({$_.status -eq 'Failed'})
        $todayFailedRunsCount = $todayFailedRuns.Count

        $todayRunsCount = $todayRuns.Count
    }

    Write-Host "$displayName -> Runs: $todayRunsCount , Succeeded: $todaySuccessRunsCount , Failed: $todayFailedRunsCount"    
}

The output of this script is the following (with obviously real world numbers :D):

In this way you can have a Powershell script that monitors your flow (or a particular flow given the name) and then react programmatically on failures or simply doing statistics on usage.

A specific flow can be retrieved by name with the following command:

$flowId = $(m365 flow list --environment $environmentId --query "[?displayName == 'Low Inventory Notification']" -o json | ConvertFrom-Json)[0].Name

and you can also export your flows in ZIP or JSON format for external savings:

$flows | ForEach-Object {
    Write-Output "Exporting as ZIP & JSON... $($_.displayName)"
    $filename = $_.displayName.Replace(" ","")
    $timestamp = Get-Date -Format "yyyymmddhhmmss"
    $exportPath = "$($filename)_$($timestamp)"
    $flowId = $_.Name

    m365 flow export --id $flowId --environment $environment --packageDisplayName $_.displayName --path "$exportPath.zip"
    m365 flow export --id $flowId --environment $environment --format json --path "$exportPath.json"
}

Exporting in JSON can be useful if you want to move your Power Automate flow to an Azure Logic App, as explained in my session at Directions EMEA.

Do you think that these features are can be useful? I think so… 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.