Quickstart: Run Resource Graph query using Azure CLI
This quickstart describes how to run an Azure Resource Graph query using the extension for Azure CLI. The article also shows how to order (sort) and limit the query's results. You can run a query for resources in your tenant, management groups, or subscriptions. When you're finished, you can remove the extension.
Prerequisites
- If you don't have an Azure account, create a free account before you begin.
- Azure CLI must be version 2.22.0 or higher for the Resource Graph extension.
- Visual Studio Code.
Connect to Azure
From a Visual Studio Code terminal session, connect to Azure. If you have more than one subscription, run the commands to set context to your subscription. Replace <subscriptionID>
with your Azure subscription ID.
az login
# Run these commands if you have multiple subscriptions
az account list --output table
az account set --subscription <subscriptionID>
Install the extension
To enable Azure CLI to query resources using Azure Resource Graph, the Resource Graph extension must be installed. You can manually install the extension with the following steps. Otherwise, the first time you run a query with az graph
you're prompted to install the extension.
List the available extensions and versions:
az extension list-available --output table
Install the extension:
az extension add --name resource-graph
Verify the extension was installed:
az extension list --output table
Display the extension's syntax:
az graph query --help
For more information about Azure CLI extensions, go to Use and manage extensions with the Azure CLI.
Run a query
After the Azure CLI extension is added to your environment, you can run a tenant-based query. The query in this example returns five Azure resources with the name
and type
of each resource. To query by management group or subscription, use the --management-groups
or --subscriptions
arguments.
Run an Azure Resource Graph query:
az graph query --graph-query 'Resources | project name, type | limit 5'
This query example doesn't use a sort modifier like
order by
. If you run the query multiple times, it might yield a different set of resources for each request.Update the query to
order by
thename
property:az graph query --graph-query 'Resources | project name, type | limit 5 | order by name asc'
Like the previous query, if you run this query multiple times it 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 thelimit
. The query limits the results to five resources and then orders those results by name.Update the query to
order by
thename
property and thenlimit
the output to five results:az graph query --graph-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.
Clean up resources
To remove the Resource Graph extension, run the following command:
az extension remove --name resource-graph
To sign out of your Azure CLI session:
az logout
Next steps
In this quickstart, you ran Azure Resource Graph queries using the extension for Azure CLI. To learn more, go to the query language details article.
Pripomienky
https://aka.ms/ContentUserFeedback.
Pripravujeme: V priebehu roka 2024 postupne zrušíme službu Problémy v službe GitHub ako mechanizmus pripomienok týkajúcich sa obsahu a nahradíme ju novým systémom pripomienok. Ďalšie informácie nájdete na stránke:Odoslať a zobraziť pripomienky pre