Skip to content

Commit 8c32d3f

Browse files
committed
Added filter to GetConfigurationsForPimGroups to only process groups assigned to Scuba roles. Removed phantom PIM groups from processing. Added URI to Write-ScubaLog message when throttled to track which endpoint triggered the logic.
1 parent 285cc2d commit 8c32d3f

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

PowerShell/ScubaGear/Modules/Providers/ExportAADProvider.psm1

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,14 @@ function GetConfigurationsForPimGroups{
610610

611611
# Get all groups enrolled in PIM for Groups management in the tenant. This only returns the ObjectID of the PIM Group as ID.
612612
# This will retrieve information from the Graph API directly and not use the cmdlet. API information is contained within the Permissions JSON file.
613-
$PIMGroups = (Invoke-GraphDirectly -Commandlet "Get-MgBetaIdentityGovernancePrivilegedAccessGroup" -M365Environment $M365Environment).Value
613+
$AllPIMGroups = (Invoke-GraphDirectly -Commandlet "Get-MgBetaIdentityGovernancePrivilegedAccessGroup" -M365Environment $M365Environment).Value
614+
if ($null -eq $AllPIMGroups -or $AllPIMGroups.Count -eq 0) {
615+
return
616+
}
614617

615-
if ($null -eq $PIMGroups -or $PIMGroups.Count -eq 0) {
618+
# Filter to retain only the PIM groups assigned to Scuba privileged roles
619+
$PIMGroups = @($AllPIMGroups | Where-Object { $_.Id -in $AllRoleAssignments.PrincipalId })
620+
if ($PIMGroups.Count -eq 0) {
616621
return
617622
}
618623

@@ -621,15 +626,24 @@ function GetConfigurationsForPimGroups{
621626
-UrlScript { "/groups/$($_.Id)?`$select=displayName" } `
622627
-M365Environment $M365Environment -ApiVersion "beta"
623628

629+
# Write a note to the log about PIM groups that no longer exist in the Entra directory
630+
$PhantomPIMGroups = $PIMGroups | Where-Object { $resp = $GroupDisplayNameResults[$_.Id]; $resp.status -eq 404; }
631+
$PhantomPIMGroups | ForEach-Object { Write-ScubaLog -Message "Skipping phantom PIM group: $($_.Id)" -Level Info -Source "GetConfigurationsForPimGroups" }
632+
633+
# Filter out phantom groups from $PIMGroups
634+
$PIMGroups = @($PIMGroups | Where-Object { $PhantomPIMGroups.Id -notcontains $_.Id })
635+
624636
# Add display names to the PIM group objects for easier access later
625637
foreach ($Group in $PIMGroups) {
626638
$displayNameResponse = $GroupDisplayNameResults[$Group.Id]
627639
if ($displayNameResponse.status -eq 200) {
628640
$Group | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $displayNameResponse.body.displayName
629641
}
630-
# If there were any errors batch fetching the PIM group names, abort execution with the details of the error.
642+
# If there was an errors fetching the group name, write to log and use the group ID instead of the name.
631643
else {
632-
throw "Failed to fetch display name for group $($Group.Id) from batch results. Status: $($displayNameResponse.status). Body: $(($displayNameResponse.body | ConvertTo-Json -Depth 10))"
644+
$GroupNameFetchError = "Failed to fetch display name for group $($Group.Id) from batch results. Status: $($displayNameResponse.status). Body: $(($displayNameResponse.body | ConvertTo-Json -Depth 10))"
645+
Write-ScubaLog -Message $GroupNameFetchError -Level Info -Source "GetConfigurationsForPimGroups"
646+
$Group | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value "$($Group.Id)"
633647
}
634648
}
635649

PowerShell/ScubaGear/Modules/Utility/Utility.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,9 @@ function Invoke-GraphBatchRequest {
598598
$requestCountForThisBatch = $pendingRequests.Count
599599
}
600600
if ($attempt -eq 0) {
601-
Write-ScubaLog -Message "Batch request throttled (HTTP 429). Retrying in $waitSeconds second(s)." -Level Info -Source "Invoke-GraphBatchRequest"
601+
Write-ScubaLog -Message "Batch size: $($pendingRequests.Count) Request: $UrlScript - Batch request throttled (HTTP 429). Retrying in $waitSeconds second(s)." -Level Info -Source "Invoke-GraphBatchRequest"
602602
} else {
603-
Write-ScubaLog -Message "Batch request throttled (HTTP 429). Retrying in $waitSeconds second(s) (attempt $($attempt + 1), Retry-After=$retryAfter)." -Level Info -Source "Invoke-GraphBatchRequest"
603+
Write-ScubaLog -Message "Batch size: $($pendingRequests.Count) Request: $UrlScript - Batch request throttled (HTTP 429). Retrying in $waitSeconds second(s) (attempt $($attempt + 1), Retry-After=$retryAfter)." -Level Info -Source "Invoke-GraphBatchRequest"
604604
}
605605
Start-Sleep -Seconds $waitSeconds
606606
$attempt++
@@ -656,9 +656,9 @@ function Invoke-GraphBatchRequest {
656656
$waitSeconds = [int][Math]::Min($longestRetryAfterSeconds * [Math]::Pow(2, $attempt), 300)
657657

658658
if ($attempt -eq 0) {
659-
Write-ScubaLog -Message "Batch request: $($throttled.Count) sub-request(s) throttled (HTTP 429). Retrying in $waitSeconds second(s)." -Level Info -Source "Invoke-GraphBatchRequest"
659+
Write-ScubaLog -Message "Batch size: $($pendingRequests.Count) Request: $UrlScript - $($throttled.Count) sub-request(s) throttled (HTTP 429). Retrying in $waitSeconds second(s)." -Level Info -Source "Invoke-GraphBatchRequest"
660660
} else {
661-
Write-ScubaLog -Message "Batch request: $($throttled.Count) sub-request(s) throttled (HTTP 429). Retrying in $waitSeconds second(s) (attempt $($attempt + 1), Retry-After=$longestRetryAfterSeconds)." -Level Info -Source "Invoke-GraphBatchRequest"
661+
Write-ScubaLog -Message "Batch size: $($pendingRequests.Count) Request: $UrlScript - $($throttled.Count) sub-request(s) throttled (HTTP 429). Retrying in $waitSeconds second(s) (attempt $($attempt + 1), Retry-After=$longestRetryAfterSeconds)." -Level Info -Source "Invoke-GraphBatchRequest"
662662
}
663663
# Cut the batch size in half so later batches in this call are smaller.
664664
# We don't trim $pendingRequests here because it already only holds the throttled

0 commit comments

Comments
 (0)