我的帐户中有两个AAD(Azure Active Directory)。
第一个AAD中的实体:['Tenant Root Group', 'group A', 'subGroup B', 'Microsoft Partner Network', 'subscription 2']
第二个AAD中的实体:['Tenant Root Group', 'subscription 3']
我正在尝试使用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']
答案 0 :(得分:1)
我认为@juunas的评论是正确的,使用凭据时需要指定租户。
我认为问题就变成了“如何获取AAD租户ID列表”
您可以使用此REST API-Tenants - List
获取您帐户的租户。
GET https://management.azure.com/tenants?api-version=2020-01-01
获取租户ID后,在用户凭据中指定租户,请确保您使用的工作帐户没有MFA(组织帐户,而不是个人帐户),用户凭据使用ROPC flow,该帐户无法用于个人帐户。
答案 1 :(得分:0)
感谢@juunas指出了这个问题的真正需要,而@Joy Wang提供了一种API解决方案,可以按帐户获取租户列表。
再次感谢@juunas,通过使用Tenants - List API,我们可以轻松列出租户。 (有关更多详细信息,请查看his answer。)
我认为这是解决此问题的好方法。
幸运的是,我发现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
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']