Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
15 May 2020 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:

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:

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here