Change Azure directory from command line
Asked Answered
S

3

31

I'm trying to use az against my Azure account. My account has two directories: one for personal (default) and one for business. I need to "switch to" the business directory so that az has access to the correct resources. However, I cannot find any way to achieve this via the command line, so when I do az group list I see the resource groups from my personal directory, not the business one.

How can I switch Azure directory from the CLI?

Shemikashemite answered 25/3, 2019 at 3:8 Comment(0)
S
58

Subscription and directory is not the same. You can have access to several subscriptions in your work directory for example.

To login to a different (non-default) directory, use the --tenant option with the az login command, passing the FQDN for the directory, e.g.

az login --tenant yourdir.onmicrosoft.com

You can find the FQDN in Azure Portal when listing the directories.

When logged into a directory, you can see list of all your available subscriptions.

List of Directories in Azure Portal

Stratocracy answered 19/9, 2019 at 8:44 Comment(3)
Also note you may need to add param --allow-no-subscriptions in case there is no subscription for the directory. So use like this: az login --tenant yourdir.onmicrosoft.com --allow-no-subscriptions. E.g. we have directory for Azure AD B2C created and seems to require this, but not sure why.Tornado
just wanted to say that the above comment saved me :) thanks to both of you!Tedra
How should exactly do that for Azure Sign in VS code for Azure ResourcesYann
S
7
# List of the tenants:
az account tenant list
[
  {
    "id": "/tenants/91358f27-xxxx-xxxxxxxxxxx",
    "tenantId": "91358f27-xxxx-xxxxxxxxxxx"
  },
  {
    "id": "/tenants/cf39b7bf-xxxx-xxxxxxxxxxx",
    "tenantId": "cf39b7bf-xxxx-xxxxxxxxxxx"
  }
]

# Select the tenant ID:
az login --tenant cf39b7bf-xxxx-xxxxxxxxxxx --allow-no-subscriptions

# Set a validated subscription:
az account set --subscription "Pago por uso"

# Verify
az account list -o table
Sidereal answered 1/8, 2021 at 18:59 Comment(0)
S
1

Ugh, nevermind. For some reason the CLI calls them subscriptions when the portal calls them directories. So I needed az account set --subscription $SUBSCRIPTION_ID

Shemikashemite answered 25/3, 2019 at 3:11 Comment(2)
This way you only change the active subscription in your default directory. You need to specify the directory during az login as shown in my answer.Stratocracy
This is not quite true. 'Directory' and 'tenant' are interchangeable names for the top level hierarchy. (Usually there will be a single tenant or directory per organisation.) A directory or tenant may contain multiple subscriptions. If you have only a single tenant associated with your account, and your tenant contains only a single subscription, then just az login should work. If your account has been guested into multiple tenants, then you will need to also specify the tenant. If your tenant has more than one subscription, then you will need to specify the subscription too.Liatris

© 2022 - 2024 — McMap. All rights reserved.