【问题标题】:Getting tenant name from azure CLI从 azure CLI 获取租户名称
【发布时间】:2018-11-13 10:38:03
【问题描述】:

我想使用 Azure CLI 检索租户名称 THIS-THING-HERE.onmicrosoft.com。我在文档中真的找不到。

编辑: 当我调用 azure account list 时,由于我使用公司电子邮件登录,因此我没有在提供的域中获得用户名:

[
  {
    "cloudName": "AzureCloud",
    "id": "46ee2f65-7112-4c96-ad0a-3ff6ca22a615",
    "isDefault": true,
    "name": "Visual Studio Professional",
    "state": "Enabled",
    "tenantId": "1caf5d6b-58cb-40e6-88b3-eb9ab9c0c010",
    "user": {
      "name": "a.krajniak@avanade.com",
      "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": "a.krajniak@avanade.com",
      "type": "user"
    }
  }
]

【问题讨论】:

  • 考虑接受这个答案 ;)
  • 我希望微软能像 az account tenant list 一样简单

标签: azure azure-cli


【解决方案1】:

你可以使用这个命令:

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

这将占用用户 upn 并占用最后一部分

【讨论】:

  • 我收到az ad: 'signed-in-user' is not in the 'az ad' command group. See 'az ad --help'
  • 更新您的 azure cli,或使用任何其他显示您的 upn 的地方,例如 az account show
  • 我已经在我的 mac 上更新了我的 azure cli,还是一样。我也在文档中搜索了这个命令,没有任何运气
  • 就像我说的,使用另一个查询:az account show --query 'user.name' | cut -d '@' -f 2 | sed 's/\"//'
  • 这仅适用于当前用户在目录中而不是访客类型
【解决方案2】:

使用 bash、az cli、curl 和 jq 获取 Azure 租户的主域:

$ 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}[]'

结果会是这样的:

mydomain.onmicrosoft.com

【讨论】:

    【解决方案3】:

    感谢az rest 子命令,这可以在单行中实现:

    这适用于租户用户,也适用于来宾用户和 CSP 管理员。

    az rest --method get --url https://graph.microsoft.com/v1.0/domains --query 'value[?isDefault].id' -o tsv
    

    【讨论】:

      【解决方案4】:

      要检索租户名称:

      在 Azure CLI(我使用 GNU/Linux)中:

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

      这将要求您通过https://aka.ms/deviceloginhttps://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": "paul@xxx.onmicrosoft.com",
          "type": "user"
                }
       }
      

      获取租户 ID:

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

      获取租户名称:

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

      希望对你有帮助

      【讨论】:

      • 使用az ad signed-in-user show --query 'userPrincipalName'
      • 仅当用户在当前目录而不是访客用户时才有效
      • 这是订阅名称,而不是租户名称。
      【解决方案5】:

      我用这个:

      az account list --query "[?isDefault].tenantId | [0]" --output tsv
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-20
        • 1970-01-01
        • 1970-01-01
        • 2018-09-11
        • 1970-01-01
        • 2022-09-27
        相关资源
        最近更新 更多