Skip to content

Instantly share code, notes, and snippets.

@Tridy
Last active July 12, 2024 14:24
Show Gist options
  • Save Tridy/2f059122ab3854fa849969d1100c325d to your computer and use it in GitHub Desktop.
Save Tridy/2f059122ab3854fa849969d1100c325d to your computer and use it in GitHub Desktop.
In Azure API Management sets all of the Pending Developer Portal revisions statuses from Pending to Failed
$subscriptionId = "-GUID-"
$resourceGroupName = "-STRING-"
$apimName = "-STRING-"
$tokenResponse = az account get-access-token --resource https://management.azure.com | ConvertFrom-Json
$accessToken = $tokenResponse.accessToken
$baseUrl = "https://management.azure.com/subscriptions/${subscriptionId}/resourceGroups/${resourceGroupName}/providers/Microsoft.ApiManagement/service/${apimName}"
$url = "${baseUrl}/portalRevisions?api-version=2022-08-01"
$headers = @{
"Authorization" = "Bearer $accessToken"
}
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
function SetPendingRevisionsToFailed()
{
foreach ($item in $response.value)
{
if ($item.properties.Status -eq "pending")
{
# ======================================
# Allowed status changes
# ======================================
# Only Pending > Publishing
# Publishing > Completed
# Publishing > Failed
# ======================================
$portalRevisionId = $item.name
$patchUrl = $url = "${baseUrl}/portalRevisions/${portalRevisionId}?api-version=2022-08-01"
$headers = @{
"Authorization" = "Bearer $accessToken"
"Content-Type" = "application/json"
"If-Match" = "*"
}
$body = @{
properties = @{
status = "Publishing"
}
} | ConvertTo-Json
Invoke-RestMethod -Uri $patchUrl -Headers $headers -Method Patch -Body $body > $null
$body = @{
properties = @{
status = "Failed"
}
} | ConvertTo-Json
Invoke-RestMethod -Uri $patchUrl -Headers $headers -Method Patch -Body $body > $null
Write-Output "Done setting item $($item.name) to Failed status"
}
else
{
Write-Output "Skipping item $($item.name) with status: $($item.properties.Status)"
}
}
}
function DisplayRevisionStatuses()
{
foreach ($item in $response.value)
{
Write-Output "Revision: $($item.name) Status: $($item.properties.Status)"
}
}
DisplayRevisionStatuses
SetPendingRevisionsToFailed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment