我使用 Azure Graph 查询获取租户中的所有资源组标签,该查询与门户中的 Azure 图形资源管理器完美配合。
这是查询:
resourcecontainers
| where type == 'microsoft.resources/subscriptions/resourcegroups'
| extend dates=format_datetime(now(), "yyyy-MM-dd")
| join kind=leftouter (
resourcecontainers
| where type == 'microsoft.resources/subscriptions'
| project SubscriptionName=name, subscriptionId)
on subscriptionId
| project SubscriptionName, subscriptionId, resourceGroup, client_entity_name=tags.client_entity_name,
owner_contact=tags.owner_contact, owner_group=tags.owner_group, financial_contact=tags.financial_contact, billing_code=tags.billing_code, security_contact=tags.security_contact,
operational_contact=tags.operational_contact, profil=tags.profil, guid=tags.guid
我在门户中获取所有结果(超过 2000 个资源组)。
当我尝试使用 python 脚本执行相同操作时,我得到了 530 个资源的页面限制。 这是我的脚本:
from azure.identity import DefaultAzureCredential
from azure.mgmt.resourcegraph import ResourceGraphClient
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resourcegraph.models import *
import json
# Initialize Azure credentials
credentials = DefaultAzureCredential()
# Initialize Resource Graph client
resource_graph_client = ResourceGraphClient(credentials)
skip = 0
result = []
query_code = f"""
resourcecontainers
| where type == 'microsoft.resources/subscriptions/resourcegroups'
| extend dates=format_datetime(now(), "yyyy-MM-dd")
| join kind=leftouter (
resourcecontainers
| where type == 'microsoft.resources/subscriptions'
| project SubscriptionName=name, subscriptionId)
on subscriptionId
| project SubscriptionName, subscriptionId, resourceGroup, client_entity_name=tags.client_entity_name,
owner_contact=tags.owner_contact, owner_group=tags.owner_group, financial_contact=tags.financial_contact, billing_code=tags.billing_code, security_contact=tags.security_contact,
operational_contact=tags.operational_contact, profil=tags.profil, guid=tags.guid, cloudbundle_type=tags.cloudbundle_type, environment=tags.environment,
classification=tags.classification, app_name=tags.app_name, app_family=tags.app_family, application_id=tags.application_id,
managed_by=tags.managed_by, managed_by_capmsp=tags.managed_by_capmsp, capmsp_service_level=tags.capmsp_service_level, sla_class=tags.sla_class,
version=tags.version, dates, type, location, id_prefix=id
"""
query = QueryRequest(
query= query_code
)
query_response = resource_graph_client.resources(query)
query_response_str = str(query_response)
json_data = json.dumps(query_response_str)
json_data = json.loads(json_data)
output_file = "resource_groups_tags.txt"
with open(output_file, "w") as f:
json.dump(json_data, f, indent=4)
这是回复的第一部分:
{'additional_properties': {}, 'total_records': 530, 'count': 530, 'result_truncated': 'false', 'skip_token': None, 'data': [{'SubscriptionName': '
我真的不知道如何处理分页来获取所有结果,因为查询中没有跳过/偏移。在微软文档中,他们谈论了“skip_token”,但我没有发现它真的很清楚,在响应中它被设置为“无”。
有人可以帮忙吗?
问候
我尝试过跳过,限制...但跳过不适用于限制,所以我不知道如何处理它。
tl;dr:当 SkipToken 到达您的查询结果时, 将其添加到后续查询中以获得下一页结果。
供应商文档 解释:
您可能还对由于 Azure 资源图在单个查询响应中最多返回 1,000 个条目,因此您可能需要对查询进行分页......
通过将从上一个查询响应返回的跳过标记传递给下一个分页查询来处理分页。
--skip
选项感兴趣 由
az
CLI 工具提供。