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

使用 Azure PowerShell 运行首个 Resource Graph 查询Run your first Resource Graph query using Azure PowerShell

使用 Azure Resource Graph 的第一步是查看是否已为 Azure PowerShell 安装了该模块。The first step to using Azure Resource Graph is to check that the module for Azure PowerShell is installed. 本快速入门将指导你完成将该模块添加到 Azure PowerShell 安装的过程。This quickstart walks you through the process of adding the module to your Azure PowerShell installation.

在此过程结束时,应该已将模块添加到所选的 Azure PowerShell 安装中,并运行首个 Resource Graph 查询。At the end of this process, you'll have added the module to your Azure PowerShell 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.

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

添加 Resource Graph 模块Add the Resource Graph module

要使 Azure PowerShell 能够查询 Azure Resource Graph,则必须添加该模块。To enable Azure PowerShell to query Azure Resource Graph, the module must be added. 此模块可以与在本地安装的 PowerShell 以及 Azure Cloud Shell 一起使用,也可以与 Azure PowerShell Docker 映像一起使用。This module can be used with locally installed PowerShell, with Azure Cloud Shell, or with the Azure PowerShell Docker image.

基本要求Base requirements

Azure Resource Graph 模块需要以下软件:The Azure Resource Graph module requires the following software:

  • Azure PowerShell 1.0.0 或更高版本。Azure PowerShell 1.0.0 or higher. 若尚未安装,请遵循这些说明If it isn't yet installed, follow these instructions.

  • PowerShellGet 2.0.1 或更高版本。PowerShellGet 2.0.1 or higher. 若尚未安装或更新,请遵循这些说明If it isn't installed or updated, follow these instructions.

安装模块Install the module

适用于 PowerShell 的 Resource Graph 模块是 Az.ResourceGraphThe Resource Graph module for PowerShell is Az.ResourceGraph.

  1. 从管理 PowerShell 提示符运行以下命令:From an administrative PowerShell prompt, run the following command:

    # Install the Resource Graph module from PowerShell Gallery
    Install-Module -Name Az.ResourceGraph
    
  2. 验证是否已导入该模块且是否为正确版本 (0.7.1):Validate that the module has been imported and is the correct version (0.7.1):

    # Get a list of commands for the imported Az.ResourceGraph module
    Get-Command -Module 'Az.ResourceGraph' -CommandType 'Cmdlet'
    

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

将 Azure PowerShell 模块添加到所选环境中后,即可尝试一个简单的 Resource Graph 查询。With the Azure PowerShell module 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. 使用 Search-AzGraph cmdlet 运行首个 Azure Resource Graph 查询:Run your first Azure Resource Graph query using the Search-AzGraph cmdlet:

    # Login first with Connect-AzAccount if not using Cloud Shell
    
    # Run Azure Resource Graph query
    Search-AzGraph -Query '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'
    Search-AzGraph -Query '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`
    Search-AzGraph -Query '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 PowerShell 环境中删除 Resource Graph 模块,可使用以下命令:If you wish to remove the Resource Graph module from your Azure PowerShell environment, you can do so by using the following command:

# Remove the Resource Graph module from the Azure PowerShell environment
Remove-Module -Name 'Az.ResourceGraph'

备注

该操作不会删除之前下载的模块文件。This does not delete the module file downloaded earlier. 只会从正在运行的 PowerShell 会话中将其删除。It only removes it from the running PowerShell session.

后续步骤Next steps