我的问题是:不使用powershell命令可以获得azure活动目录租户id吗?

我找到了这两个博客,有了这个帮助,我已经能够从powershell获得租户ID和订阅ID。这是找回房客的唯一方法吗?

在Windows PowerShell中获取Windows Azure活动目录租户ID

Windows Azure AD认证支持PowerShell

谢谢


当前回答

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

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

这将要求您通过https://aka.ms/devicelogin或https://aka.ms/deviceloginchina登录

$ azure account show
info:    Executing command account show
data:    Name                        : BizSpark Plus
data:    ID                          : aZZZZZZZ-YYYY-HHHH-GGGG-abcdef569123
data:    State                       : Enabled
data:    Tenant ID                   : 0XXXXXXX-YYYY-HHHH-GGGG-123456789123
data:    Is Default                  : true
data:    Environment                 : AzureCloud
data:    Has Certificate             : No
data:    Has Access Token            : Yes
data:    User name                   : nico@XXXXXXX.onmicrosoft.com
data:    
info:    account show command OK

或者仅仅是:

azure account show --json | jq -r '.[0].tenantId'

或者新的az:

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

我希望这对你们有帮助

其他回答

一键回答:

打开这个URL:

https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/Properties

xxx@Azure:~$ az ad sp create-for-rbac
Retrying role assignment creation: 1/36
{
  "appId": "401143c2-95ef-4792-9900-23e07f7801e7",
  "displayName": "azure-cli-2018-07-10-20-31-57",
  "name": "http://azure-cli-2018-07-10-20-31-57",
  "password": "a0471d14-9300-4177-ab08-5c45adb3476b",
  "tenant": "e569f29e-b008-4cea-b6f0-48fa8532d64a"
}

截至目前(06/07/2018),一个简单的方法是在Azure门户中运行Azure云Shell中的az帐户显示(需要存储帐户)。

——命令——

az account show

——命令输出——

{
  "environmentName": "AzureCloud",
  "id": "{Subscription Id (GUID)}",
  "isDefault": true,
  "name": "{Subscription Name}",
  "state": "Enabled",
  "tenantId": "{Tenant Id (GUID)}",
  "user": {
    "cloudShellID": true,
    "name": "{User email}",
    "type": "user"
  }
}

有关Azure Cloud Shell的更多详细信息,请参阅Azure Cloud Shell | Microsoft Docs概述。

从Java:

public static String GetSubscriptionTenantId (String subscriptionId) throws ClientProtocolException, IOException
{
    String tenantId = null;
    String url = "https://management.azure.com/subscriptions/" + subscriptionId + "?api-version=2016-01-01";

    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(url);
    HttpResponse response = client.execute(request);

    Header[] headers = response.getAllHeaders();
    for (Header header : headers)
    {
        if (header.getName().equals("WWW-Authenticate"))
        {
            // split by '"' to get the URL, split the URL by '/' to get the ID
            tenantId = header.getValue().split("\"")[1].split("/")[3];
        }
    }

    return tenantId;
}

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

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

这将要求您通过https://aka.ms/devicelogin或https://aka.ms/deviceloginchina登录

$ azure account show
info:    Executing command account show
data:    Name                        : BizSpark Plus
data:    ID                          : aZZZZZZZ-YYYY-HHHH-GGGG-abcdef569123
data:    State                       : Enabled
data:    Tenant ID                   : 0XXXXXXX-YYYY-HHHH-GGGG-123456789123
data:    Is Default                  : true
data:    Environment                 : AzureCloud
data:    Has Certificate             : No
data:    Has Access Token            : Yes
data:    User name                   : nico@XXXXXXX.onmicrosoft.com
data:    
info:    account show command OK

或者仅仅是:

azure account show --json | jq -r '.[0].tenantId'

或者新的az:

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

我希望这对你们有帮助