Az-GetRoleAssigments 不返回 DisplayName、SignInName 和对象类型的数据 - Azure Powershell Runbook

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

我一直致力于获取分配给 azure 订阅的 RBAC 角色列表,以了解将哪个 RBAC 分配给用户或 AD 组。根据 Microsoft 网站,我可以使用 powershell 并使用以下代码 Get-AZRoleAssignment 来完成此操作。

我的第一次尝试是创建一个本地 powershell 文件来测试获取数据的过程并将输出添加到 Excel(我使用自己的用户向 Microsoft Entra (AD) 进行身份验证)。结果正如预期的那样

enter image description here

为了自动执行此操作并且不使用我自己的用户名和密码(避免 MFA 弹出),我决定使用 Azure Automation Powershell Runbook(对 azure 订阅和存储帐户使用管理身份)。代码已成功执行,并且创建了 Excel,但现在有 2 个空白列,这些列之前不是空白的 + 所有行上的“对象类型”列值都是“未知”(如果我运行本地 powershell,这些列将显示数据)。下面示例图片中的行与图片 #1 相同,但在 Azure Runbook 中执行:

enter image description here

我想也许我缺少 AzureAD 模块,所以我添加了它以及其他一些模块,例如:EntraIDTools、ResolveEntraID(这些已添加到我的 Azure 自动化帐户,因为在我的本地 powershell 代码上,第一行安装了 AzureAD 模块)。到目前为止,没有任何工作,最终的 Excel 输出仍然将 DisplayName 和 SignInName 列值显示为空白 + 对象类型显示 Uknown 。此时我有点沮丧

下面是我的 Azure Automation Powershell Runbook 7.2

# Add needed variables
Param
(
  [Parameter (Mandatory= $true)]
  [String] $StorageAccountName
)


# Connect using a Managed Service Identity
try {
        $AzureContext = (Connect-AzAccount -Identity).context
    }
catch{
        Write-Output "There is no system-assigned user identity. Aborting."; 
        exit
    }


Get-AZRoleAssignment | 
Select-Object RoleAssignmentId,  Scope, DisplayName, SignInName, RoleDefinitionName, RoleDefinitionId, ObjectId, ObjectType, CanDelegate | 
Export-CSV -Encoding ASCII ($env:TEMP2+"MT_AZRoleAssigments.csv") -Notype

# Connect using a Managed Service Identity
try {
        $AzureContext = (Connect-AzAccount -Identity).context
    }
catch{
        Write-Output "There is no system-assigned user identity. Aborting."; 
        exit
    }

$Context = New-AzStorageContext -StorageAccountName $StorageAccountName

Set-AzStorageBlobContent -Context $Context -Container "compliance" -File ($env:TEMP2+"MT_AZRoleAssigments.csv") -Blob "MT_AZRoleAssigments.csv" -Force 
powershell azure-active-directory azure-runbook
1个回答
0
投票

Get-AzRoleAssignment
的文档非常清楚为什么会发生
ObjectType
等于
Unkown
的情况:

cmdlet 可以根据输入参数调用以下 Microsoft Graph API:

  • 获取/用户/{id}
  • GET /servicePrincipals/{id}
  • 获取/组/{id}
  • GET /directoryObjects/{id}
  • POST /directoryObjects/getByIds

请注意,如果未找到角色分配的对象或当前帐户没有足够的权限来获取对象类型,则此 cmdlet 会在输出中将

ObjectType
标记为
Unknown

如果您希望托管身份能够从角色分配中提取此信息,您需要为其分配所需的 API 权限,即:授予

Directory.Read.All
应允许您的 MI 查询 Entra Id 中的任何主体。

© www.soinside.com 2019 - 2024. All rights reserved.