WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 05b61a7

Browse files
committed
Fix role assignment check in cleanup script
1 parent 95903b3 commit 05b61a7

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

.github/tests/cleanup-scripts/cleanup_azure_resouces.ps1

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# This file can be used to clean up Resource Groups if there has been an issue with the End to End tests.
22
# CAUTION: Make sure you are connected to the correct subscription before running this script!
3+
4+
# Check for and install the resource-graph extension if not already installed
5+
$installedExtensions = az extension list --query "[].name" -o tsv
6+
if ($installedExtensions -notcontains "resource-graph") {
7+
Write-Host "Installing Azure CLI resource-graph extension..."
8+
az extension add --name resource-graph
9+
} else {
10+
Write-Host "Azure CLI resource-graph extension is already installed."
11+
}
12+
313
$managementGroupFilter = "alz-r"
414
if($managementGroupFilter -eq "")
515
{
@@ -70,13 +80,15 @@ $managementGroups | ForEach-Object -Parallel {
7080
} -ThrottleLimit 10
7181

7282
$roleDefinitionsFilter = $using:roleDefinitionsFilter
73-
$subscriptions = $using:subscriptions
83+
7484
$roleDefinitions = az role definition list --custom-role-only true --scope "/providers/Microsoft.Management/managementGroups/$managementGroup" --query "[].{name:name,roleName:roleName,id:id,assignableScopes:assignableScopes}" -o json | ConvertFrom-Json | Where-Object { $_.roleName -like "*$roleDefinitionsFilter*" -and $_.assignableScopes -contains "/providers/Microsoft.Management/managementGroups/$managementGroup" }
7585
$roleDefinitions | ForEach-Object -Parallel {
7686
$managementGroup = $using:managementGroup
7787
$roleDefinition = $_
7888

79-
$roleAssignments = az role assignment list --role $roleDefinition.name --scope "/providers/Microsoft.Management/managementGroups/$managementGroup" --query "[].{id:id,principalName:principalName,principalId:principalId}" -o json | ConvertFrom-Json
89+
Write-Host "$($roleDefinition.roleName) - $($managementGroup): Querying role assignments using Resource Graph for role definition $($roleDefinition.name)"
90+
$query = "authorizationresources | where type == 'microsoft.authorization/roleassignments' | where properties.roleDefinitionId == '/providers/Microsoft.Authorization/RoleDefinitions/$($roleDefinition.name)' | order by ['name'] asc"
91+
$roleAssignments = az graph query -q $query --query "data[].{id:id,principalId:properties.principalId}" -o json | ConvertFrom-Json
8092
$roleAssignments | ForEach-Object -Parallel {
8193
$managementGroup = $using:managementGroup
8294
$roleDefinition = $using:roleDefinition
@@ -85,19 +97,14 @@ $managementGroups | ForEach-Object -Parallel {
8597
az role assignment delete --ids $roleAssignment.id
8698
} -ThrottleLimit 10
8799

88-
foreach ($subscription in $using:subscriptions) {
89-
$subscriptionRoleAssignments = az role assignment list --role $roleDefinition.name --subscription $subscription --query "[].{id:id,principalName:principalName,principalId:principalId}" -o json | ConvertFrom-Json
90-
$subscriptionRoleAssignments | ForEach-Object -Parallel {
91-
$roleDefinition = $using:roleDefinition
92-
$subscription = $using:subscription
93-
$roleAssignment = $_
94-
Write-Host "Deleting role assignment: $($roleAssignment.id) for role definition: $($roleDefinition.roleName) in subscription: $subscription"
95-
az role assignment delete --ids $roleAssignment.id
96-
} -ThrottleLimit 10
97-
}
98-
99100
Write-Host "Deleting custom role definition: $($roleDefinition.roleName) in management group: $managementGroup"
100-
az role definition delete --name $roleDefinition.name --scope "/providers/Microsoft.Management/managementGroups/$managementGroup"
101+
$result = az role definition delete --name $roleDefinition.name --scope "/providers/Microsoft.Management/managementGroups/$managementGroup" 2>&1
102+
if($result -like "*ERROR*")
103+
{
104+
Write-Warning "Role definition $($roleDefinition.roleName) in management group: $managementGroup could not be deleted...$([Environment]::NewLine)$result"
105+
} else {
106+
Write-Host "Role definition $($roleDefinition.roleName) in management group: $managementGroup deleted successfully."
107+
}
101108

102109
} -ThrottleLimit 10
103110
} -ThrottleLimit 10

0 commit comments

Comments
 (0)