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

使用 Azure CLI 运行你的第一个 Resource Graph 查询Run your first Resource Graph query using Azure CLI

使用 Azure Resource Graph 的第一步是确保为 Azure CLI 安装了该扩展。The first step to using Azure Resource Graph is to check that the extension for Azure CLI is installed. 本快速入门将指导你完成将该扩展添加到 Azure CLI 安装的过程。This quickstart walks you through the process of adding the extension to your Azure CLI installation. 可以通过安装在本地的 Azure CLI 或通过 Azure Cloud Shell 使用该扩展。You can use the extension with Azure CLI installed locally or through the Azure Cloud Shell.

在此过程结束时,你已将该扩展添加到所选的 Azure CLI 安装中,并将运行你的第一个 Resource Graph 查询。At the end of this process, you'll have added the extension to your Azure CLI installation of choice and run your first Resource Graph query.

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

添加 Resource Graph 扩展Add the Resource Graph extension

若要使 Azure CLI 能够查询 Azure Resource Graph,则必须添加该扩展。To enable Azure CLI to query Azure Resource Graph, the extension must be added. 此扩展适用于可以使用 Azure CLI 的任何位置,包括 Windows 10 上的 bashCloud Shell(独立的或位于门户内的)、Azure CLI Docker 映像,也适用于在本地安装了 Azure CLI 的任何位置。This extension works wherever Azure CLI can be used, including bash on Windows 10, Cloud Shell (both standalone and inside the portal), the Azure CLI Docker image, or locally installed.

  1. 请确保安装最新的 Azure CLI(至少为 2.0.45)。Check that the latest Azure CLI is installed (at least 2.0.45). 若尚未安装,请遵循这些说明If it isn't yet installed, follow these instructions.

  2. 在所选的 Azure CLI 环境中,使用以下命令导入该扩展:In your Azure CLI environment of choice, import it with the following command:

    # Add the Resource Graph extension to the Azure CLI environment
    az extension add --name resource-graph
    
  3. 验证该扩展是否已安装以及是否为预期的版本(至少为 0.1.7):Validate that the extension has been installed and is the expected version (at least 0.1.7):

    # Check the extension list (note that you may have other extensions installed)
    az extension list
    
    # Run help for graph query options
    az graph query -h
    

运行首个 Resource Graph 查询Run your first Resource Graph query

将 Azure CLI 扩展添加到所选环境中后,即可尝试一个简单的 Resource Graph 查询。With the Azure CLI extension added to your environment of choice, it's time to try out a simple Resource Graph query. 该查询将返回前五个 Azure 资源,以及每个资源的名称和资源类型。The query will return the first five Azure resources with the Name and Resource Type of each resource.

  1. 使用 graph 扩展和 query 命令运行你的第一个 Azure Resource Graph 查询:Run your first Azure Resource Graph query using the graph extension and query command:

    # Login first with az login if not using Cloud Shell
    
    # Run Azure Resource Graph query
    az graph query -q 'project name, type | limit 5'
    

    备注

    由于此查询示例未提供排序修饰符(例如 order by),因此多次运行此查询可能会为每个请求生成一组不同的资源。As this query example does not provide a sort modifier such as order by, running this query multiple times is likely to yield a different set of resources per request.

  2. 将查询更新为 order by Name 属性:Update the query to order by the Name property:

    # Run Azure Resource Graph query with 'order by'
    az graph query -q 'project name, type | limit 5 | order by name asc'
    

    备注

    与第一个查询一样,多次运行此查询可能会为每个请求生成一组不同的资源。Just as with the first query, running this query multiple times is likely to yield a different set of resources per request. 查询命令的顺序非常重要。The order of the query commands is important. 在本例中,order by 位于 limit 之后。In this example, the order by comes after the limit. 这将首先限制查询结果,然后对其进行排序。This will first limit the query results and then order them.

  3. 将查询更新为先 order by Name 属性,然后再 limit 为前五个结果:Update the query to first order by the Name property and then limit to the top five results:

    # Run Azure Resource Graph query with `order by` first, then with `limit`
    az graph query -q 'project name, type | order by name asc | limit 5'
    

假设环境中没有任何变化,则多次运行最后一个查询时,返回的结果将是一致的且符合预期的 -- 按 Name 属性排序,但仍限制为前五个结果。When the final query is run several times, assuming that nothing in your environment is changing, the results returned will be consistent and as expected -- ordered by the Name property, but still limited to the top five results.

清理Cleanup

如果希望从 Azure CLI 环境中删除 Resource Graph 扩展,可使用以下命令:If you wish to remove the Resource Graph extension from your Azure CLI environment, you can do so by using the following command:

# Remove the Resource Graph extension from the Azure CLI environment
az extension remove -n resource-graph

备注

这不会删除之前下载的扩展文件。This does not delete the extension file downloaded earlier. 只会从正在运行的 Azure CLI 环境中将其删除。It only removes it from the running Azure CLI environment.

后续步骤Next steps