Skip to content

Commit

Permalink
Updates to use dynamic pagination (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregslack78 authored Feb 2, 2025
1 parent 6fd76db commit 8d65d84
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
4 changes: 3 additions & 1 deletion Scripts/Helpers/Get-AzPolicyOrSetDefinitions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ function Get-AzPolicyOrSetDefinitions {
$query = "PolicyResources | where type == 'microsoft.authorization/policydefinitions'"
$progressItemName = "Policy definitions"
$excludedIds = $desiredState.excludedPolicyDefinitions
$progressIncrement = 1000
}
policySetDefinitions {
$query = "PolicyResources | where type == 'microsoft.authorization/policysetdefinitions'"
$progressItemName = "Policy Set definitions"
$excludedIds = $desiredState.excludedPolicySetDefinitions
$progressIncrement = 250
}
}

$policyResources = Search-AzGraphAllItems -Query $query -ProgressItemName $progressItemName
$policyResources = Search-AzGraphAllItems -Query $query -ProgressItemName $progressItemName -ProgressIncrement $progressIncrement
foreach ($policyResource in $policyResources) {
$resourceTenantId = $policyResource.tenantId
if ($resourceTenantId -in @($null, "", $environmentTenantId)) {
Expand Down
45 changes: 37 additions & 8 deletions Scripts/Helpers/Search-AzGraphAllItems.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ function Search-AzGraphAllItems {
)

# Search-AzGraph can only return a maximum of 1000 items. Without the -First it will only return 100 items
$body = @{
query = $Query
# options = @{
# "`$top" = 1000
# "`$skip" = 0
# }
if ($ProgressItemName -ne "Policy Set definitions") {
$body = @{
query = $Query
}
}
else {
$body = @{
query = $Query
options = @{
"`$top" = $ProgressIncrement
"`$skip" = 0
}
}
}
if ($ScopeSplat.ManagementGroup) {
$body.managementGroups = @($ScopeSplat.ManagementGroup)
Expand All @@ -29,13 +36,35 @@ function Search-AzGraphAllItems {

[System.Collections.ArrayList] $data = [System.Collections.ArrayList]::new()

$bodyJson = $body | ConvertTo-Json -Depth 100
$dsi = 1
$dsi = 0

do {
try {
$bodyJson = $body | ConvertTo-Json -Depth 100

$response = Invoke-AzRestMethod -Method POST `
-Path "/providers/Microsoft.ResourceGraph/resources?api-version=2022-10-01" `
-Payload $bodyJson
$dsi++

if ($response.StatusCode -ge 400) {
$contentString = $response.Content -join ""
$contentObj = ConvertFrom-Json -InputObject $contentString
if ($contentObj.error.details[0].code -eq "ResponsePayloadTooLarge") {
Write-Host "Payload too large detected. Adjusting request and retrying..."
$ProgressIncrement = [Convert]::ToInt32([math]::Floor($ProgressIncrement / 2))
# Modify body to include pagination options
$body = @{
query = $Query
options = @{
"`$top" = $ProgressIncrement
"`$skip" = 0
}
}
# Retry immediately without incrementing $dsi
continue
}
}
}
catch {
Write-Warning "Recovering Data Stream Error: $_"
Expand Down

0 comments on commit 8d65d84

Please sign in to comment.