powershell script is not working as expect

2025-02-07T07:19:29.9166667+00:00

I wanted to cancel all running or pending runs in a Logic App Standard workflow(from a custome days)
But I think the below script is not working as expected.

Variables $ResourceGroupName = "ResourceGroupName " $LogicAppName = "logic-app-standard" $WorkflowName = "workflow1" $SubscriptionId = "SubscriptionId"

 

Timeframe: Last 24 hours $StartTime = (Get-Date).AddHours(-24).ToString("o") $EndTime = (Get-Date).ToString("o")

 

Get Access Token for REST API Calls $AccessToken = (Get-AzAccessToken -ResourceUrl https://management.azure.com).Token

 

List Runs within the last 24 hours $RunsUrl = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Logic/workflows/$WorkflowName/runs?api-version=2019-05-01&$filter=startTime ge $StartTime and startTime le $EndTime" $Runs = Invoke-RestMethod -Method Get -Uri $RunsUrl -Headers @{ Authorization = "Bearer $AccessToken" }

 

Filter Runs for "Running" or "Waiting" status $RunningRuns = $Runs.value | Where-Object { $.status -eq "Running" -or $.status -eq "Waiting" }

 

Cancel Each Run foreach ($Run in $RunningRuns) {     $RunId = $Run.name     $CancelUrl = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Logic/workflows/$WorkflowName/runs/$RunId/cancel?api-version=2019-05-01"     Write-Host "Cancelling Run ID: $RunId"     Invoke-RestMethod -Method Post -Uri $CancelUrl -Headers @{ Authorization = "Bearer $AccessToken" } }

 

Write-Host "All eligible runs have been cancelled."

  

$Runs.value | ForEach-Object { Write-Host "Run ID: $($.name), Status: $($.status)" }

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,621 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,796 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Khantwal, Pradeep (ext) (IT APS EIS) 0 Reputation points
    2025-02-07T07:21:19.0266667+00:00

    Variables $ResourceGroupName = "ResourceGroupName " $LogicAppName = "logic-app-standard" $WorkflowName = "workflow1" $SubscriptionId = "SubscriptionId"

    Timeframe: Last 24 hours $StartTime = (Get-Date).AddHours(-24).ToString("o") $EndTime = (Get-Date).ToString("o")

    Get Access Token for REST API Calls $AccessToken = (Get-AzAccessToken -ResourceUrl https://management.azure.com).Token

    List Runs within the last 24 hours $RunsUrl = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Logic/workflows/$WorkflowName/runs?api-version=2019-05-01&$filter=startTime ge $StartTime and startTime le $EndTime" $Runs = Invoke-RestMethod -Method Get -Uri $RunsUrl -Headers @{ Authorization = "Bearer $AccessToken" }

    Filter Runs for "Running" or "Waiting" status $RunningRuns = $Runs.value | Where-Object { $.status -eq "Running" -or $.status -eq "Waiting" }

    Cancel Each Run foreach ($Run in $RunningRuns) { $RunId = $Run.name $CancelUrl = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Logic/workflows/$WorkflowName/runs/$RunId/cancel?api-version=2019-05-01" Write-Host "Cancelling Run ID: $RunId" Invoke-RestMethod -Method Post -Uri $CancelUrl -Headers @{ Authorization = "Bearer $AccessToken" } }

    Write-Host "All eligible runs have been cancelled."

    $Runs.value | ForEach-Object { Write-Host "Run ID: $($.name), Status: $($.status)" }

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.