Click here to Skip to main content
15,881,139 members
Articles / Hosted Services / Azure
Tip/Trick

How to Start and Monitor Long-Running Azure Logic App Workflows with Powershell

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
15 May 2020CPOL 4.3K   1  
Kicking off and monitoring long-running Azure logic app workflows with Powershell
The Powershell code given in this tip provides the ability to kick-off an Azure Logic App workflow, receive the response, parse the response for the polling URL, then periodically check the polling URL for the result of the workflow (success/fail).

Using the Code

The Powershell script can kick off a long-running Azure Logic App workflow and continually monitor the process until the workflow:

  1. finishes successfully
  2. fails
  3. exceeds the max run time set on the command-line

How to call the PS1 on the command-line:

C++
powershell "C:\a\powershell_azure_logic_app_monitor.ps1" 
-url "'https://prod-29.centralus.logic.azure.com:443/workflows/vvwwxxyyzz'" -maxruntime 20	

The code is as given below:

PowerShell
Param(
  [string]$url,
  [decimal]$maxruntime
)
$JSONBody = ''
echo "---"
echo "1. START THE WORKFLOW."
$execRecord = Invoke-WebRequest -Method POST -Uri $url 
-Headers @{"Accept" = "application/json"} -ContentType "application/json" 
-Body $JSONBody -UseBasicParsing

$rawcontent = $execRecord.RawContent
echo "---"
echo "2. CAPTURE RAW CONTENT RESPONSE WHEN WORKFLOW IS STARTED." + $rawcontent

$rawarray = $rawcontent.split("`r`n")
echo "---"
echo "3. PARSE RESPONSE AND EXTRACT THE POLLING URL."
foreach ($element in $rawarray) 
{
	if ($element.Length -gt 13)
	{
		if ($element.Substring(0,14) -eq 'Location: http')
		{
			echo "line:"$element
			$MonitorURL = $element
			$MonitorURL = $MonitorURL -replace 'Location: ',''
		}
	}
}
if ($MonitorURL.Length -lt 1)
{
	echo "NO POLLING URL FOUND."
	exit 44;
}

echo "---"
echo "4. MONITOR WORKFLOW STATUS VIA POLLING URL."
echo $MonitorURL

$timeout = new-timespan -Minutes $maxruntime
$sw = [diagnostics.stopwatch]::StartNew()
while ($sw.elapsed -lt $timeout){
	$execRecord = Invoke-Restmethod -Method GET -Uri $MonitorURL 
    -Headers @{"Accept" = "application/json"} -ContentType "application/json"
	echo "POLLING RESULT: " + $execRecord
	if($execRecord -eq 'success')
	{
		exit 0;
	}
	if($execRecord -eq 'failure')
	{
		exit 22;
	}
	start-sleep -seconds 10
}
echo "MAX RUNTIME EXCEEDED."
exit 33

History

  • 15th May, 2020: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Oproot Research
United States United States
+ Oproot Research
Learn all that is learnable.

Comments and Discussions

 
-- There are no messages in this forum --