【问题标题】:Azure Active Directory: Add Service Principal to Directory Readers Role with PowerShellAzure Active Directory:使用 PowerShell 将服务主体添加到目录读取者角色
【发布时间】:2019-01-16 10:43:04
【问题描述】:
  • 在 VSTS 中的自托管代理上运行时,Azure PowerShell 任务中的命令 (Get-AzureRmADUser -Mail $user).Id 返回 null
  • 问题在于服务主体需要具有从 Active Directory 读取的权限

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

【问题讨论】:

  • 这不是问题,你最好把它写成一篇博文!
  • 在这里有这个有什么不好?它基于一个问题(为什么 (Get-AzureRmADUser -Mail $user).Id 返回 null)并直接提供答案。不知道你的问题到底是什么。
  • 如果您投反对票,请说明原因

标签: azure powershell azure-devops azure-active-directory service-principal


【解决方案1】:

先决条件

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

步骤

  • 通过Install-Module AzureAD [1] 安装 Azure AD 模块

  • 连接到 Azure Active Directory

    • Connect-AzureAD
  • 获取“目录读者”角色的 ID

    • $roleId = (Get-AzureADDirectoryRole | where-object {$_.DisplayName -eq "Directory Readers"}).Objectid
  • 获取服务主体对象 ID

    • $spObjectId = (Get-AzureADServicePrincipal -SearchString "spName").ObjectId
      • 这当然只有在结果只包含一个 ObjectId 时才有效
      • 这不是在 Azure Active Directory 中注册的应用程序的 ObjectId
  • 将服务主体添加到“目录读者”角色

    • Add-AzureADDirectoryRoleMember -ObjectId $roleId -RefObjectId $spObjectId
  • 检查是否将 SP 分配给目录读者角色

    • Get-AzureADDirectoryRoleMember -ObjectId $roleId | Where-Object {$_.ObjectId -eq $spObjectId}
  • 如果您想稍后从角色中删除服务主体

    • Remove-AzureADDirectoryRoleMember -ObjectId $roleId -MemberId $spObjectId

另见 [2]

资源

[1]Install Azure AD Module

[2]Using a Service Principal to connect to a directory in PowerShell

【讨论】:

猜你喜欢
  • 2019-06-17
  • 2016-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-10
  • 2014-06-16
  • 2018-09-22
  • 1970-01-01
相关资源
最近更新 更多