Using PowerShell to monitor a Power Automate flow

Translated from the Spanish original. Read in Spanish

In this tutorial we’ll learn how to monitor the status of a flow in Power Automate using PowerShell. This is useful if you need to keep track of the flow’s runs and get alerts when certain conditions occur.

iStock AI Generator

Connect to SharePoint Online

To start, you need to connect to SharePoint Online using the system-assigned managed identity. Use the following PowerShell command to connect:

Connect-PnPOnline -url zerogap.sharepoint.com -ManagedIdentity

Define the flows to monitor

Next, define the Power Automate flows you want to monitor. To do so, create a dictionary with each flow’s name and its ID.

$flows_to_monitor = @{Audit_Log_Handler = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}

Set the alert webhook target

Set the variable that holds the webhook the alerts will be sent to. This webhook can be the URL of a service such as Teams, Slack, etc.

$webhook = Get-AutomationVariable -Name "ExampleAlertWebhookVariable"

Get the latest runs

For each flow, get the latest runs with the get-pnpflowrun command.

     $flow_id_string=[String]$flow.Values
     write-output  $flow_id_string
     $monitor=get-pnpflowrun -Environment "yyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" -Flow $flow_id_string | select -First 10

Count and alert if too many jobs are running

Check each job and, if its status is “Running”, increase the alert counter.

     foreach ($job in $monitor) {
         if ($job.Properties.Status -eq "Running") {
             $counter_alert++
         }
     }

If more than 5 jobs are running at the same time, an alert is raised.

     write-output $counter_alert
     if ($counter_alert -gt 5 ) {

Take action when there’s an alert

Decide what to do when the alert condition is met — for example, send a notification through the webhook with details about the flow and the most recent job.

         $job_id=$monitor[0].id
         $flow_name=[String]$flow.keys
         $payload = @{"text" = "Flow Alert: at least 5 jobs are still running in the $flow_name flow. The last job execution is $job_id"}
         Invoke-RestMethod -uri $webhook -method Post -Body (convertto-json -InputObject $payload)
     }
}

With this PowerShell script, you can keep your Power Automate flows under constant watch and be alerted to any unusual behaviour that may need your attention.

Complete example

# Connect using the system managed Identity
Connect-PnPOnline -url zerogap.sharepoint.com -ManagedIdentity

####  Flows to monitor ####
$flows_to_monitor = @{Audit_Log_Handler = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}
$counter_alert = 0

## Alert Webhook Target ##
$webhook = Get-AutomationVariable -Name "ExampleAlertWebhookVariable"

# Check last 10 jobs for all targeted flows
foreach ($flow in $flows_to_monitor) {

     # Get the last 10 runs
     $flow_id_string=[String]$flow.Values
     write-output  $flow_id_string
     $monitor=get-pnpflowrun -Environment "yyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" -Flow $flow_id_string | select -First 10

     foreach ($job in $monitor) {
         if ($job.Properties.Status -eq "Running") {
             $counter_alert++
         }
     }
     # If there are more than 5 jobs running at the same time. Alert.
     write-output $counter_alert
     if ($counter_alert -gt 5 ) {
         # Do something
         $job_id=$monitor[0].id
         $flow_name=[String]$flow.keys
         $payload = @{"text" = "Flow Alert: at least 5 jobs are still running in the $flow_name flow. The last job execution is $job_id"}
         Invoke-RestMethod -uri $webhook -method Post -Body (convertto-json -InputObject $payload)
     }
}

Maximiliano Díaz Doglia

AI Platform Engineer & Full-Stack Developer
Building Enterprise Integrations & Automations