April 13, 2024

Get Power Automate Fails with PowerShell

Visibility of flow failures is important in terms of managing a Power Platform environment. If something you thought was automated is actually failing, this could cause a daisy chain of pain for you and people using your application. There are several ways of getting a handle on this, but I wanted something I could run from the command line.

What Libraries Did You Use?

For this exercise, I used three different PowerShell libraries:

The first library (Microsoft.PowerApps.Administration.Powershell) does most of the work and reads the data. I then passed it to Import-Excel, which loads the data to Excel. Lastly, a Burnttoast notification to let me know this is done because the run takes a few minutes.

The Code

Here is the code:

#Step 1 - Run this command
#You can add different endpoints if you are on GCC or other tenant
Add-PowerAppsAccount -Endpoint "usgov"

#-------------------------------------

#Step 2 - run this command 
#Get a list of your flows
$flows = Get-AdminFlow

#----------------------------------------

#Step 3 - run the remainder of this code all at once

# Set how many days you want to check, this checks 3
$filterDate=(Get-Date).AddDays(-3)

# Set the url for your tenant
$replaceURL = "https://your-tenant-powerautomate-url/"

# Where do you want the file saved
$path = "C:\Temp\FailedFlowsLastThreeDays.xlsx"

# Loop and check for failures
$data = foreach ($flow in $flows) {Get-FlowRun -FlowName $flow.FlowName -EnvironmentName $flow.EnvironmentName | Where-Object {($_.Status -eq 'Failed') -and ($_.StartTime -ge $filterDate)}}

# Expand inner property
$output = $data | Select-Object FlowRunName, Status, StartTime -ExpandProperty Internal

# Fix the relative url and export to Excel
$output | Select-Object name, Status, StartTime, @{Name='link';Expression={$_.id.Replace("/providers/Microsoft.ProcessSimple/", $replaceurl)}} | Export-Excel -Path $path

# Get a toast notification that this is done
New-BurntToastNotification  -Text "Power Automate Failure Report",'The script is done running!'

How Does This Work?

There are two introductory steps that you need to do one by one: Add-PowerAppsAccount and Get-AdminFlows. These steps connect you to your environment and pull the list of flows. I had to do these sequentially by pasting them into PowerShell one-by-one. There are ways to save credentials and bypass the login window that pops up each time, but for me and the tenant I work mostly on having a 2 second popup is a tiny inconvenience.

After that, I pasted the rest in. The rest of the code starts out by setting variables (How many days I want to look back? What is the url for my tenant? Where do I want to save the Excel file?). After that, a quick foreach loop on the list of flows to pull failed flows that are within the window I am looking at. There are nested properties in this one, so the $output variable is filled after expanding one we need (Internal). The next step pulls out the id (which actually is a relative url in the Internal property) and a quick replace on it turns that into a url we can use. This is sent to an Excel file at a place of my choosing and I get a toast notification letting me know this is all done.

Conclusion

There are probably many other ways to do this, but personally I am enjoying using PowerShell more and more to do quick administrative things across different products. Although it may be a little bit more work to set up, if I only have to go one place to do one group of tasks, that is a win for me and my busy day.

You may also like