Resubmit your failed Power Automate flow runs automatically using M365 CLI and REST API

Have you ever been forced to resubmit lot of failed Power Automate flow runs manually, if so this blog post will help you to automatically resubmit the flow runs using

  • CLI for Microsoft 365
  • Power Automate REST API
  • Power Automate Management connector

CLI for Microsoft 365:

Microsoft 365 CLI helps you manage configuration settings of Microsoft 365 tenant and its various services like SharePoint, Power Automate, Power Apps, Microsoft Graph etc and to build automation scripts on any platform.

Getting started: The CLI for Microsoft 365 is available and distributed as an NPM package. To use it, install it globally using:

npm i -g @pnp/cli-microsoft365

To install the beta version

npm i -g @pnp/cli-microsoft365@next

To update to the latest stable version

@pnp/cli-microsoft365@latest

Next, login to Microsoft 365 CLI using the following command.

m365 login

You will be presented with a code and a login URL https://microsoft.com/devicelogin, navigate to the URL and enter the code > Sign-in using the Microsoft 365 work account. The above command uses device code flow to authenticate and authorize the user through an Azure Active directory app PnP Management Shell. If you are accessing M365 CLI for the first time, you may have to consent for permissions. After the sign-in process is completed, you can enter various commands available within Microsoft 365 CLI.

Let us start with a basic command to list all Power Automate environments in your Tenant

m365 flow environment list

You can try the various cmdlets available as shown in the below screenshot with in Microsoft 365 CLI from the following url

https://pnp.github.io/cli-microsoft365/cmd/flow/flow-list/

CLI cmdlet to List all Flow Runs:

Replace the flowEnvironmentID & flowGUID pertaining to yours

m365 flow run list --environment flowEnvironmentID --flow flowGUID --output json

The above command lists all runs details. It provides information like status (Failed, Successful), run ID, run start time etc

CLI cmdlet to Resubmit a Flow Run:

Replace the flowEnvironmentID, flowGUID & flowRunID

m365 flow run resubmit --environment flowEnvironmentID --flow flowGUID --name flowRunID –confirm

There are cmdlets which accepts JMESPath to query. You can run the M365 CLI commands stored in a file like PowerShell cmdlets. Find below the M365 CLI cmdlets stored in a PowerShell file (.ps1) to resubmit the failed flows automatically.

$flowEnvironment=$args[0]
$flowGUID=$args[1]
$flowRuns = m365 flow run list --environment $flowEnvironment --flow $flowGUID --output json | ConvertFrom-Json
foreach ($run in $flowRuns) 
{
    if($run.status -eq "Failed")
	{
		Write-Output "Run details: " $run
		#Resubmit all the failed flows
		m365 flow run resubmit --environment $flowEnvironment --flow $flowGUID --name $run.name --confirm
		Write-Output "Run resubmitted successfully"			
	}
}

The above script stored in a file can be executed as shown below by passing the Flow Environment ID and the Flow ID in the command line

You can modify the script to Resubmit flow run according to your requirement, for e.g. within a certain date range since there is information on the run start date. There are sample scripts available in the github repo for M365 CLI submitted by community members

https://pnp.github.io/cli-microsoft365/sample-scripts/

Power Automate REST API:

There are Power Automate REST API endpoints to list the Flow Runs and to re-submit a run. Go through the following blog post for more information on how access the Power Automate REST API endpoints

Everything to know about Power Automate REST API to manage your flows

The above-mentioned blogpost helps you to call the following Power Automate REST APIs from a custom connector and programmatically from other applications.

API Endpoint to list flow runs:

GET https://api.flow.microsoft.com/providers/Microsoft.ProcessSimple/environments/{FlowEnvironment}/flows/{FlowGUID}/runs?api-version=2016-11-01

Endpoint to Resubmit a flow run:

POST https://api.flow.microsoft.com/providers/Microsoft.ProcessSimple/environments/{FlowEnvironment}/flows/{FlowGUID}/triggers/manual/histories/{FlowRunID}/resubmit?api-version=2016-11-01

Power Automate Management Connector:

There is also an action to Resubmit flow run from the Power Automate management connector. Find the action below to resubmit a flow run with the details filled in. The environment and the flow value has to be selected from the dropdown.

For the trigger name, you can get the exact name from the flow definition file or using the expression trigger() added to the flow on a compose action.

To get the flow definition file go to the flow and export it as a Package.zip

Open the Zip package, go to the path Microsoft.Flow\flows\{flowGUID} and then open the file definition.json. Search for the keyword triggers, you can find the name of the trigger

In a Power Automate flow, you can get the flow run details using the following expression.

workflow()

Find below test result of a flow run using the expression workflow() on the compose action which has the runid and other details of the flow run.

With these possibilities you can automatically resubmit a failed flow run (time out, failure due to config change etc) if the details of the failed flows are logged somewhere.

There is also PowerShell support for Power Platform, do look at the following documentation to get to know the list of available cmdlets:

https://docs.microsoft.com/en-us/power-platform/admin/powerapps-powershell#cmdlet-list—maker-cmdlets

Cmdlet Get-FlowRun, gets all the flow runs of a particular flow.

Summary: I would recommend getting familiar with Microsoft 365 CLI which has various cmdlets to make your job easier. The syntax of all commands is well documented with examples. Hope you have found this informational & thanks for reading. If you are visiting my blog for the first time, please do look at my other blogposts.

2 thoughts on “Resubmit your failed Power Automate flow runs automatically using M365 CLI and REST API

  1. Hi there thanks for your great articles. I have been successful in using CLI for Microsoft 365 in Command Line in Windows but after installing in PowerShell for Windows I cannot run any M365 commands without an error. I have searched everywhere but nothing. I want to run your .ps1 scripts to manage a large Power Automate base. Please advise if you have the time. Thanks

    Like

  2. Thank you for the article – really helpful and straight forward.

    How do I add a date range for resubmitting Flow runs in my .ps1 file?

    Like

Leave a comment