Get notifications when flows fail
There are many ways to deal with errors in Power Automate flows: You can check the run history of a flow, use Power Platform Analytics to get an overview and with some skillful logic, you can even make the failing flow itself notify you of an issue.
But what happens if a flow fails unexpectedly? You may find out about it immediately or hours later, either way it would be great if you got an email about it, right? Even better a ticket would automatically be created in your organization's IT support system!
It's a bit tricky but turns out it's possible (even creating the ticket part).
Get flow run data programmatically
Currently the only way I see to programmatically get information about the run history of a flow is through the PowerShell cmdlets for PowerApps. Neither the API, nor Power Automate Management actions provide this information.
So this is where I started:
Install-Module -Name Microsoft.PowerApps.Administration.PowerShell
Install-Module -Name Microsoft.PowerApps.PowerShell -AllowClobber
$pass = ConvertTo-SecureString "MY_SECRET_PASSWORD" -AsPlainText -Force
Add-PowerAppsAccount -Username [email protected] -Password $pass
foreach($flow in Get-AdminFlow)
{
? ? foreach($runHistory in Get-FlowRun -FlowName $flow.FlowName)
? ? {
? ? ? ? if($runHistory.Status -eq "Failed")?
? ? ? ? {
? ? ? ? ? ? """" + $flow.DisplayName + """"+ " failed at " + $runHistory.StartTime + " eventID: " + $runHistory.FlowRunName
? ? ? ? }
? ? }
}
This PowerShell script uses the Get-FlowRun cmdlet to get the run history of each flow in my account and prints out information about flows that failed.
You get the display name of the flow, when it started and the ID of the run, which can be used as the unique ID of the failure event. Additional information can also be obtained through the same process.
Ok but I promised it will work automatically, so let's look at the next part.
Running the script automatically
We need this script to periodically run somewhere and that somewhere will be a dedicated server in your basement... just kidding, it's not the 90s anymore, it will run in Azure ;)
There are a few ways to run PowerShell scripts in Azure, in this case I went with an Automation account:
Once you create the account, you'll need to import the modules needed to communicate with Power Automate. On the desktop you can just run the Install-Module commands but in the cloud you can't. Instead, you need to go to Modules and click on "Browse Gallery".
Search for "PowerApps", you'll need to install Microsoft.PowerApps.Administration.PowerShell and Microsoft.PowerApps.PowerShell
Now go to Runbooks and click on "Create a runbook", configure as PowerShell version 5.1.
In the runbook, click on Edit and write in the following script:
$pass?=?ConvertTo-SecureString?"[MY SECRET PASSWORD]"?-AsPlainText?-Forc
Add-PowerAppsAccount?-Username?[ACCOUNT EMAIL] -Password?$pass
$url?=?[FLOW URL]
foreach($flow?in?Get-AdminFlow)
{
????foreach($runHistory?in?Get-FlowRun?-FlowName?$flow.FlowName)
????{
????????if($runHistory.Status?-eq?"Failed")?
????????{
????????????$body?=?""""?+?$flow.DisplayName?+?""""+?"?failed?at?"?+?$runHistory.StartTime?+?"?eventID:?"?+?$runHistory.FlowRunName?
????????????Invoke-RestMethod?-Method?'Post'?-Uri?$url?-Body?$body
????????}
????}
}
Click Save and then Publish.
You'll notice this script is somewhat different from the one we started with. The Install-Module commands were removed because we already did that and a $url variable was added, along with a line that executes a POST to this URL.
We'll get back to it a little bit later, meanwhile let's complete the work on the Runbook.
The last thing we need to do is set a schedule so that this script runs periodically. To do that, go to Schedules > Add a schedule > Link a schedule to your runbook > Add a schedule
This is one way to invoke the runbook. Another option is creating a Webhook so you can run it on demand and integrate it into your other processes.
Getting the notification
Alright, so this is the last part. The URL we added in the script and the Invoke-RestMethod command are meant to call a Power Automate flow that will respond to every failure event.
The notification flow needs to be an Instant Flow, triggered on "When a HTTP request is received". Copy the generated URL and update the script in Azure.
In this example I'm sending an email to myself with information about the failure but as I mentioned in the beginning, you can build any process you want to handle this event, like creating a ticket in your IT system or notifying the developer on call.
This solution is not necessarily production ready as is, you'd need to handle credentials more securely but it gives you an idea on how some monitoring tasks can be automated with this approach.
Microsoft Power Platform Solution Architect | Business Applications Consultant | Scrum Coach
1 年Dear Ilya Fainberg. Thanks for the great post. I am trying to set up something similar. The only problem is, that usually the user that needs to run the PowerShell has MFA activated. Do you have an idea, how I can get authenticated?
Paolo Boccagni
Senior cloud | Digital Transformation | Cloud Solutions | Integration| Ex -Oracle Db Consultant | Ex-Oracle Apex Consulant | Ex-Java /J2EE
2 年Murat Muhammet Ademo?lu Victor Vidamo sounds cool even though we are using log analytics workspace.
Power Platform | Pro / Low / No Code / Integrations Developer | Dataverse | D365 CE | Data Enthusiast | XRMToolBox Plugin Author | Technical consultant / architect | CI/CD ALM Designer / Developer | Azure Integrations
2 年Hi Ilya, I made use of this solution, however by making a little change the use of Get-FlowRun cmdlet i.e, also pass environment guid in parameter So it works as below: Get-FlowRun -FlowName?[GUID] -EnvironmentName [GUID]
Power Platform | Pro / Low / No Code / Integrations Developer | Dataverse | D365 CE | Data Enthusiast | XRMToolBox Plugin Author | Technical consultant / architect | CI/CD ALM Designer / Developer | Azure Integrations
2 年Hi Ilya, i needed this much however while trying the powershell it does not retrieve any run history ... i tried searching in Docs for help about Get-FlowRun but i do not find anything. Do you have a reference for that ?