找到“未找到的“身份”的角色分配的肯定方法。”与Powershell一起使用Azure?

发布于 2025-01-24 15:39:51 字数 306 浏览 1 评论 0 原文

如果您在Azure中分配角色,然后在删除角色分配之前删除身份,则会以“未找到身份”结束。健康)状况。它是无害的,但是它占据了角色分配插槽并缩小角色分配列表。我想找到并删除这些。

我认为这是这样的:

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

...将使我无关的身份而掌握这些角色,而且似乎有效,但是Azure Docs并没有真正保证这将涵盖所有案例。

我应该添加其他东西吗?还是有更好的方法可以找到这些角色分配?

If you assign a role in Azure and later delete the identity before you delete the role assignment, you wind up with an "Identity not found." condition. It's harmless, but it takes up a role assignment slot and clutters the role assignments list. I would like to find and delete these.

I am thinking that this:

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

...will get me those roles without an associated identity, and it seems to work, but the Azure docs don't really create much of an assurance that this will cover all cases.

Should I add something else to this? Or is there a better way of finding these roles assignments?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生漫 2025-01-31 15:39:51

事实证明,这种是间接识别这些孤立角色分配的方法。但是,除非您有许可获得所有这些角色,否则您将无法删除这些孤立的角色分配。

Turns out this is the way to identify these orphaned role assignments, albeit indirectly. However, you will not be able to remove these orphaned role assignments unless you have the permission to get to all of them.

笑饮青盏花 2025-01-31 15:39:51

可以对此进行修改以完成所有孤立的角色分配,无论它是哪个角色,但这是我投入一些参数的管道中的大部分工作。

我使用作为起点。我选择不迭代订阅(这是我的管道中的参数)。

在管道YAML中使用PowerShell任务:

- task: AzurePowerShell@5
inputs:
  azureSubscription: ${{ variables.azureServiceConnection }}
  azurePowerShellVersion: LatestVersion
  pwsh: true
  ScriptType: 'InlineScript'
  Inline: |
    
    Write-Output "Checking for orphaned assignments for role '${{ parameters.roleName }}'"
    $orphanedRoleAssignments = Get-AzRoleAssignment | Where-object -Property Displayname -eq $null
    
    if ($orphanedRoleAssignments.Count -eq 0) {
      Write-Output "No orphaned role assignments found. Exiting."
      exit 0
    }
    Write-Output "Total number of orphaned role assignments: $($orphanedRoleAssignments.Count)"
    
    $orphanedRoleAssignments = $orphanedRoleAssignments | Where-Object -Property RoleDefinitionName -eq "${{ parameters.roleName }}"
    
    if ($orphanedRoleAssignments.Count -eq 0) {
      Write-Output "No orphaned role assignments for the role '${{ parameters.roleName }}' found. Exiting."
      exit 0
    }
    Write-Output "Number of orphaned role assignments for role '${{ parameters.roleName }}': $($orphanedRoleAssignments.Count)"
    
    $orphanCounter = 0
    foreach ($assignment in $orphanedRoleAssignments) {
      $orphanCounter++
      Write-Output "Attempting to remove item number $orphanCounter for RoleAssignmentName: $($assignment.RoleAssignmentName) | RoleAssignmentId: $($assignment.RoleAssignmentId) | ObjectId: $($assignment.ObjectId) | RoleDefinitionName: $($assignment.RoleDefinitionName) | Scope: $($assignment.Scope)"
      
      Remove-AzRoleAssignment -ObjectId $assignment.ObjectId -RoleDefinitionName "${{ parameters.roleName }}" -Scope $assignment.Scope
      
      Write-Output "Successfully removed item number $orphanCounter"
    }
displayName: 'Remove orphaned role assignments'

请注意,根据文档,需要objectID(或signInname/serviceprincipalname),roledefinitionName和范围。

This could be modified to do all orphaned role assignments, regardless of which role it is, but here's the bulk of work I put into a pipeline that takes some parameters.

I used this post as a starting point. I chose to NOT iterate over subscriptions (It's a parameter in my pipeline).

Using powershell task in pipeline yaml:

- task: AzurePowerShell@5
inputs:
  azureSubscription: ${{ variables.azureServiceConnection }}
  azurePowerShellVersion: LatestVersion
  pwsh: true
  ScriptType: 'InlineScript'
  Inline: |
    
    Write-Output "Checking for orphaned assignments for role '${{ parameters.roleName }}'"
    $orphanedRoleAssignments = Get-AzRoleAssignment | Where-object -Property Displayname -eq $null
    
    if ($orphanedRoleAssignments.Count -eq 0) {
      Write-Output "No orphaned role assignments found. Exiting."
      exit 0
    }
    Write-Output "Total number of orphaned role assignments: $($orphanedRoleAssignments.Count)"
    
    $orphanedRoleAssignments = $orphanedRoleAssignments | Where-Object -Property RoleDefinitionName -eq "${{ parameters.roleName }}"
    
    if ($orphanedRoleAssignments.Count -eq 0) {
      Write-Output "No orphaned role assignments for the role '${{ parameters.roleName }}' found. Exiting."
      exit 0
    }
    Write-Output "Number of orphaned role assignments for role '${{ parameters.roleName }}': $($orphanedRoleAssignments.Count)"
    
    $orphanCounter = 0
    foreach ($assignment in $orphanedRoleAssignments) {
      $orphanCounter++
      Write-Output "Attempting to remove item number $orphanCounter for RoleAssignmentName: $($assignment.RoleAssignmentName) | RoleAssignmentId: $($assignment.RoleAssignmentId) | ObjectId: $($assignment.ObjectId) | RoleDefinitionName: $($assignment.RoleDefinitionName) | Scope: $($assignment.Scope)"
      
      Remove-AzRoleAssignment -ObjectId $assignment.ObjectId -RoleDefinitionName "${{ parameters.roleName }}" -Scope $assignment.Scope
      
      Write-Output "Successfully removed item number $orphanCounter"
    }
displayName: 'Remove orphaned role assignments'

Note that according to documentation, ObjectId (or SignInName/ServicePrincipalName), RoleDefinitionName, and Scope are required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文