4
votes

I would like to retrieve the tenant name THIS-THING-HERE.onmicrosoft.com using Azure CLI. I can't really find in documentation.

EDIT: When I'm calling azure account list I don't get user name in the domain provided since I'm login with corporate email:

[
  {
    "cloudName": "AzureCloud",
    "id": "46ee2f65-7112-4c96-ad0a-3ff6ca22a615",
    "isDefault": true,
    "name": "Visual Studio Professional",
    "state": "Enabled",
    "tenantId": "1caf5d6b-58cb-40e6-88b3-eb9ab9c0c010",
    "user": {
      "name": "[email protected]",
      "type": "user"
    }
  },
  {
    "cloudName": "AzureCloud",
    "id": "1efd84d6-173f-42cc-80db-7b2c17eb0edd",
    "isDefault": false,
    "name": "Microsoft Azure Enterprise",
    "state": "Enabled",
    "tenantId": "c48d02ad-7efd-4910-9b51-ebb7a4b75379",
    "user": {
      "name": "[email protected]",
      "type": "user"
    }
  }
]
3
consider accepting this answer ;) - 4c74356b41
I wish microsoft made it as easy as az account tenant list - Cristian E.

3 Answers

4
votes

You could use this command:

az ad signed-in-user show --query 'userPrincipalName' | cut -d '@' -f 2 | sed 's/\"//'

this will take user upn and take the last part

0
votes

To retrieve tenant name:

In the Azure CLI (I use GNU/Linux):

$ azure login  # add "-e AzureChinaCloud" if you're using Azure China

This will ask you to login via https://aka.ms/devicelogin or https://aka.ms/deviceloginchina

    $ azure account show

 {
  "environmentName": "AzureCloud",
  "id": "aaabbbcccdd-eeff-gghh-iijj-abcdef256984",
  "isDefault": true,
  "name": "MSDN Subscription",
  "state": "Enabled",
  "tenantId": "ggzzttyyh-56rg-op4e-iixx-kiednd256",
  "user": {
    "cloudShellID": true,
    "name": "[email protected]",
    "type": "user"
          }
 }

To get tenant ID:

az account list | jq -r '.[].tenantId'

To get tenant name:

az account list | jq -r '.[].user'.name

I hope it helps

0
votes

To get the primary domain of an Azure tenant using bash, az cli, curl and jq:

$ az login
$ AZURE_TOKEN_ID=$(az account get-access-token --resource-type ms-graph --query accessToken --output tsv)
$ curl --header "Authorization: Bearer ${AZURE_TOKEN_ID}" --request GET 'https://graph.microsoft.com/v1.0/domains' | jq -r '.value[] | select(.isDefault == true) | {id}[]'

The result will be something like:

mydomain.onmicrosoft.com