我想使用 Azure CLI 检索租户名称
THIS-THING-HERE.onmicrosoft.com
。我在文档中找不到。
编辑: 当我调用 azure 帐户列表时,由于我使用公司电子邮件登录,因此在提供的域中没有获取用户名:
[
{
"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"
}
}
]
我用这个:
az account list --query "[?isDefault].tenantId | [0]" --output tsv
您可以使用此命令:
az ad signed-in-user show --query 'userPrincipalName' | cut -d '@' -f 2 | sed 's/\"//'
这将获取用户 upn 并获取最后一部分
借助
az rest
子命令,一行代码即可实现这一点:
这适用于租户用户,也适用于来宾用户和 CSP 管理员。
az rest --method get --url https://graph.microsoft.com/v1.0/domains --query 'value[?isDefault].id' -o tsv
要使用 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
要检索租户名称:
在 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
{
"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": "[email protected]",
"type": "user"
}
}
获取租户 ID:
az account list | jq -r '.[].tenantId'
获取租户姓名:
az account list | jq -r '.[].user'.name
希望对你有帮助
您可以使用此命令获取租户列表,它有一个 displayName 属性:
az rest --method get --url '/tenants?api-version=2020-01-01'
如果您可以通过 Azure Lighthouse 访问客户租赁,则可以通过附加
$includeAllTenantCategories=true
来显示这些内容
将其传递给 az cli 并在 powershell 中转义 $ 符号将产生以下命令
az rest --method get --url '/tenants?api-version=2020-01-01' --url-parameters `$includeAllTenantCategories=true