You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过MS Graph PowerShell获取自定义AD角色的成员

获取自定义Azure AD角色成员的MS Graph PowerShell方法

自定义Azure AD角色和内置角色的机制不同,无法通过Get-MgDirectoryRole或Get-MgDirectoryRoleMember直接获取成员,需要通过角色定义和角色分配的cmdlet来实现:

  1. 获取自定义角色的RoleDefinitionId
    先通过Get-MgRoleManagementDirectoryRoleDefinition定位你的自定义角色,比如按名称筛选:
# 替换为你的自定义角色名称
$customRoleName = "自定义角色名称"
$customRole = Get-MgRoleManagementDirectoryRoleDefinition -Filter "DisplayName eq '$customRoleName'"
$roleDefinitionId = $customRole.Id
  1. 获取该角色的所有分配记录
    使用Get-MgRoleManagementDirectoryRoleAssignment获取该角色的所有分配,结果里的PrincipalId就是成员的目录对象ID:
$roleAssignments = Get-MgRoleManagementDirectoryRoleAssignment -Filter "RoleDefinitionId eq '$roleDefinitionId'"
  1. 查询成员详细信息
    遍历分配记录,通过Get-MgDirectoryObject获取成员的具体信息:
foreach ($assignment in $roleAssignments) {
    Get-MgDirectoryObject -DirectoryObjectId $assignment.PrincipalId | 
        Select-Object Id, DisplayName, UserPrincipalName, ObjectType
}

补充说明

  • 内置角色会有对应的DirectoryRole实例(即启用后的角色),所以可以用Get-MgDirectoryRoleMember;但自定义角色没有这个实例,只能通过角色分配关系来关联成员。
  • 确保你的MS Graph PowerShell模块是最新版本,避免cmdlet缺失。

内容的提问来源于stack exchange,提问作者Ovw-lang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 09:10:00