快速入门:使用 ARM 模板创建共享查询Quickstart: Create a shared query by using an ARM template

可以将 Resource Graph 查询另存为专用查询或共享查询。 Resource Graph queries can be saved as a private query or a shared query. 专用查询保存到个人门户配置文件中,不显示给其他人。A private query is saved to the individuals portal profile and isn't visible to others. 共享查询是一个资源管理器对象,可通过权限和基于角色的访问权限与他人共享。A shared query is a Resource Manager object that can be shared with others through permissions and role-based access. 可以通过共享查询以通用且一致的方式执行资源发现操作。A shared query provides common and consistent execution of resource discovery. 本快速入门使用 Azure 资源管理器模板(ARM 模板)来创建共享查询。This quickstart uses an Azure Resource Manager template (ARM template) to create a shared query.

ARM 模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。An ARM template is a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for your project. 该模板使用声明性语法,使你可以声明要部署的内容,而不需要编写一系列编程命令来进行创建。The template uses declarative syntax, which lets you state what you intend to deploy without having to write the sequence of programming commands to create it.

如果你的环境满足先决条件,并且你熟悉如何使用 ARM 模板,请选择“部署到 Azure”按钮。If your environment meets the prerequisites and you're familiar with using ARM templates, select the Deploy to Azure button. Azure 门户中会打开模板。The template will open in the Azure portal.

部署用于创建 Azure 共享查询的 ARM 模板

先决条件Prerequisites

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

查看模板Review the template

在本快速入门中,我们创建一个名为“按 OS 进行 VM 计数”的共享查询。In this quickstart, you create a shared query called Count VMs by OS. 若要使用 Resource Graph 资源管理器在 SDK 或门户中尝试此查询,请参阅示例 - 按 OS 类型对虚拟机进行计数To try this query in SDK or in portal with Resource Graph Explorer, see Samples - Count virtual machines by OS type.

本快速入门中使用的模板来自 Azure 快速启动模板The template used in this quickstart is from Azure Quickstart Templates.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "queryName": {
            "defaultValue": "Count VMs by OS",
            "type": "String",
            "metadata": {
                "description": "The name of the shared query."
            }
        },
        "queryCode": {
            "defaultValue": "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by tostring(properties.storageProfile.osDisk.osType)",
            "type": "String",
            "metadata": {
                "description": "The Azure Resource Graph query to be saved to the shared query."
            }
        },
        "queryDescription": {
            "defaultValue": "This shared query counts all virtual machine resources and summarizes by the OS type.",
            "type": "String",
            "metadata": {
                "description": "The description of the saved Azure Resource Graph query."
            }
        }
    },
    "resources": [
        {
            "type": "microsoft.resourcegraph/queries",
            "apiVersion": "2018-09-01-preview",
            "name": "[parameters('queryName')]",
            "location": "global",
            "properties": {
                "query": "[parameters('queryCode')]",
                "description": "[parameters('queryDescription')]"
            }
        }
    ]
}

该模板中定义了以下资源:The resource defined in the template is:

部署模板Deploy the template

备注

Azure Resource Graph 服务免费。Azure Resource Graph service is free. 有关详细信息,请参阅 Azure Resource Graph 概述For more information, see Overview of Azure Resource Graph.

  1. 选择下图登录到 Azure 门户并打开模板:Select the following image to sign in to the Azure portal and open the template:

    部署用于创建 Azure 共享查询的 ARM 模板

  2. 选择或输入以下值:Select or enter the following values:

    名称Name Value
    订阅Subscription 选择 Azure 订阅。Select your Azure subscription.
    资源组Resource group 选择“新建”,指定名称,然后选择“确定”。 Select Create new, specify a name, and then select OK.
    位置Location 选择区域。Select a region. 例如“中国东部”。For example, China East.
    查询名称Query name 保留默认值“按 OS 进行 VM 计数”。Leave the default value Count VMs by OS.
    查询代码Query code 保留默认值 Resources | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by tostring(properties.storageProfile.osDisk.osType)Leave the default value Resources | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by tostring(properties.storageProfile.osDisk.osType)
    查询说明Query description 保留默认值“此共享查询对所有虚拟机资源计数,并按 OS 类型进行汇总。”Leave the default value This shared query counts all virtual machine resources and summarizes by the OS type.
    我同意上述条款和条件I agree to the terms and conditions stated above (选择)(Select)
  3. 选择“购买”。Select Purchase.

其他某些资源:Some additional resources:

验证部署Validate the deployment

若要运行新的共享查询,请执行以下步骤:To run the new shared query, follow these steps:

  1. 在门户搜索栏中,搜索“Resource Graph 查询”并将其选中。From the portal search bar, search for Resource Graph queries and select it.

  2. 选择名为“按 OS 进行 VM 计数”的共享查询,然后在“概览”页上选择“结果”选项卡。 Select the shared query named Count VMs by OS, then select the Results tab on the Overview page.

也可在 Resource Graph 资源管理器中打开共享查询:Alternatively, the shared query can be opened from Resource Graph Explorer:

  1. 在门户搜索栏中,搜索“Resource Graph 资源管理器”并将其选中。From the portal search bar, search for Resource Graph Explorer and select it.

  2. 选择“打开查询”按钮。Select the Open a query button.

  3. 将“类型”更改为“共享查询”。Change Type to Shared queries. 如果在列表中看不到“按 OS 进行 VM 计数”,请使用筛选器框来限制结果。If you don't see the Count VMs by OS in the list, use the filter box to limit the results. 当“按 OS 进行 VM 计数”共享查询可见后,请选择其名称。Once the Count VMs by OS shared query is visible, select its name.

  4. 加载查询后,选择“运行查询”按钮。Once the query is loaded, select the Run query button. 结果将显示在下面的“结果”选项卡中。Results are displayed in the Results tab below.

清理资源Clean up resources

若要删除创建的共享查询,请执行以下步骤:To remove the shared query created, follow these steps:

  1. 在门户搜索栏中,搜索“Resource Graph 查询”并将其选中。From the portal search bar, search for Resource Graph queries and select it.

  2. 选中名为“按 OS 进行 VM 计数”的共享查询旁边的复选框。Set the check box next to the shared query named Count VMs by OS.

  3. 选择页面顶部的“删除”按钮。Select the Delete button along the top of the page.

后续步骤Next steps

在本快速入门中,你已创建一个 Resource Graph 共享查询。In this quickstart, you created a Resource Graph shared query.

若要了解有关共享查询的详细信息,请继续学习以下内容的教程:To learn more about shared queries, continue to the tutorial for: