Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Azure by (13.1k points)

I was trying to get the objectID, for which I need to automate Connect-AzureAD. I am using this code:

Connect-AzureAD -TenantId $tenantId  -Verbose

$userObjectID = $(Get-AzureADUser -Filter "UserPrincipalName eq '$Owner'").ObjectId

It got stuck. Can anyone help me with this?

1 Answer

0 votes
by (26.7k points)

You can try the below code for the solution of your problem. This task is running on Azure DevOps pipeline.

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, "").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, "").AccessToken

Write-Output "Hi I'm $($context.Account.Id)"

Connect-AzureAD -AadAccessToken $aadToken -AccountId $context.Account.Id -TenantId $

I hope this help.

Want to become Azure expert? join azure devops certification now!!

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94k users

Browse Categories