您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

高级资源图表查询Advanced Resource Graph queries

了解使用 Azure 资源图表进行查询的第一步是对查询语言有基本的了解。The first step to understanding queries with Azure Resource Graph is a basic understanding of the Query Language. 如果还不熟悉 Azure 数据资源管理器,建议查看基础知识,以了解如何撰写所需资源的请求。If you aren't already familiar with Azure Data Explorer, it's recommended to review the basics to understand how to compose requests for the resources you're looking for.

我们将逐步介绍以下高级查询:We'll walk through the following advanced queries:

如果没有 Azure 订阅,请在开始之前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

语言支持Language support

Azure CLI(通过扩展)和 Azure PowerShell(通过模块)支持 Azure 资源图表。Azure CLI (through an extension) and Azure PowerShell (through a module) support Azure Resource Graph. 在运行以下任何查询之前,请检查环境是否已准备就绪。Before running any of the following queries, check that your environment is ready. 有关安装和验证所选 shell 环境的步骤,请参阅 Azure CLIAzure PowerShellSee Azure CLI and Azure PowerShell for steps to install and validate your shell environment of choice.

获取虚拟机规模集容量和大小Get virtual machine scale set capacity and size

此查询将查找虚拟机规模集资源,并获取各种详细信息,包括规模集的虚拟机大小和容量。This query looks for virtual machine scale set resources and gets various details including the virtual machine size and the capacity of the scale set. 此查询使用 toint() 函数将容量强制转换为数字以供排序。The query uses the toint() function to cast the capacity to a number so that it can be sorted. 最后会将列重命名为自定义命名属性。Finally, the columns are renamed into custom named properties.

where type=~ 'microsoft.compute/virtualmachinescalesets'
| where name contains 'contoso'
| project subscriptionId, name, location, resourceGroup, Capacity = toint(sku.capacity), Tier = sku.name
| order by Capacity desc
az graph query -q "where type=~ 'microsoft.compute/virtualmachinescalesets' | where name contains 'contoso' | project subscriptionId, name, location, resourceGroup, Capacity = toint(sku.capacity), Tier = sku.name | order by Capacity desc"
Search-AzGraph -Query "where type=~ 'microsoft.compute/virtualmachinescalesets' | where name contains 'contoso' | project subscriptionId, name, location, resourceGroup, Capacity = toint(sku.capacity), Tier = sku.name | order by Capacity desc"

列出所有标记名称List all tag names

此查询以标记开头,并生成一个 JSON 对象,列出所有唯一标记名称及其对应的类型。This query starts with the tag and builds a JSON object listing all unique tag names and their corresponding types.

project tags
| summarize buildschema(tags)
az graph query -q "project tags | summarize buildschema(tags)"
Search-AzGraph -Query "project tags | summarize buildschema(tags)"

由正则表达式匹配的虚拟机Virtual machines matched by regex

此查询查找与某个正则表达式(称为 regex)匹配的虚拟机。This query looks for virtual machines that match a regular expression (known as regex). 可以使用 matches regex @ 定义要匹配的正则表达式,即 ^Contoso(.*)[0-9]+$The matches regex @ allows us to define the regex to match, which is ^Contoso(.*)[0-9]+$. 该 regex 定义说明如下:That regex definition is explained as:

  • ^ - 匹配项必须以该字符串的开头开头。^ - Match must start at the beginning of the string.
  • Contoso - 区分大小写的字符串。Contoso - The case-sensitive string.
  • (.*) - 子表达式匹配项:(.*) - A subexpression match:
    • . - 匹配任何单一字符(换行符除外)。. - Matches any single character (except a new line).
    • * - 匹配上一个元素零次或多次。* - Matches previous element zero or more times.
  • [0-9] - 数字 0 到 9 的字符组匹配项。[0-9] - Character group match for numbers 0 through 9.
  • + - 匹配上一个元素一次或多次。+ - Matches previous element one or more times.
  • $ - 上一个元素的匹配项必须出现在该字符串的末尾。$ - Match of the previous element must occur at the end of the string.

按名称进行匹配后,查询将对名称进行投影并按名称升序排序。After matching by name, the query projects the name and orders by name ascending.

where type =~ 'microsoft.compute/virtualmachines' and name matches regex @'^Contoso(.*)[0-9]+$'
| project name
| order by name asc
az graph query -q "where type =~ 'microsoft.compute/virtualmachines' and name matches regex @'^Contoso(.*)[0-9]+$' | project name | order by name asc"
Search-AzGraph -Query "where type =~ 'microsoft.compute/virtualmachines' and name matches regex @'^Contoso(.*)[0-9]+$' | project name | order by name asc"

使用 DisplayNames 包括租户和订阅名称Include the tenant and subscription names with DisplayNames

此查询使用新的 Include 参数和选项 DisplayNamessubscriptionDisplayNametenantDisplayName 添加到结果中。This query uses the new Include parameter with option DisplayNames to add subscriptionDisplayName and tenantDisplayName to the results. 此参数仅可用于 Azure CLI 和 Azure PowerShell。This parameter is only available for Azure CLI and Azure PowerShell.

az graph query -q "limit 1" --include displayNames
Search-AzGraph -Query "limit 1" -Include DisplayNames

备注

如果查询未使用 project 指定返回的属性,则 subscriptionDisplayNametenantDisplayName 将自动包括在结果中。If the query doesn't use project to specify the returned properties, subscriptionDisplayName and tenantDisplayName are automatically included in the results. 如果查询确实使用了 project,则每个 DisplayName 字段必须显式包含在 project 中,否则它们将不会在结果中返回,即使使用了 Include 参数也是如此。If the query does use project, each of the DisplayName fields must be explicitly included in the project or they won't be returned in the results, even when the Include parameter is used.

后续步骤Next steps