【问题标题】:Recursively Delete Azure Management Groups (Nested Lists inside PSObject)递归删除 Azure 管理组(PSObject 内的嵌套列表)
【发布时间】:2020-10-29 18:14:15
【问题描述】:

我正在尝试编写一个递归删除 Azure 管理组的脚本。 Azure Management Groups 最多可以有 6 个层次结构。除非先删除所有子级,否则无法删除顶级管理组。

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

返回一个“PSManagementGroup”对象。如果它有子管理组,您可以像这样访问它们:

$toplevelgroup.Children

返回如下内容:

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

注意 Children : 3 实际上是因为下一级向下管理组称为 '3'。

我的脚本需要

  1. 根据传递的管理组名称参数获取顶级管理组对象。
  2. 测试有多少级别的子管理组
  3. 首先循环通过最低级别的管理组并将其删除,然后转到下一个父级别并删除它们并重复,直到顶级组也被删除。

我最初写的是愚蠢的,只有在有 3 级管理组时才有效,所以我正在寻找一些指针来帮助使这个脚本更健壮。

# 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

文档:

Get-AzManagementGroup Remove-AzManagementGroup

【问题讨论】:

    标签: azure powershell


    【解决方案1】:

    如果您想根据上述逻辑逐级删除组(从底部到顶部)。你可以使用下面的 sn-p。

    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)
    }
    }
    
    }
    

    您可以多次迭代 - 直到根组没有子元素。

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

    或者,
    如果您的目标是删除顶层的整个组。您可以编写一个递归调用的函数,直到根被删除。

    功能逻辑如下:

    1. 获取群组
    2. 查找是否有孩子
    3. 如果是,请从第 1 步开始对小组中的每个孩子进行操作。
    4. 删除组。

    分享一下基于上述逻辑的函数的sn-p。

    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
    }
    

    你可以像下面这样调用函数:

    remove-recursively -name <RootGroupName>
    

    输出片段:

    逻辑 1: 逻辑2:

    建议在开发环境中测试脚本。

    【讨论】:

    • @Liam - 我想快速加入并与您核实上述代码是否满足您的要求或我遗漏了什么?
    猜你喜欢
    • 2022-10-21
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 2019-05-27
    • 2021-04-24
    • 1970-01-01
    • 2021-09-19
    相关资源
    最近更新 更多