0

I'm trying to write a script which recursively deletes Azure Management Groups. Azure Management Groups can have up to 6 levels of hierarchy. You cannot delete the top level management group unless all the children have been deleted first.

$toplevelgroup = Get-AzManagementGroup -GroupName 'MyTopLevelGroup' -Expand -Recurse

Returns a 'PSManagementGroup' object. If it has child management groups you can access them like this:

$toplevelgroup.Children

which returns something like this:

Type        : /providers/Microsoft.Management/managementGroups
Id          : /providers/Microsoft.Management/managementGroups/2
Name        : 2
DisplayName : 2
Children    : 3

Note Children : 3 is actually because the next level down mgmt group is called '3'.

My script would need to

  1. Get the Top-Level Mgmt Group Object based on the parameter of the mgmt group name passed.
  2. Test how many levels of child mgmt groups there are
  3. Loop through the lowest level of management groups first and delete them, then move on to the next parent level and delete those and repeat until the top level group is also deleted.

What I originally wrote is dumb and only works if there are 3 level of management group so I'm looking for some pointer to help make this script more robust.

# Delete All Management Groups
$toplevelgroup = Get-AzManagementGroup -GroupName 'MyTopLevelGroup' -Expand -Recurse
$children = $toplevelgroup.Children
$grandchildren = $toplevelgroup.Children.Children
foreach ($grandchild in $grandchildren) {
    Remove-AzManagementGroup -GroupName $grandchild.Name
}
foreach ($child in $children){
    Remove-AzManagementGroup -GroupName $child.Name 
}
Remove-AzManagementGroup -GroupName $toplevelgroup.Name

Docs:

Get-AzManagementGroup Remove-AzManagementGroup

1 Answer 1

1

If you want to remove groups level after level(bottom most to top) based on the above logic. You could use the below snippet.

function remove-recursively($name)
{
#gets the parent item
$parent = Get-AzManagementGroup -GroupName $name -Expand -Recurse

#if the children is null
if($parent.Children -eq $null)
{

$temp = $parent.Name 
Write-Host "Removing management Group $temp" -ForegroundColor White
#removes the bottom most in the iteration.
Remove-AzManagementGroup -InputObject $parent
}
else
{
foreach($children in $parent.Children)
{
#recurse if it is not null
remove-recursively($children.Name)
}
}

}

You can iterate multiple times - until the root group has no child elements.

while((Get-AzManagementGroup -GroupName ("CEO") -Expand -Recurse).Children -ne $null)
{
remove-recursively -name CEO
}

Alternatively,
If your goal is delete the entire group at the top level. You could write a function that gets called recursively until the root gets deleted.

Function Logic is as follows :

  1. Get the group
  2. Find if there is any children
  3. If yes, Follow from step 1 for each children in group.
  4. Delete the group.

Sharing you the snippet of the function that is based on the above logic.

function remove-recursively($name)
{
#Enters the parent Level
Write-Host "Entering the scope with $name" -ForegroundColor Green
$parent = Get-AzManagementGroup -GroupName $name -Expand -Recurse

#Checks if there is any parent level.
if($parent.Children -ne $null)
{
Write-Host "Found the following Children :" -ForegroundColor White
Write-host ($parent.Children | select Name).Name -ForegroundColor Yellow
foreach($children in $parent.Children)
{
#tries to recurs to each child item
remove-recursively($children.Name)
}
}

#this below executes if all the child items are deleted or if doesn't have any child item
Write-Host "Removing the scope $name" -ForegroundColor Cyan
#Comment the below line if you just want to understand the flow
Remove-AzManagementGroup -InputObject $parent
}

You can invoke the function like below :

remove-recursively -name <RootGroupName>

Snippet of the Output :

Logic 1 : enter image description here Logic 2 : enter image description here

Recommend to test the scripts in the Dev environment.

1
  • @Liam -I wanted to quickly drop in and check with you whether the above code met your requirement or did i miss something ?
    – Satya V
    Aug 10, 2020 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.