|
1 | 1 | ---
|
2 |
| -title: "Quickstart: Your first Azure CLI query" |
3 |
| -description: In this quickstart, you follow the steps to enable the Resource Graph extension for Azure CLI and run your first query. |
4 |
| -ms.date: 08/17/2021 |
| 2 | +title: "Quickstart: Run Resource Graph query using Azure CLI" |
| 3 | +description: In this quickstart, you run an Azure Resource Graph query using the extension for Azure CLI. |
| 4 | +ms.date: 04/22/2024 |
5 | 5 | ms.topic: quickstart
|
6 | 6 | ms.custom: devx-track-azurecli
|
7 | 7 | ---
|
8 |
| -# Quickstart: Run your first Resource Graph query using Azure CLI |
9 | 8 |
|
10 |
| -The first step to using Azure Resource Graph is to check that the extension for [Azure |
11 |
| -CLI](/cli/azure/) is installed. This quickstart walks you through the process of adding the |
12 |
| -extension to your Azure CLI installation. You can use the extension with Azure CLI installed locally |
13 |
| -or through the [Azure Cloud Shell](https://shell.azure.com). |
| 9 | +# Quickstart: Run Resource Graph query using Azure CLI |
14 | 10 |
|
15 |
| -At the end of this process, you'll have added the extension to your Azure CLI installation of choice |
16 |
| -and run your first Resource Graph query. |
| 11 | +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. |
17 | 12 |
|
18 | 13 | ## Prerequisites
|
19 | 14 |
|
20 |
| -If you don't have an Azure subscription, create a [free](https://azure.microsoft.com/free/) account |
21 |
| -before you begin. |
| 15 | +- If you don't have an Azure account, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 16 | +- [Azure CLI](/cli/azure/install-azure-cli) must be version 2.22.0 or higher for the Resource Graph extension. |
| 17 | +- [Visual Studio Code](https://code.visualstudio.com/). |
22 | 18 |
|
23 |
| -<!-- [!INCLUDE [cloud-shell-try-it.md](../../../includes/cloud-shell-try-it.md)] --> |
| 19 | +## Connect to Azure |
24 | 20 |
|
25 |
| -## Add the Resource Graph extension |
| 21 | +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. |
26 | 22 |
|
27 |
| -To enable Azure CLI to query Azure Resource Graph, the extension must be added. This extension |
28 |
| -works wherever Azure CLI can be used, including [bash on Windows 10](/windows/wsl/install-win10), |
29 |
| -[Cloud Shell](https://shell.azure.com) (both standalone and inside the portal), the [Azure CLI |
30 |
| -Docker image](https://hub.docker.com/_/microsoft-azure-cli), or locally installed. |
| 23 | +```azurecli |
| 24 | +az login |
| 25 | +
|
| 26 | +# Run these commands if you have multiple subscriptions |
| 27 | +az account list --output table |
| 28 | +az account set --subscription <subscriptionID> |
| 29 | +``` |
31 | 30 |
|
32 |
| -1. Check that the latest Azure CLI is installed (at least **2.0.76**). If it isn't yet installed, |
33 |
| - follow [these instructions](/cli/azure/install-azure-cli-windows). |
| 31 | +## Install the extension |
34 | 32 |
|
35 |
| -1. In your Azure CLI environment of choice, import it with the following command: |
| 33 | +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. |
| 34 | + |
| 35 | +1. List the available extensions and versions: |
| 36 | + |
| 37 | + ```azurecli |
| 38 | + az extension list-available --output table |
| 39 | + ``` |
| 40 | + |
| 41 | +1. Install the extension: |
36 | 42 |
|
37 | 43 | ```azurecli
|
38 |
| - # Add the Resource Graph extension to the Azure CLI environment |
39 | 44 | az extension add --name resource-graph
|
40 | 45 | ```
|
41 | 46 |
|
42 |
| -1. Validate that the extension has been installed and is the expected version (at least **1.0.0**): |
| 47 | +1. Verify the extension was installed: |
43 | 48 |
|
44 | 49 | ```azurecli
|
45 |
| - # Check the extension list (note that you may have other extensions installed) |
46 |
| - az extension list |
| 50 | + az extension list --output table |
| 51 | + ``` |
47 | 52 |
|
48 |
| - # Run help for graph query options |
49 |
| - az graph query -h |
| 53 | +1. Display the extension's syntax: |
| 54 | + |
| 55 | + ```azurecli |
| 56 | + az graph query --help |
50 | 57 | ```
|
51 | 58 |
|
52 |
| -## Run your first Resource Graph query |
| 59 | + For more information about Azure CLI extensions, go to [Use and manage extensions with the Azure CLI](/cli/azure/azure-cli-extensions-overview). |
53 | 60 |
|
54 |
| -With the Azure CLI extension added to your environment of choice, it's time to try out a simple |
55 |
| -tenant-based Resource Graph query. The query returns the first five Azure resources with the |
56 |
| -**Name** and **Resource Type** of each resource. To query by |
57 |
| -[management group](../management-groups/overview.md) or subscription, use the `--managementgroups` |
58 |
| -or `--subscriptions` arguments. |
| 61 | +## Run a query |
59 | 62 |
|
60 |
| -1. Run your first Azure Resource Graph query using the `graph` extension and `query` command: |
| 63 | +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](../management-groups/overview.md) or subscription, use the `--management-groups` or `--subscriptions` arguments. |
61 | 64 |
|
62 |
| - ```azurecli |
63 |
| - # Login first with az login if not using Cloud Shell |
| 65 | +1. Run an Azure Resource Graph query: |
64 | 66 |
|
65 |
| - # Run Azure Resource Graph query |
66 |
| - az graph query -q 'Resources | project name, type | limit 5' |
| 67 | + ```azurecli |
| 68 | + az graph query --graph-query 'Resources | project name, type | limit 5' |
67 | 69 | ```
|
68 | 70 |
|
69 |
| - > [!NOTE] |
70 |
| - > As this query example does not provide a sort modifier such as `order by`, running this query |
71 |
| - > multiple times is likely to yield a different set of resources per request. |
| 71 | + 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. |
72 | 72 |
|
73 |
| -1. Update the query to `order by` the **Name** property: |
| 73 | +1. Update the query to `order by` the `name` property: |
74 | 74 |
|
75 | 75 | ```azurecli
|
76 |
| - # Run Azure Resource Graph query with 'order by' |
77 |
| - az graph query -q 'Resources | project name, type | limit 5 | order by name asc' |
| 76 | + az graph query --graph-query 'Resources | project name, type | limit 5 | order by name asc' |
78 | 77 | ```
|
79 | 78 |
|
80 |
| - > [!NOTE] |
81 |
| - > Just as with the first query, running this query multiple times is likely to yield a different |
82 |
| - > set of resources per request. The order of the query commands is important. In this example, |
83 |
| - > the `order by` comes after the `limit`. This command order first limits the query results and |
84 |
| - > then orders them. |
| 79 | + 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 the `limit`. The query limits the results to five resources and then orders those results by name. |
85 | 80 |
|
86 |
| -1. Update the query to first `order by` the **Name** property and then `limit` to the top five |
87 |
| - results: |
| 81 | +1. Update the query to `order by` the `name` property and then `limit` the output to five results: |
88 | 82 |
|
89 | 83 | ```azurecli
|
90 |
| - # Run Azure Resource Graph query with `order by` first, then with `limit` |
91 |
| - az graph query -q 'Resources | project name, type | order by name asc | limit 5' |
| 84 | + az graph query --graph-query 'Resources | project name, type | order by name asc | limit 5' |
92 | 85 | ```
|
93 | 86 |
|
94 |
| -When the final query is run several times, assuming that nothing in your environment is changing, |
95 |
| -the results returned are consistent and ordered by the **Name** property, but still limited to the |
96 |
| -top five results. |
| 87 | + 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. |
97 | 88 |
|
98 | 89 | ## Clean up resources
|
99 | 90 |
|
100 |
| -If you wish to remove the Resource Graph extension from your Azure CLI environment, you can do so by |
101 |
| -using the following command: |
| 91 | +To remove the Resource Graph extension, run the following command: |
| 92 | + |
| 93 | +```azurecli |
| 94 | +az extension remove --name resource-graph |
| 95 | +``` |
| 96 | + |
| 97 | +To sign out of your Azure CLI session: |
102 | 98 |
|
103 | 99 | ```azurecli
|
104 |
| -# Remove the Resource Graph extension from the Azure CLI environment |
105 |
| -az extension remove -n resource-graph |
| 100 | +az logout |
106 | 101 | ```
|
107 | 102 |
|
108 | 103 | ## Next steps
|
109 | 104 |
|
110 |
| -In this quickstart, you've added the Resource Graph extension to your Azure CLI environment and run |
111 |
| -your first query. To learn more about the Resource Graph language, continue to the query language |
112 |
| -details page. |
| 105 | +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. |
113 | 106 |
|
114 | 107 | > [!div class="nextstepaction"]
|
115 |
| -> [Get more information about the query language](./concepts/query-language.md) |
| 108 | +> [Understanding the Azure Resource Graph query language](./concepts/query-language.md) |
0 commit comments