Azure Active Directory: Add Service Principal to Directory Readers Role with PowerShell

╄→尐↘猪︶ㄣ 提交于 2019-12-04 21:59:36

Prerequisites

  • Check if you have the proper permissions to get the object id from a Service Principal
  • Check if you have the proper permissions to add the Service Principal to the "Directory Readers" role in the Azure Active Directory tenant (-> Admin)

Steps

  • Install the Azure AD Module via Install-Module AzureAD [1]

  • Connect to the Azure Active Directory

    • Connect-AzureAD
  • Get the Id of the "Directory Readers" role

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

    • $spObjectId = (Get-AzureADServicePrincipal -SearchString "spName").ObjectId
      • This of course only works if the result includes only one ObjectId
      • This is not the ObjectId of the application registered in the Azure Active Directory
  • Add service principal to the "Directory Readers" role

    • Add-AzureADDirectoryRoleMember -ObjectId $roleId -RefObjectId $spObjectId
  • Check if SP is assigned to the Directory Readers role

    • Get-AzureADDirectoryRoleMember -ObjectId $roleId | Where-Object {$_.ObjectId -eq $spObjectId}
  • If you want to remove the Service Principal from the role at a later stage

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

See also [2]

Resources

[1] Install Azure AD Module

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!