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:
- finishes successfully
- fails
- 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