You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Write-ToConsoleLog"Checking for custom role definitions on management group: $ManagementGroupId ($ManagementGroupDisplayName)"-NoNewLine
544
+
545
+
# Get all custom role definitions scoped to this management group
546
+
$customRoleDefinitions= (az role definition list --custom-role-only true --scope "/providers/Microsoft.Management/managementGroups/$ManagementGroupId"--query "[].{name:name,roleName:roleName,id:id,assignableScopes:assignableScopes}"-o json) |ConvertFrom-Json
if (-not$customRoleDefinitions-or$customRoleDefinitions.Count-eq0) {
553
+
Write-ToConsoleLog"No custom role definitions found on management group: $ManagementGroupId ($ManagementGroupDisplayName), skipping."-NoNewLine
554
+
return
555
+
}
556
+
557
+
Write-ToConsoleLog"Found $($customRoleDefinitions.Count) custom role definition(s) on management group: $ManagementGroupId ($ManagementGroupDisplayName)"-NoNewLine
558
+
559
+
# For each custom role definition, find and delete all assignments first
Write-ToConsoleLog"Processing custom role definition: $($roleDefinition.roleName) (ID: $($roleDefinition.name))"-NoNewLine
562
+
563
+
# Find all role assignments for this custom role on the management group
564
+
Write-ToConsoleLog"Checking for role assignments of custom role '$($roleDefinition.roleName)' on management group: $ManagementGroupId ($ManagementGroupDisplayName)"-NoNewLine
565
+
$mgRoleAssignments= (az role assignment list --role $roleDefinition.roleName--scope "/providers/Microsoft.Management/managementGroups/$ManagementGroupId"--query "[].{id:id,principalName:principalName,principalId:principalId}"-o json) |ConvertFrom-Json
566
+
567
+
if ($mgRoleAssignments-and$mgRoleAssignments.Count-gt0) {
568
+
Write-ToConsoleLog"Found $($mgRoleAssignments.Count) role assignment(s) of custom role '$($roleDefinition.roleName)' on management group: $ManagementGroupId ($ManagementGroupDisplayName)"-NoNewLine
Write-ToConsoleLog"Deleting role assignment of custom role '$roleDefinitionName' for principal: $($assignment.principalName) ($($assignment.principalId)) from management group: $managementGroupId ($managementGroupDisplayName)"-NoNewLine
579
+
580
+
if($using:PlanMode) {
581
+
Write-ToConsoleLog"(Plan Mode) Would run: az role assignment delete --ids $($assignment.id)"-NoNewLine -Color Gray
582
+
} else {
583
+
$result= az role assignment delete --ids $assignment.id2>&1
584
+
if (!$result) {
585
+
Write-ToConsoleLog"Deleted role assignment of custom role '$roleDefinitionName' from management group: $managementGroupId ($managementGroupDisplayName)"-NoNewLine
586
+
} else {
587
+
Write-ToConsoleLog"Failed to delete role assignment of custom role '$roleDefinitionName' from management group: $managementGroupId ($managementGroupDisplayName)"-IsWarning -NoNewLine
588
+
}
589
+
}
590
+
} -ThrottleLimit $using:ThrottleLimit
591
+
} else {
592
+
Write-ToConsoleLog"No role assignments found for custom role '$($roleDefinition.roleName)' on management group: $ManagementGroupId ($ManagementGroupDisplayName)"-NoNewLine
593
+
}
594
+
595
+
# Find all role assignments for this custom role on subscriptions under the management group
596
+
if ($Subscriptions-and$Subscriptions.Count-gt0) {
597
+
Write-ToConsoleLog"Checking for role assignments of custom role '$($roleDefinition.roleName)' on subscriptions under management group: $ManagementGroupId ($ManagementGroupDisplayName)"-NoNewLine
Write-ToConsoleLog"Checking for role assignments of custom role '$($roleDefinition.roleName)' on subscription: $($subscription.Name) (ID: $($subscription.Id))"-NoNewLine
608
+
609
+
$subRoleAssignments= (az role assignment list --role $roleDefinition.roleName--subscription $subscription.Id--query "[].{id:id,principalName:principalName,principalId:principalId}"-o json) |ConvertFrom-Json
610
+
611
+
if ($subRoleAssignments-and$subRoleAssignments.Count-gt0) {
612
+
Write-ToConsoleLog"Found $($subRoleAssignments.Count) role assignment(s) of custom role '$($roleDefinition.roleName)' on subscription: $($subscription.Name) (ID: $($subscription.Id))"-NoNewLine
613
+
614
+
foreach ($assignmentin$subRoleAssignments) {
615
+
Write-ToConsoleLog"Deleting role assignment of custom role '$($roleDefinition.roleName)' for principal: $($assignment.principalName) ($($assignment.principalId)) from subscription: $($subscription.Name) (ID: $($subscription.Id))"-NoNewLine
616
+
617
+
if($using:PlanMode) {
618
+
Write-ToConsoleLog"(Plan Mode) Would run: az role assignment delete --ids $($assignment.id)"-NoNewLine -Color Gray
619
+
} else {
620
+
$result= az role assignment delete --ids $assignment.id2>&1
621
+
if (!$result) {
622
+
Write-ToConsoleLog"Deleted role assignment of custom role '$($roleDefinition.roleName)' from subscription: $($subscription.Name) (ID: $($subscription.Id))"-NoNewLine
623
+
} else {
624
+
Write-ToConsoleLog"Failed to delete role assignment of custom role '$($roleDefinition.roleName)' from subscription: $($subscription.Name) (ID: $($subscription.Id))"-IsWarning -NoNewLine
625
+
}
626
+
}
627
+
}
628
+
} else {
629
+
Write-ToConsoleLog"No role assignments found for custom role '$($roleDefinition.roleName)' on subscription: $($subscription.Name) (ID: $($subscription.Id))"-NoNewLine
630
+
}
631
+
} -ThrottleLimit $using:ThrottleLimit
632
+
}
633
+
634
+
# Now delete the custom role definition itself
635
+
Write-ToConsoleLog"Deleting custom role definition: $($roleDefinition.roleName) (ID: $($roleDefinition.name))"-NoNewLine
636
+
637
+
if($PlanMode) {
638
+
Write-ToConsoleLog"(Plan Mode) Would run: az role definition delete --name $($roleDefinition.name) --scope "/providers/Microsoft.Management/managementGroups/$ManagementGroupId""-NoNewLine -Color Gray
639
+
} else {
640
+
$result= az role definition delete --name $roleDefinition.name--scope "/providers/Microsoft.Management/managementGroups/$ManagementGroupId"2>&1
641
+
if (!$result) {
642
+
Write-ToConsoleLog"Deleted custom role definition: $($roleDefinition.roleName) (ID: $($roleDefinition.name))"-NoNewLine
643
+
} else {
644
+
Write-ToConsoleLog"Failed to delete custom role definition: $($roleDefinition.roleName) (ID: $($roleDefinition.name))"-IsWarning -NoNewLine
645
+
}
646
+
}
647
+
}
648
+
649
+
Write-ToConsoleLog"All custom role definitions processed for management group: $ManagementGroupId ($ManagementGroupDisplayName)"-NoNewLine
650
+
}
651
+
526
652
# Main execution starts here
527
653
if ($PSCmdlet.ShouldProcess("Delete Management Groups and Clean Subscriptions","delete")) {
528
654
@@ -531,6 +657,7 @@ function Remove-PlatformLandingZone {
0 commit comments