Azure Active Directory:使用 PowerShell 将服务主体添加到目录读取者角色 [英] Azure Active Directory: Add Service Principal to Directory Readers Role with PowerShell

查看:20
本文介绍了Azure Active Directory:使用 PowerShell 将服务主体添加到目录读取者角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 在 VSTS 中的自托管代理上运行时,Azure PowerShell 任务中的命令 (Get-AzureRmADUser -Mail $user).Id 返回 null
  • 问题在于服务主体需要具有从 Active Directory 读取的权限

如何为服务主体授予从 Azure Active Directory 读取的正确权限?

How can I give the the Service Principal the correct permissions to read from the Azure Active Directory?

推荐答案

先决条件

  • 检查您是否具有从服务主体获取对象 ID 的适当权限
  • 检查您是否具有将服务主体添加到 Azure Active Directory 租户(-> 管理员)中的目录读取者"角色的适当权限
    • 通过 Install-Module AzureAD [1]

    连接到 Azure Active Directory

    Connect to the Azure Active Directory

    • Connect-AzureAD

    获取目录读者"角色的 ID

    Get the Id of the "Directory Readers" role

    • $roleId = (Get-AzureADDirectoryRole | where-object {$_.DisplayName -eq "Directory Readers"}).Objectid

    获取服务主体对象 ID

    Get the Service Principal Object ID

    • $spObjectId = (Get-AzureADServicePrincipal -SearchString "spName").ObjectId
      • 这当然只有在结果只包含一个 ObjectId 时才有效
      • 这不是在 Azure Active Directory 中注册的应用程序的 ObjectId

      将服务主体添加到目录读取者"角色

      Add service principal to the "Directory Readers" role

      • Add-AzureADDirectoryRoleMember -ObjectId $roleId -RefObjectId $spObjectId

      检查是否将 SP 分配给目录读取者角色

      Check if SP is assigned to the Directory Readers role

      • 获取-AzureADDirectoryRoleMember -ObjectId $roleId |Where-Object {$_.ObjectId -eq $spObjectId}

      如果您想稍后从角色中删除服务主体

      If you want to remove the Service Principal from the role at a later stage

      • 删除-AzureADDirectoryRoleMember -ObjectId $roleId -MemberId $spObjectId

      另见 [2]

      See also [2]

      [1] 安装 Azure AD 模块

      [2] 使用服务主体连接到 PowerShell 中的目录

      这篇关于Azure Active Directory:使用 PowerShell 将服务主体添加到目录读取者角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆