快速入门:使用 Azure PowerShell 创建 Resource Graph 共享查询

本文介绍如何使用 Az.ResourceGraph PowerShell 模块创建 Azure Resource Graph 共享查询。

先决条件

  • 如果没有 Azure 订阅,请在开始前创建一个试用版订阅帐户。

重要

尽管 Az.ResourceGraph PowerShell 模块为预览版,但必须使用 Install-Module cmdlet 单独安装它。

Install-Module -Name Az.ResourceGraph -Scope CurrentUser -Repository PSGallery -Force
  • 如果有多个 Azure 订阅,请选择应当计费的资源所在的相应订阅。 使用 Set-AzContext cmdlet 选择特定订阅。

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

创建 Resource Graph 共享查询

将 Az.ResourceGraph PowerShell 模块添加到所选环境中后,即可创建一个 Resource Graph 共享查询。 共享查询是一个 Azure 资源管理器对象,你可授予该对象权限或在 Azure Resource Graph Explorer 中运行该对象。 该查询汇总了按“位置”分组的所有资源。

  1. 使用 New-AzResourceGroup 创建资源组,以存储 Azure Resource Graph 共享查询。 此资源组名为 resource-graph-queries,并位于 chinanorth2

    # Login first with `Connect-AzAccount -Environment AzureChinaCloud`
    Connect-AzAccount -Environment AzureChinaCloud
    
    # 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
    $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. 列出新资源组中的共享查询。 Get-AzResourceGraphQuery cmdlet 返回一组值。

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

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

清理资源

如果要从 Azure 环境中删除 Resource Graph 共享查询和资源组,可以使用以下命令执行此操作:

# 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

后续步骤

在本快速入门中,你已使用 Azure PowerShell 创建了一个 Resource Graph 共享查询。 若要详细了解 Resource Graph 语言,请继续阅读查询语言详细信息页。