快速入门:使用 Azure PowerShell 创建 Resource Graph 共享查询Quickstart: Create a Resource Graph shared query using Azure PowerShell

本文介绍如何使用 Az.ResourceGraph PowerShell 模块创建 Azure Resource Graph 共享查询。This article describes how you can create an Azure Resource Graph shared query using the Az.ResourceGraph PowerShell module.

先决条件Prerequisites

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

重要

尽管 Az.ResourceGraph PowerShell 模块为预览版,但必须使用 Install-Module cmdlet 单独安装它。While the Az.ResourceGraph PowerShell module is in preview, you must install it separately using the Install-Module cmdlet.

Install-Module -Name Az.ResourceGraph
  • 如果有多个 Azure 订阅,请选择应当计费的资源所在的相应订阅。If you have multiple Azure subscriptions, choose the appropriate subscription in which the resources should be billed. 使用 Set-AzContext cmdlet 选择特定订阅。Select a specific subscription using the Set-AzContext cmdlet.

    Set-AzContext -SubscriptionId 00000000-0000-0000-0000-000000000000
    

创建 Resource Graph 共享查询Create a Resource Graph shared query

将 Az.ResourceGraph PowerShell 模块添加到所选环境中后,即可创建一个 Resource Graph 共享查询。With the Az.ResourceGraph PowerShell module added to your environment of choice, it's time to create a Resource Graph shared query. 共享查询是一个 Azure 资源管理器对象,你可授予该对象权限或在 Azure Resource Graph Explorer 中运行该对象。The shared query is an Azure Resource Manager object that you can grant permission to or run in Azure Resource Graph Explorer. 该查询汇总了按“位置”分组的所有资源。The query summarizes the count of all resources grouped by location.

  1. 使用 New-AzResourceGroup 创建资源组,以存储 Azure Resource Graph 共享查询。Create a resource group with New-AzResourceGroup to store the Azure Resource Graph shared query. 此资源组名为 resource-graph-queries,并位于 chinanorth2This resource group is named resource-graph-queries and the location is chinanorth2.

    # Login first with `Connect-AzAccount -Environment AzureChinaCloud` if not using local Shell
    
    # Create the resource group
    New-AzResourceGroup -Name resource-graph-queries -Location chinanorth2
    
  2. 使用 Az.ResourceGraph PowerShell 模块和 New-AzResourceGraphQuery cmdlet 创建 Azure Resource Graph 共享查询:Create the Azure Resource Graph shared query using the Az.ResourceGraph PowerShell module and New-AzResourceGraphQuery cmdlet:

    # Create the Azure Resource Graph shared query
    $Params = @{
      Name = 'Summarize resources by location'
      ResourceGroupName = 'resource-graph-queries'
      Location = 'chinanorth2'
      Description = 'This shared query summarizes resources by location for a pinnable map graphic.'
      Query = 'Resources | summarize count() by location'
    }
    New-AzResourceGraphQuery @Params
    
  3. 列出新资源组中的共享查询。List the shared queries in the new resource group. Get-AzResourceGraphQuery cmdlet 返回一组值。The Get-AzResourceGraphQuery cmdlet returns an array of values.

    # List all the Azure Resource Graph shared queries in a resource group
    Get-AzResourceGraphQuery -ResourceGroupName resource-graph-queries
    
  4. 若要仅获取单个共享查询结果,请使用 Get-AzResourceGraphQuery 及其 Name 参数。To get just a single shared query result, use Get-AzResourceGraphQuery with its Name parameter.

    # Show a specific Azure Resource Graph shared query
    Get-AzResourceGraphQuery -ResourceGroupName resource-graph-queries -Name 'Summarize resources by location'
    

清理资源Clean up resources

如果要从 Azure 环境中删除 Resource Graph 共享查询和资源组,可以使用以下命令执行此操作:If you wish to remove the Resource Graph shared query and resource group from your Azure environment, you can do so by using the following commands:

# Delete the Azure Resource Graph shared query
Remove-AzResourceGraphQuery -ResourceGroupName resource-graph-queries -Name 'Summarize resources by location'

# Remove the resource group
# WARNING: This command deletes ALL resources you've added to this resource group
Remove-AzResourceGroup -Name resource-graph-queries

后续步骤Next steps

在本快速入门中,你已使用 Azure PowerShell 创建了一个 Resource Graph 共享查询。In this quickstart, you've created a Resource Graph shared query using Azure PowerShell. 若要详细了解 Resource Graph 语言,请继续阅读查询语言详细信息页。To learn more about the Resource Graph language, continue to the query language details page.