Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting tenant name from azure CLI

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"
    }
  }
]
like image 940
Dzior Avatar asked Nov 13 '18 10:11

Dzior


People also ask

How do I find my Azure tenant name?

Sign in to the Azure portal. Select Azure Active Directory from the menu. The Azure Active Directory Overview page appears. To find the Azure AD tenant ID or primary domain name, look for Tenant ID and Primary domain in the Basic information section.

Which CLI command shows Azure AD tenant details such as the tenant ID?

You can also list your subscriptions and view their IDs programmatically by using Get-AzSubscription (Azure PowerShell) or az account list (Azure CLI).

How do I get my Azure CLI subscription details?

To see the subscription you're currently using or to get a list of available subscriptions, run the az account show or az account list command. Go to Learn to use Bash with the Azure CLI to see more examples of ways to use az account show . The --output parameter is a global parameter, available for all commands.


2 Answers

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

like image 180
4c74356b41 Avatar answered Sep 20 '22 16:09

4c74356b41


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
like image 38
mpowrie Avatar answered Sep 19 '22 16:09

mpowrie