教程:在 Azure 门户中创建和共享 Azure Resource Graph 查询Tutorial: Create and share an Azure Resource Graph query in the Azure portal

使用 Azure Resource Graph 资源管理器可以直接在 Azure 门户中保存 Resource Graph 查询。Azure Resource Graph Explorer lets you save your Resource Graph queries directly in the Azure portal. 查询有两种:专用_和_共享There are two types of queries: Private and Shared. 专用查询保存在 Azure 门户设置中。A Private query is saved in your Azure portal settings. 而共享查询是 Azure 资源管理器资源,可以使用 Azure 基于角色的访问控制 (Azure RBAC) 进行管理,并使用资源锁进行保护。Whereas a Shared query is a Azure Resource Manager resource that can be managed with Azure role-based access control (Azure RBAC) and protected with resource locks. 这两种类型的查询都是静态加密的。Both types of queries are encrypted at rest.

通过将查询保存在 Azure 门户中,可以在查找收藏夹查询或常用查询时节省时间。By saving queries in the Azure portal, you save the time you might otherwise spend looking for your favorite or commonly used queries. 共享查询时,可以通过重复操作来帮助你的团队实现一致性和高效的目标。When you share queries, you help your team realize goals of consistency and efficiency through repetition.

在本教程中,你将完成以下任务:In this tutorial, you'll complete the following tasks:

  • 创建和删除专用查询Create and delete a Private query
  • 创建共享查询Create a Shared query
  • 发现共享查询Discover Shared queries
  • 删除共享查询Delete a Shared query

先决条件Prerequisites

需要一个 Azure 订阅才能完成此教程。To complete this tutorial, you need an Azure subscription. 如果没有订阅,请在开始之前创建一个试用帐户If you don't have one, create a trial account before you begin.

创建和删除专用查询Create and delete a Private query

专用查询仅供创建它们的帐户访问和可见。Private queries are accessible and visible only to the account that creates them. 由于这些查询保存在帐户的 Azure 门户设置中,因此只能在 Azure 门户中创建、使用和删除这些查询。As they're saved in an account's Azure portal settings, they can be created, used, and deleted only from inside the Azure portal. 专用查询不是资源管理器资源。A Private query isn't a Resource Manager resource. 若要创建新的专用查询,请执行以下步骤:To create a new Private query, follow these steps:

  1. 从门户菜单中,选择“所有服务”或使用所有页面顶部的 Azure 搜索框。From the portal menu, select All services or use the Azure search box at the top of all pages. 搜索并选择 Resource Graph 资源管理器。Search for and then select Resource Graph Explorer.

  2. 在“Azure Resource Graph 资源管理器”页的“查询 1”选项卡上,输入以下查询:On the Query 1 tab on the Azure Resource Graph Explorer page, enter the following query:

    Resources
    | where type =~ 'Microsoft.Compute/virtualMachines'
    | summarize count() by tostring(properties.storageProfile.osDisk.osType)
    

    选择“运行查询”以在底部窗格中查看查询结果。Select Run query to see the query results in the bottom pane.

    有关此查询的详细信息,请参阅示例 – 按 OS 类型对虚拟机进行计数For more information about this query, see Samples – Count virtual machines by OS type.

  3. 选择“保存”或“另存为”,输入“按 OS 对 VM 进行计数”作为名称,将类型保留为“专用查询”,然后选择“保存查询”窗格底部的“保存” 。Select Save or Save as, enter Count VMs by OS as the name, leave the type as Private query, and then select Save at the bottom of the Save query pane. 选项卡标题从“查询 1”更改为“按 OS 对 VM 进行计数” 。The tab title changes from Query 1 to Count VMs by OS.

  4. 离开 Azure 门户中的 Azure Resource Graph 资源管理器,然后返回到该资源管理器。Move away from Azure Resource Graph Explorer in the Azure portal and then return to it. 请注意,不再显示已保存的查询,并且已返回“查询 1”选项卡。Notice that the saved query is no longer displayed and the Query 1 tab has returned.

  5. 选择“打开查询”。Select Open a query. 确保该类型为“专用查询”。Make sure that the type is Private query. 已保存的名称“按 OS 对 VM 进行计数”现在会显示在“查询名称”列表中。The saved name Count VMs by OS now appears in the Query Name list. 选择已保存查询的标题链接后,会将其加载到具有该查询的名称的新选项卡中。When you select the title link of the saved query, it's loaded into a new tab with that query's name.

    备注

    当已保存的查询处于打开状态且选项卡显示其名称时,选择“保存”按钮将使用所做的任何更改来更新它。When a saved query is open and the tab shows its name, selecting the Save button updates it with any changes that have been made. 若要从这一打开的查询中创建新保存的查询,请选择“另存为”并继续操作,就像保存全新的查询一样。To create a new saved query from this open query, select Save as and proceed as if you were saving a brand new query.

  6. 若要删除已保存的查询,请再次选择“打开查询”,并验证“类型”字段是否设置为“专用查询”。To delete the saved query, select Open a query again, and verify that the Type field is set to Private query. 在已保存的 Count VMs by OS 查询的行上,选择“删除”(回收站图标)。On the row of the saved Count VMs by OS query, select Delete (Recycle bin icon). 在确认对话框中,选择“是”以完成查询删除。In the confirmation dialog box, select Yes to finish deleting the query. 然后,关闭“打开查询”窗格。Then, close the Open a query pane.

创建共享查询Create a Shared query

与专用查询不同,共享查询是资源管理器资源。Unlike a Private query, a Shared query is a Resource Manager resource. 这种情况意味着,这些查询保存到资源组,可以使用 RBAC 进行管理和控制,甚至可以使用资源锁进行保护。This fact means the query gets saved to a resource group, can be managed and controlled with Azure RBAC, and can even be protected with resource locks. 作为资源,具有相应权限的任何人都可以查看和使用该资源。As a resource, anyone who has the appropriate permissions can see and use it. 若要创建新的共享查询,请执行以下步骤:To create a new Shared query, follow these steps:

  1. 从门户菜单中,选择“所有服务”或使用所有页面顶部的 Azure 搜索框搜索并选择“Resource Graph 资源管理器”。From the portal menu, select All services, or use the Azure search box at the top of all pages to search for and select Resource Graph Explorer.

  2. 在“Azure Resource Graph 资源管理器”页的“查询 1”选项卡上,输入以下查询:On the Query 1 tab on the Azure Resource Graph Explorer page, enter the following query:

    Resources
    | where type =~ 'Microsoft.Compute/virtualMachines'
    | summarize count() by tostring(properties.storageProfile.osDisk.osType)
    

    选择“运行查询”以在底部窗格中查看查询结果。Select Run query to see the query results in the bottom pane.

    有关此查询的详细信息,请参阅示例 – 按 OS 类型对虚拟机进行计数For more information about this query, see Samples – Count virtual machines by OS type.

  3. 选择“保存”或“另存为” 。Select Save or Save as.

    使用“保存”按钮保存新查询

  4. 在“保存查询”窗格中,输入“按 OS 对 VM 进行计数”作为名称。In the Save query pane, enter Count VMs by OS for the name.

  5. 将类型更改为“共享查询”,将描述设置为“按 OS 类型对虚拟机进行计数”,并将“订阅”设置为指定查询资源的创建位置。Change the type to Shared query, set the description to Count of virtual machines by OS type, and set Subscription to specify where the query resource gets created.

  6. 保持选中“发布到 resource-graph-queries 资源组”复选框,并将“资源组位置”设置为“中国东部”************。Leave the Publish to resource-graph-queries resource group check box selected and the Resource Group location set to China East.

  7. 在“保存查询”窗格底部选择“保存”。 Select Save at the bottom of the Save query pane. 选项卡标题从“查询 1”更改为“按 OS 对 VM 进行计数” 。The tab title changes from Query 1 to Count VMs by OS. 第一次使用 resource-graph-queries 资源组时,创建资源组后保存所需的时间比预期时间更长。The first time the resource-graph-queries resource group is used, the save takes longer than expected as the resource group gets created.

    将新查询另存为共享查询

    备注

    如果要提供保存共享查询的现有资源组的名称,则可以清除“发布到 resource-graph-queries 资源组”复选框。You can clear the Publish to resource-graph-queries resource group check box if you want to provide the name of an existing resource group to save the shared query into. 使用查询的默认命名资源组可以更轻松地发现共享查询。Using the default named resource group for queries makes Shared queries easier to discover. 它还使该资源组的用途更加明显。It also makes the purpose of that resource group more apparent. 但出于安全原因,可以根据现有权限选择现有资源组。However, you might opt to select an existing resource group for security reasons based on existing permissions.

  8. 离开 Azure 门户中的 Azure Resource Graph 资源管理器,然后返回到该资源管理器。Move away from Azure Resource Graph Explorer in the Azure portal and then return to it. 请注意,不再显示已保存的查询,并且已返回“查询 1”选项卡。Notice that the saved query is no longer displayed and the Query 1 tab has returned.

  9. 选择“打开查询”。Select Open a query. 验证类型是否设置为“共享查询”,以及“订阅”和“资源组”的组合是否与查询的保存位置匹配 。Verify that the type is set to Shared query and the combination of Subscription and Resource group match where you saved the query. 已保存“按 OS 对 VM 进行计数”项现在会显示在“查询名称”列表中。The saved Count VMs by OS item now appears in the Query Name list. 选择已保存查询的标题链接以将其加载到具有该查询的名称的新选项卡中。Select the title link of the saved query to load it into a new tab with that query's name. 作为共享查询,将在标题旁边的选项卡中显示一个图标,表示它是共享查询。As a Shared query, it displays an icon in the tab next to the title, denoting it as shared.

    显示带有图标的共享查询

    备注

    当已保存的查询处于打开状态且选项卡显示其名称时,“保存”按钮将使用所做的任何更改来更新它。When a saved query is open and the tab shows its name, the Save button updates it with any changes that have been made. 若要创建新保存的查询,请选择“另存为”并继续操作,就像保存全新的查询一样。To create a new saved query, select Save as and proceed as if you were saving a brand new query.

发现共享查询Discover Shared queries

由于共享查询是资源管理器资源,因此有多种方法可进行查找:Because a Shared query is a Resource Manager resource, there are several ways to find one:

  • 从 Resource Graph 资源管理器中,选择“打开查询”并将类型设置为“共享查询” 。From Resource Graph Explorer, select Open a query and set the type to Shared query.
  • 通过 Resource Graph 查询门户页。From the Resource Graph queries portal page.
  • 通过保存共享查询的资源组。From the resource group that the Shared query was saved in.
  • 通过查询 Resource Graph。Through a query to Resource Graph.

查看 Resource Graph 查询View Resource Graph queries

在 Azure 门户中,Resource Graph 查询页显示已登录帐户有权访问的共享查询。In the Azure portal, the Resource Graph queries page displays Shared queries that the logged-in account has access to. 此页支持按名称、订阅、资源组和 Resource Graph 查询的其他属性进行筛选。This page enables filtering by name, subscription, resource group, and other properties of the Resource Graph query. 还可以使用此界面标记、导出和删除 Resource Graph 查询。You can also tag, export, and delete Resource Graph queries by using this interface.

选择其中一个查询将打开 Resource Graph 查询页。Selecting one of the queries opens the Resource Graph query page. 与其他资源管理器资源一样,此页提供交互式概述以及活动日志、访问控制和标记。Like other Resource Manager resources, this page offers an interactive overview along with the Activity log, access control, and tags. 还可以直接从此页应用资源锁。You can also apply a resource lock directly from this page.

通过从门户菜单中选择“所有服务”或使用所有页面顶部的 Azure 搜索框,转到 Resource Graph 查询页。Get to the Resource Graph queries page from the portal menu by selecting All services or by using the Azure search box at the top of all pages. 搜索并选择 Resource Graph ExplorerSearch for and select Resource Graph Explorer.

列出资源组资源List Resource groups resources

Resource Graph 查询与作为资源组一部分的其他资源一起列出。The Resource Graph query is listed alongside other resources that are part of a resource group. 选择 Resource Graph 查询可打开该查询的页面。Selecting the Resource Graph query opens the page for that query. 省略号和快捷菜单选项(通过右键单击触发)的工作方式与 Resource Graph 查询页上的工作方式相同。The ellipsis and shortcut menu options (triggered by right-clicking) work the same as on the Resource Graph query page.

查询 Resource GraphQuery Resource Graph

可以通过查询 Resource Graph 来查找 Resource Graph 查询。You can find Resource Graph queries through a query to Resource Graph. 以下 Resource Graph 查询按类型 Microsoft.ResourceGraph/queries 进行限制,然后使用 project 仅列出名称、修改时间和查询本身:The following Resource Graph query limits by type Microsoft.ResourceGraph/queries, and then uses project to list only the name, time modified, and the query itself:

Resources
| where type == "microsoft.resourcegraph/queries"
| project name, properties.timeModified, properties.query

运行共享查询Run a shared query

可使用 {{shared-query-uri}} 语法(预览版)运行 Resource Graph 共享查询。A Resource Graph shared query can be run with the {{shared-query-uri}} syntax (preview). 有关更多详细信息,请参阅共享查询语法For more information, see Shared query syntax.

删除共享查询Delete a Shared query

如果不再需要共享查询,请将其删除。If a Shared query is no longer needed, delete it. 删除共享查询后,将删除相应的资源管理器资源。By deleting a Shared query, you remove the corresponding Resource Manager resource. 结果图表固定到的任何仪表板现在都会显示错误消息。Any dashboards that the results chart was pinned to now display an error message. 显示该错误消息后,请使用“从仪表板中删除”按钮清除仪表板。When that error message is displayed, use the Remove from dashboard button to clean up your dashboard.

可以通过以下界面删除共享查询:You can delete a Shared query through the following interfaces:

  • Resource Graph 查询页Resource Graph queries page
  • Resource Graph 查询页Resource Graph query page
  • Resource Graph 资源管理器中的“打开查询”页The Open a query page in Resource Graph Explorer
  • 资源组页Resource groups page

清理资源Clean up resources

完成本教程后,如果不再需要,请删除创建的专用查询和共享查询。When you're finished with this tutorial, delete the Private and Shared queries you created if you no longer want them.

后续步骤Next steps

在本教程中,你已创建专用查询和共享查询。In this tutorial, you've created Private and Shared queries. 若要详细了解 Resource Graph 语言,请继续阅读查询语言详细信息页。To learn more about the Resource graph language, continue to the query language details page.