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

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

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

Windows Azure AD认证支持PowerShell

谢谢


当前回答

获取tenantID的简单方法是:

Connect-MsolService -cred $LiveCred  #sign in to tenant

(Get-MSOLCompanyInformation).objectid.guid  #get tenantID

其他回答

只是为一个老问题(但仍然相关的问题)添加一个新方法。 在新的门户中,在任何屏幕上单击帮助图标并选择“显示诊断”将显示一个JSON文档,其中包含所有租户信息,包括TenantId、租户名称以及更多有用的信息

PowerShell:

Add-AzureRmAccount #if not already logged in 
Get-AzureRmSubscription -SubscriptionName <SubscriptionName> | Select-Object -Property 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'

我希望这对你们有帮助

您还可以通过登录url resoures.azure.com获得租户id,实际上是所有的订阅详细信息

从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;
}