Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Azure AD objectid of the signed in user?

I'm trying to invoke an ARM template that requires a PrincipalId of the currently signed in user.

https://learn.microsoft.com/en-us/azure/templates/microsoft.keyvault/vaults

I've signed in using powershell, as a guest account in the organisation's AAD. When I check the resulting context, I get:

Name             : [[email protected], 5f813400-5b93-43b0-af8f-5fd04714f1ef]
Account          : [email protected]
SubscriptionName : SomeSubscriptionName
TenantId         : e6d2d4cc-b762-486e-8894-4f5f540d5f31
Environment      : AzureCloud

I'm wondering how to get the AAD ObjectId from the above, without string parsing "Name"?

Note that the documentation for the ARM Template is not very clear so not sure if [email protected] would work just as well (am assuming it's talking about a Guid).

Thank you.

like image 300
user9314395 Avatar asked Aug 16 '18 06:08

user9314395


3 Answers

You can also get it using the azure cli

az ad signed-in-user show --query objectId -o tsv
like image 168
Lolorol Avatar answered Oct 23 '22 03:10

Lolorol


You could try Get-AzureRmADUser to get the ObjectId .

Sample:

Get-AzureRmADUser -UserPrincipalName "[email protected]"

Result:

enter image description here

The Id is the ObjectId, you could get it. Also, you could get it via other properties, not only -UserPrincipalName, just refer to the link of the command.

Update:

If you use a Guest account, you could try the command below.

Get-AzureADUser | ?{$_.UserType -eq "Guest"} | ?{$_.UserPrincipalName -like "*partofyouraccount*"}

enter image description here

Note: Before using this command, you need to install Azure AD powershell module.

like image 24
Joy Wang Avatar answered Oct 23 '22 05:10

Joy Wang


The info on "Name" you are seeing is related to the subscription. Use the command below to get the objectId under "Account":

(Get-AzContext).Account.ExtendedProperties.HomeAccountId.Split('.')[0]
like image 36
Fabiano Avatar answered Oct 23 '22 05:10

Fabiano