如何从不同的 AAD 租户获取管理组和订阅?
How to get management groups and subscriptions from different AAD tenant?
蔚蓝
我的帐户中有两个 AAD(Azure Active Directory)。
第一个 AAD 中的实体:['Tenant Root Group', 'group A', 'subGroup B', 'Microsoft Partner Network', 'subscription 2']
第二个 AAD 中的实体:['Tenant Root Group', 'subscription 3']
Python
我正在尝试使用 python azure SDK 获取 management groups
和 subscriptions
每个 directory
.
下面的代码可以列出第一个目录中的实体,但是第二个目录中的其他实体并没有像我期望的那样列出。
有谁知道如何获取两个目录中的所有实体?
代码
from azure.mgmt.managementgroups import ManagementGroupsAPI
from msrestazure.azure_active_directory import UserPassCredentials
def get_entities(credentials):
mgmt_groups_api = ManagementGroupsAPI(credentials)
entities = mgmt_groups_api.entities.list()
entity_infos = [entity for entity in entities]
entity_names = [entity.display_name for entity in entity_infos]
print(entity_names)
def main():
credentials = UserPassCredentials(
'account',
'password',
)
get_entities(credentials)
if __name__ == '__main__':
main()
输出
['Group A', 'subGroup B', 'subGroup C', 'subscription 1', 'subscription 2']
我认为@juunas的评论是正确的,使用凭证时需要指定租户。
I think the problem becomes 'how to get a list of AAD tenant id'
您可以使用此 REST API - Tenants - List
为您的帐户获取租户。
GET https://management.azure.com/tenants?api-version=2020-01-01
获取租户id后,在用户凭证中指定租户,确保你使用的是没有MFA的工作账户(组织账户,不是个人账户),用户凭证使用不会的ROPC flow使用个人帐户。
感谢@juunas 指出这个问题真正需要什么,@Joy Wang 提供了一个 API 解决方案来按帐户获取租户列表。
API解决方案
再次感谢@juunas,使用Tenants - List API we can easily listing tenants. (For more detail please take a look at 。)
我认为这是解决这个问题的一个很好的通用方法。
用于 Python 解决方案的 Azure SDK
幸运的是,我发现 Azure SDK for Python
提供了 SubscriptionClient 允许我以编程方式列出租户。
这是我在 Python 中列出租户的方式:
def get_tenants() -> [TenantIdDescription]:
credentials = UserPassCredentials(
'account',
'password',
)
sub_client = SubscriptionClient(credentials)
tenants = sub_client.tenants.list()
return tenants
合并SubscriptionClient为原始码
from azure.mgmt.managementgroups import ManagementGroupsAPI
from azure.mgmt.resource import SubscriptionClient
from msrestazure.azure_active_directory import UserPassCredentials
azure_account = ''
azure_pwd = ''
def get_credential(tenant_id: str = None):
if tenant_id:
return UserPassCredentials(
azure_account,
azure_pwd,
tenant=tenant_id
)
else:
return UserPassCredentials(
azure_account,
azure_pwd,
)
def get_entities(tenant_id=None):
credentials = get_credential(tenant_id)
mgmt_groups_api = ManagementGroupsAPI(credentials)
entities = mgmt_groups_api.entities.list()
entity_infos = [entity for entity in entities]
entity_names = [entity.display_name for entity in entity_infos]
print(f' entities: {entity_names}')
def get_tenants():
credentials = get_credential()
sub_client = SubscriptionClient(credentials)
tenants = sub_client.tenants.list()
return tenants
def main():
tenants = get_tenants()
i = 0
print('[tenant list]')
for tenant in tenants:
print(f'tenant {i}:')
print(f' name: {tenant.display_name}')
print(f' id: {tenant.tenant_id}')
get_entities(tenant.tenant_id)
print()
i = i + 1
if __name__ == '__main__':
main()
产出
[tenant list]
tenant 0:
name: tenant1
id: 00000000-0000-0000-0000-000000000000
entities: ['Tenant Root Group', 'group A', 'subGroup B', 'Microsoft Partner Network', 'subscription 2']
tenant 1:
name: tenant2
id: 00000000-0000-0000-0000-000000000000
entities: ['Tenant Root Group', 'subscription 3']
蔚蓝
我的帐户中有两个 AAD(Azure Active Directory)。
第一个 AAD 中的实体:['Tenant Root Group', 'group A', 'subGroup B', 'Microsoft Partner Network', 'subscription 2']
第二个 AAD 中的实体:['Tenant Root Group', 'subscription 3']
Python
我正在尝试使用 python azure SDK 获取 management groups
和 subscriptions
每个 directory
.
下面的代码可以列出第一个目录中的实体,但是第二个目录中的其他实体并没有像我期望的那样列出。
有谁知道如何获取两个目录中的所有实体?
代码
from azure.mgmt.managementgroups import ManagementGroupsAPI
from msrestazure.azure_active_directory import UserPassCredentials
def get_entities(credentials):
mgmt_groups_api = ManagementGroupsAPI(credentials)
entities = mgmt_groups_api.entities.list()
entity_infos = [entity for entity in entities]
entity_names = [entity.display_name for entity in entity_infos]
print(entity_names)
def main():
credentials = UserPassCredentials(
'account',
'password',
)
get_entities(credentials)
if __name__ == '__main__':
main()
输出
['Group A', 'subGroup B', 'subGroup C', 'subscription 1', 'subscription 2']
我认为@juunas的评论是正确的,使用凭证时需要指定租户。
I think the problem becomes 'how to get a list of AAD tenant id'
您可以使用此 REST API - Tenants - List
为您的帐户获取租户。
GET https://management.azure.com/tenants?api-version=2020-01-01
获取租户id后,在用户凭证中指定租户,确保你使用的是没有MFA的工作账户(组织账户,不是个人账户),用户凭证使用不会的ROPC flow使用个人帐户。
感谢@juunas 指出这个问题真正需要什么,@Joy Wang 提供了一个 API 解决方案来按帐户获取租户列表。
API解决方案
再次感谢@juunas,使用Tenants - List API we can easily listing tenants. (For more detail please take a look at
我认为这是解决这个问题的一个很好的通用方法。
用于 Python 解决方案的 Azure SDK
幸运的是,我发现 Azure SDK for Python
提供了 SubscriptionClient 允许我以编程方式列出租户。
这是我在 Python 中列出租户的方式:
def get_tenants() -> [TenantIdDescription]:
credentials = UserPassCredentials(
'account',
'password',
)
sub_client = SubscriptionClient(credentials)
tenants = sub_client.tenants.list()
return tenants
合并SubscriptionClient为原始码
from azure.mgmt.managementgroups import ManagementGroupsAPI
from azure.mgmt.resource import SubscriptionClient
from msrestazure.azure_active_directory import UserPassCredentials
azure_account = ''
azure_pwd = ''
def get_credential(tenant_id: str = None):
if tenant_id:
return UserPassCredentials(
azure_account,
azure_pwd,
tenant=tenant_id
)
else:
return UserPassCredentials(
azure_account,
azure_pwd,
)
def get_entities(tenant_id=None):
credentials = get_credential(tenant_id)
mgmt_groups_api = ManagementGroupsAPI(credentials)
entities = mgmt_groups_api.entities.list()
entity_infos = [entity for entity in entities]
entity_names = [entity.display_name for entity in entity_infos]
print(f' entities: {entity_names}')
def get_tenants():
credentials = get_credential()
sub_client = SubscriptionClient(credentials)
tenants = sub_client.tenants.list()
return tenants
def main():
tenants = get_tenants()
i = 0
print('[tenant list]')
for tenant in tenants:
print(f'tenant {i}:')
print(f' name: {tenant.display_name}')
print(f' id: {tenant.tenant_id}')
get_entities(tenant.tenant_id)
print()
i = i + 1
if __name__ == '__main__':
main()
产出
[tenant list]
tenant 0:
name: tenant1
id: 00000000-0000-0000-0000-000000000000
entities: ['Tenant Root Group', 'group A', 'subGroup B', 'Microsoft Partner Network', 'subscription 2']
tenant 1:
name: tenant2
id: 00000000-0000-0000-0000-000000000000
entities: ['Tenant Root Group', 'subscription 3']