Επεξεργασία

Κοινή χρήση μέσω


Quickstart: Run Resource Graph query using Azure portal

This quickstart describes how to run an Azure Resource Graph query in the Azure portal using Azure Resource Graph Explorer. Resource Graph Explorer allows you to query information about the Azure Resource Manager resource types and properties. Resource Graph Explorer also provides an interface for working with multiple queries, evaluating the results, and even converting the results of some queries into a chart that can be pinned to an Azure dashboard.

Prerequisites

If you don't have an Azure account, create a free account before you begin.

Run a query

Run a query from the Azure portal using Azure Resource Graph Explorer.

  1. Sign in to the Azure portal.

  2. Search for resource graph and select Resource Graph Explorer.

    Screenshot of the Azure portal to search for resource graph.

  3. If you need to change the scope, select Directory. Then select the directory, management group, or subscription for the resources you want to query.

    Screenshot of the Azure Resource Graph Explorer to change the scope for directory, management group, or subscription.

  4. In the Query 1 portion of the window, copy and paste the following query. Then select Run query.

    resources
    | project name, type
    | limit 5
    

    Screenshot of Azure Resource Graph Explorer that highlights run query, results, and messages.

    This query example doesn't provide a sort modifier like order by. If you run the query multiple times, it might yield a different set of resources for each request.

  5. Review the query response in the Results tab and select the Messages tab to see details about the query, including the count of results and duration of the query. Errors, if any, are displayed in Messages.

  6. Update the query to order by the name property. Then, select Run query

    resources
    | project name, type
    | limit 5
    | order by name asc
    

    Like the previous query, running this query multiple times might yield a different set of resources for each request. The order of the query commands is important. In this example, the order by comes after the limit. The query limits the results to five resources and then orders those results by name.

  7. Update the query to order by the name property and then limit to the top five results. Then, select Run query.

    resources
    | project name, type
    | order by name asc
    | limit 5
    

    If this query is run several times with no changes to your environment, the results are consistent and ordered by the name property, but still limited to five results. The query orders the results by name and then limits the output to five resources.

Schema browser

The schema browser is located in the left pane of Resource Graph Explorer. This list of resources shows all the resource types of Azure resources supported by Azure Resource Graph and that exist in your tenant. Select a resource type or property to show child properties that can be used to create a Resource Graph query.

Select a table name from the schema browser and it gets added to the query. When you select a resource type it gets added to the query, like where type =="<resource type>". If you select a property it gets added to the next line in the query, like where <propertyName> == "INSERT_VALUE_HERE". You can use the schema browser to find properties that you can use in queries. Be sure to replace INSERT_VALUE_HERE with your own value, and adjust the query with conditions, operators, and functions.

This example shows a query that was built from the schema browser by selecting the table authorizationresources with resource type microsoft.authorization/roledefinitions and the property roleName.

authorizationresources
| where type == "microsoft.authorization/roledefinitions"
| where properties['roleName'] == "INSERT_VALUE_HERE"

Screenshot of Azure Resource Graph Explorer schema browser that highlights resource type and properties.

Download query results as a CSV file

To download comma-separated values (CSV) results from the Azure portal, browse to the Azure Resource Graph Explorer and run a query. On the toolbar, select Download as CSV as shown in the following screenshot:

Screenshot of Azure Resource Graph Explorer with Download as CSV highlighted.

When you use the Download as CSV export functionality of Azure Resource Graph Explorer, the result set is limited to 55,000 records. This limitation is a platform limit that can't be overridden by filing an Azure support ticket.

Create a chart from query results

You can create charts from queries that output a count for the number of resources. Queries that output lists can't be made into a chart. If you try to create a chart from a list, a message like the result set isn't compatible with a donut chart visualization is displayed in the Charts tab.

To create a chart from query results, do the following steps:

  1. In the Query 1 portion of the window, enter the following query and select Run query.

    resources
    | where type == "microsoft.compute/virtualmachines"
    | summarize count() by tostring(properties.storageProfile.osDisk.osType)
    
  2. Select the Results tab and note that the response for this query provides counts.

  3. Select the Charts tab. Change the type from Select chart type... to either Bar chart or Donut chart.

    Screenshot of Azure Resource Graph Explorer with charts drop-down menu highlighted.

Pin query visualization to dashboard

When you have results from a query that can be visualized, that data visualization can be pinned to your Azure portal dashboard. After running the previous query, follow these steps:

  1. Select Save and use the name Virtual machine by OS type and type as Private queries. Then select Save at the bottom of the right pane.
  2. Select Run query to rerun the query you saved.
  3. On the Charts tab, select a data visualization. Then select Pin to dashboard.
  4. From Pin to Dashboard select the existing dashboard where you want the chart to appear.
  5. Select Dashboard from the hamburger menu (three horizontal lines) on the top, left side of any portal page.

The query is now available on your dashboard with the title Virtual machine by OS type. If the query wasn't saved before it was pinned, the name is Query 1 instead.

The query and resulting data visualization run and update each time the dashboard loads, providing real time and dynamic insights to your Azure environment directly in your workflow.

Queries that result in a list can also be pinned to the dashboard. The feature isn't limited to data visualizations of queries.

When a query is run from the portal, you can select Directory to change the query's scope for the directory, management group, or subscription of the resources you want to query. When Pin to dashboard is selected, the results are added to your Azure dashboard with the scope used when the query was run.

For more information about working with dashboards, see Create a dashboard in the Azure portal.

Clean up resources

If you want to remove the sample Resource Graph dashboards from your Azure portal environment, do the following steps:

  1. Select Dashboard from the hamburger menu (three horizontal lines) on the top, left side of any portal page.
  2. On your dashboard, find the Virtual machine by OS type chart and select the ellipsis (...) to display the menu.
  3. Select Remove from dashboard select Save to confirm.

If you want to delete saved queries, like Virtual machine by OS type, do the following steps:

  1. Go to Azure Resource Graph Explorer.
  2. Select Open a query.
  3. Select Type Private queries.
  4. From Query name select the rubbish bin icon to Delete this query.
  5. Select Yes to confirm the deletion.

Next steps

In this quickstart, you used Azure Resource Graph Explorer to run a query and reviewed how to use charts and dashboards. To learn more, go to the query language details article.