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

快速入门:使用 Azure PowerShell 运行首个 Resource Graph 查询Quickstart: 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.

添加 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 一起使用,也可以与 PowerShell Docker 映像一起使用。This module can be used with locally installed PowerShell, with Azure Cloud Shell, or with the 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.5):Validate that the module has been imported and is the latest version (0.7.5):

    # 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.

备注

如果查询未从你已有权访问的订阅返回结果,请注意 Search-AzGraph cmdlet 默认为默认上下文中的订阅。If the query does not return results from a subscription you already have access to, then note that Search-AzGraph cmdlet defaults to subscriptions in the default context. 若要查看作为默认上下文一部分的订阅 ID 列表,请运行此 (Get-AzContext).Account.ExtendedProperties.Subscriptions。如果你希望搜索你有权访问的所有订阅,可以通过运行 $PSDefaultParameterValues=@{"Search-AzGraph:Subscription"= $(Get-AzSubscription).ID}Search-AzGraph cmdlet 设置 PSDefaultParameterValuesTo see the list of subscription IDs which are part of the default context run this (Get-AzContext).Account.ExtendedProperties.Subscriptions If you wish to search across all the subscriptions you have access to, one can set the PSDefaultParameterValues for Search-AzGraph cmdlet by running $PSDefaultParameterValues=@{"Search-AzGraph:Subscription"= $(Get-AzSubscription).ID}

清理资源Clean up resources

若希望从 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 current session
Remove-Module -Name 'Az.ResourceGraph'

# Uninstall the Resource Graph module from the environment
Uninstall-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