1

I am doing the following for connecting to the AAD usingazure devops Pipeline for Powershell task

  1. created a SPN and granted the Permissions for the Graph
  2. While using the code below to connect using the access token it says Error Pipleline

My code is simple As below

    Install-Module -Name "AzureAD" -Force
   $context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
   $graphToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, "https://graph.microsoft.com").AccessToken
   $aadToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, "https://graph.windows.net").AccessToken      
  
   Write-Output "Hi I'm $($context.Account.Id)"
   Write-Output "AAD Token is  $($aadToken)"
   Connect-AzureAD -TenantId $context.tenant.id -AadAccessToken $aadToken -MsAccessToken $graphToken -AccountId $context.Account.Id 
   $outputusers= get-azureaduser -Top 5
   Write-Output "The users are listed $($outputusers)"

Could you please let me know if i missed any detail. The SPN is just a devops pipeline one and its not a AAD user. So the token may be invalid for authentication.

What do i need to do for the SPN to be a user in AAD. Please advice

1 Answer 1

1

From your code, you use Get-AzureADUser to call Azure AD Graph, so try to add Azure Active Directory Graph Directory.Read.All permissions, can solve your problem.

enter image description here enter image description here

By the way, the -MsAccessToken $graphToken command can be omitted.

Sign up to request clarification or add additional context in comments.

3 Comments

Carl Zhao it is pretty awesome answer for the authentication problems. Thanks
@CarlZhao is the Directory.Read.All permission the same for both the Microsoft Graph API as the Azure Active Directory Graph? In other words, it doesn't matter which of the two APIs you choose as long as it is permission Directory.Read.All?
No, the functions of these two endpoints are different, so you need to select the Directory.Read.All permission under Azure Active Directory Graph api.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.