删除 Azure 中的孤立角色分配

问题描述 投票:0回答:2

用户已被删除的角色分配仍为

Identity not found

az role assignment list
不会返回displayName来以这种方式过滤掉它。

例如:

    "canDelegate": null,
    "condition": null,
    "conditionVersion": null,
    "description": "",
    "id": "/subscriptions/xxxxxxxxxxxxx/providers/Microsoft.Authorization/roleAssignments/xxxxxxxxxxxxx",
    "name": "xxxxxxxxxxxxx",
    "principalId": "xxxxxxxxxxxxx",
    "principalType": "ServicePrincipal",
    "roleDefinitionId": "/subscriptions/xxxxxxxxxxxxx/providers/Microsoft.Authorization/roleDefinitions/xxxxxxxxxxxxx",
    "roleDefinitionName": "User Access Administrator",
    "scope": "/subscriptions/xxxxxxxxxxxxx",
    "type": "Microsoft.Authorization/roleAssignments"

我们是否有任何简单的方法可以使用 az cli 找到这些并删除它们?这样就可以将其放入脚本中。

azure azure-cli
2个回答
0
投票

我们是否有任何简单的方法可以使用 az cli 找到这些并删除它们

使用azure cli,我找不到任何方法来获取孤立角色,但我可以找到一种替代方案,即Uinsg PowerShell,如下所示,我遵循了Microsoft-DocumentSO-Thread

Get-AzRoleAssignment | Where-object -Property Displayname -eq $null

输出:

enter image description here

您还可以使用以下命令获得孤立角色,我遵循了

$o = "Unknown"
Get-AzRoleAssignment | Where-object -Property ObjectType -eq $o

输出:

enter image description here

现在您可以使用

Remove-AzRoleAssignment
删除这些角色 MS-Doc


0
投票

这是我使用的,在我的用例中似乎可以正常工作:

function Remove-Orphan-Role-Assignments {
    $orphans = az role assignment list --query "[?principalName=='']" --all | ConvertFrom-Json 
    $orphanCount = $orphans.Length
    Write-Host "Orphans found: $($orphanCount)"
    if ( $orphanCount -eq 0 ) { 
        return;
    }

$reply = Read-Host -Prompt "Delete orphans? [y/n]"
if ( $reply -eq 'y' ) { 
    foreach ($orphan in $orphans)
        {
            Write-Host "roleDefinitionId: $($orphan.roleDefinitionId)"
            az role assignment delete --role $orphan.roleDefinitionId
        }
        Write-Host "$($orphanCount) deleted."
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.