Skip to content

Commit e925e9b

Browse files
authored
Merge pull request #279404 from davidsmatlak/ds-updates-arg-shared-cli-20240626
Updates ARG Az CLI shared query
2 parents 5d5a966 + feaa090 commit e925e9b

File tree

2 files changed

+107
-96
lines changed

2 files changed

+107
-96
lines changed

articles/governance/resource-graph/first-query-azurecli.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
---
22
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
3+
description: In this quickstart, you run a Resource Graph query using Azure CLI and the resource-graph extension.
4+
ms.date: 06/26/2024
55
ms.topic: quickstart
66
ms.custom: devx-track-azurecli
77
---
88

99
# Quickstart: Run Resource Graph query using Azure CLI
1010

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.
11+
This quickstart describes how to run an Azure Resource Graph query using the Azure CLI and the Resource Graph extension. 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 finish, you can remove the extension.
1212

1313
## Prerequisites
1414

1515
- If you don't have an Azure account, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
1616
- [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/).
17+
- A Bash shell environment where you can run Azure CLI commands. For example, Git Bash in a [Visual Studio Code](https://code.visualstudio.com/) terminal session.
1818

1919
## Connect to Azure
2020

@@ -30,7 +30,7 @@ az account set --subscription <subscriptionID>
3030

3131
## Install the extension
3232

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.
33+
To enable Azure CLI to query resources using Azure Resource Graph, the Resource Graph extension must be installed. The first time you run a query with `az graph` a prompt is displayed to install the extension. Otherwise, use the following steps to do a manual installation.
3434

3535
1. List the available extensions and versions:
3636

Lines changed: 102 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,152 +1,163 @@
11
---
2-
title: "Quickstart: Create a shared query with Azure CLI"
3-
description: In this quickstart, you follow the steps to enable the Resource Graph extension for Azure CLI and create a shared query.
4-
ms.date: 08/17/2021
2+
title: "Quickstart: Create Resource Graph shared query using Azure CLI"
3+
description: In this quickstart, you create an Azure Resource Graph shared query using Azure CLI and the resource-graph extension.
4+
ms.date: 06/26/2024
55
ms.topic: quickstart
66
ms.custom: devx-track-azurecli
77
---
8-
# Quickstart: Create a Resource Graph shared query using Azure CLI
98

10-
The first step to using Azure Resource Graph with [Azure CLI](/cli/azure/) is to check that the
11-
extension is installed. This quickstart walks you through the process of adding the extension to
12-
your Azure CLI installation. You can use the extension with Azure CLI installed locally or through
13-
the [Azure Cloud Shell](https://shell.azure.com).
9+
# Quickstart: Create Resource Graph shared query using Azure CLI
1410

15-
At the end of this process, you'll have added the extension to your Azure CLI installation of choice
16-
and create a Resource Graph shared query.
11+
This quickstart describes how to create an Azure Resource Graph shared query with Azure CLI and the Resource Graph extension. The [az graph shared-query](/cli/azure/graph/shared-query) commands are an _experimental_ feature of [az graph query](/cli/azure/graph#az-graph-query).
12+
13+
A shared query can be run from Azure CLI with the _experimental_ feature's commands, or you can run the shared query from the Azure portal. A shared query is an Azure Resource Manager object that you can grant permission to or run in Azure Resource Graph Explorer. When you finish, you can remove the Resource Graph extension.
1714

1815
## Prerequisites
1916

20-
If you don't have an Azure subscription, create a [free](https://azure.microsoft.com/free/) account
21-
before you begin.
17+
- If you don't have an Azure account, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
18+
- [Azure CLI](/cli/azure/install-azure-cli) must be version 2.22.0 or higher for the Resource Graph extension.
19+
- A Bash shell environment where you can run Azure CLI commands. For example, Git Bash in a [Visual Studio Code](https://code.visualstudio.com/) terminal session.
20+
21+
## Connect to Azure
2222

23-
<!-- [!INCLUDE [cloud-shell-try-it.md](../../../includes/cloud-shell-try-it.md)] -->
23+
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.
2424

25-
## Add the Resource Graph extension
25+
```azurecli
26+
az login
27+
28+
# Run these commands if you have multiple subscriptions
29+
az account list --output table
30+
az account set --subscription <subscriptionID>
31+
```
2632

27-
To enable Azure CLI to work with 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.
33+
## Install the extension
3134

32-
1. Check that the latest Azure CLI is installed (at least **2.8.0**). If it isn't yet installed,
33-
follow [these instructions](/cli/azure/install-azure-cli-windows).
35+
To enable Azure CLI to query resources using Azure Resource Graph, the Resource Graph extension must be installed. The first time you run a query with `az graph` a prompt is displayed to install the extension. Otherwise, use the following steps to do a manual installation.
3436

35-
1. In your Azure CLI environment of choice, use
36-
[az extension add](/cli/azure/extension#az-extension-add) to import the Resource Graph extension
37-
with the following command:
37+
1. List the available extensions and versions:
3838

3939
```azurecli
40-
# Add the Resource Graph extension to the Azure CLI environment
41-
az extension add --name resource-graph
40+
az extension list-available --output table
4241
```
4342

44-
1. Validate that the extension has been installed and is the expected version (at least **1.1.0**)
45-
with [az extension list](/cli/azure/extension#az-extension-list):
43+
1. Install the extension:
4644

4745
```azurecli
48-
# Check the extension list (note that you may have other extensions installed)
49-
az extension list
50-
51-
# Run help for graph query options
52-
az graph query -h
46+
az extension add --name resource-graph
5347
```
5448

55-
## Create a Resource Graph shared query
49+
1. Verify the extension was installed:
5650

57-
With the Azure CLI extension added to your environment of choice, it's time to a Resource Graph
58-
shared query. The shared query is an Azure Resource Manager object that you can grant permission to
59-
or run in Azure Resource Graph Explorer. The query summarizes the count of all resources grouped by
60-
_location_.
51+
```azurecli
52+
az extension list --output table
53+
```
6154

62-
1. Create a resource group with [az group create](/cli/azure/group#az-group-create) to store the
63-
Azure Resource Graph shared query. This resource group is named `resource-graph-queries` and the
64-
location is `westus2`.
55+
1. Display the extension's syntax:
6556

6657
```azurecli
67-
# Login first with az login if not using Cloud Shell
68-
69-
# Create the resource group
70-
az group create --name 'resource-graph-queries' --location 'westus2'
58+
az graph query --help
7159
```
7260

73-
1. Create the Azure Resource Graph shared query using the `graph` extension and
74-
[az graph shared-query create](/cli/azure/graph/shared-query#az-graph-shared-query-create)
75-
command:
61+
For more information about Azure CLI extensions, go to [Use and manage extensions with the Azure CLI](/cli/azure/azure-cli-extensions-overview).
62+
63+
## Create a shared query
64+
65+
Create a resource group and a shared that summarizes the count of all resources grouped by location.
66+
67+
1. Create a resource group to store the Azure Resource Graph shared query.
7668

7769
```azurecli
78-
# Create the Azure Resource Graph shared query
79-
az graph shared-query create --name 'Summarize resources by location' \
80-
--description 'This shared query summarizes resources by location for a pinnable map graphic.' \
81-
--graph-query 'Resources | summarize count() by location' \
82-
--resource-group 'resource-graph-queries'
70+
az group create --name "demoSharedQuery" --location westus2
8371
```
8472

85-
1. List the shared queries in the new resource group. The
86-
[az graph shared-query list](/cli/azure/graph/shared-query#az-graph-shared-query-list)
87-
command returns an array of values.
73+
1. Create the shared query.
8874

8975
```azurecli
90-
# List all the Azure Resource Graph shared queries in a resource group
91-
az graph shared-query list --resource-group 'resource-graph-queries'
76+
az graph shared-query create --name "Summarize resources by location" \
77+
--description "This shared query summarizes resources by location for a pinnable map graphic." \
78+
--graph-query "Resources | summarize count() by location" \
79+
--resource-group demoSharedQuery
9280
```
9381

94-
1. To get just a single shared query result, use the
95-
[az graph shared-query show](/cli/azure/graph/shared-query#az-graph-shared-query-show)
96-
command.
82+
1. List all shared queries in the resource group.
9783

9884
```azurecli
99-
# Show a specific Azure Resource Graph shared query
100-
az graph shared-query show --resource-group 'resource-graph-queries' \
101-
--name 'Summarize resources by location'
85+
az graph shared-query list --resource-group demoSharedQuery
10286
```
10387

104-
1. Run the shared query in Azure CLI with the `{{shared-query-uri}}` syntax in an
105-
[az graph query](/cli/azure/graph#az-graph-query) command.
106-
First, copy the `id` field from the result of the previous `show` command. Replace
107-
`shared-query-uri` text in the example with the value from the `id` field, but leave the
108-
surrounding `{{` and `}}` characters.
88+
1. Limit the results to a specific shared query.
10989

11090
```azurecli
111-
# Run a Azure Resource Graph shared query
112-
az graph query --graph-query "{{shared-query-uri}}"
91+
az graph shared-query show --resource-group "demoSharedQuery" \
92+
--name "Summarize resources by location"
11393
```
11494

115-
> [!NOTE]
116-
> The `{{shared-query-uri}}` syntax is a **Preview** feature.
95+
## Run the shared query
96+
97+
You can use the Azure CLI experimental feature syntax or the Azure portal to run the shared query.
98+
99+
### Use experimental feature to run shared query
100+
101+
Run the shared query in Azure CLI with the `{{shared-query-uri}}` syntax in an `az graph query` command. You get the resource ID of your shared query and store it in a variable. The variable is used when you run the shared query.
117102

118-
Another way to find Resource Graph shared queries is through the Azure portal. In the portal, use
119-
the search bar to search for "Resource Graph queries". Select the shared query. On the **Overview**
120-
page, the **Query** tab displays the saved query. The **Edit** button opens it in
121-
[Resource Graph Explorer](./first-query-portal.md).
103+
```azurecli
104+
sharedqueryid=$(az graph shared-query show --resource-group "demoSharedQuery" \
105+
--name "Summarize resources by location" \
106+
--query id \
107+
--output tsv)
108+
109+
az graph query --graph-query "{{$sharedqueryid}}"
110+
```
111+
112+
You can use the `subscriptions` parameter to limit the results.
113+
114+
```azurecli
115+
az graph query --graph-query "{{$sharedqueryid}}" --subscriptions 11111111-1111-1111-1111-111111111111
116+
```
117+
118+
### Run the shared query from portal
119+
120+
You can verify the shared query works using Azure Resource Graph Explorer. To change the scope, use the **Scope** menu on the left side of the page.
121+
122+
1. Sign in to [Azure portal](https://portal.azure.com).
123+
1. Enter _resource graph_ into the search field at the top of the page.
124+
1. Select **Resource Graph Explorer**.
125+
1. Select **Open query**.
126+
1. Change **Type** to _Shared queries_.
127+
1. Select the query _Count VMs by OS_.
128+
1. Select **Run query** and the view output in the **Results** tab.
129+
130+
You can also run the query from your resource group.
131+
132+
1. In Azure, go to the resource group, _demoSharedQuery_.
133+
1. From the **Overview** tab, select the query _Count VMs by OS_.
134+
1. Select the **Results** tab.
122135

123136
## Clean up resources
124137

125-
If you wish to remove the Resource Graph shared query, resource group, and extension from your Azure
126-
CLI environment, you can do so by using the following commands:
138+
To remove the resource group and shared query:
139+
140+
```azurecli
141+
az group delete --name demoSharedQuery
142+
```
127143

128-
- [az graph shared-query delete](/cli/azure/graph/shared-query#az-graph-shared-query-delete)
129-
- [az group delete](/cli/azure/group#az-group-delete)
130-
- [az extension remove](/cli/azure/extension#az-extension-remove)
144+
To remove the Resource Graph extension, run the following command:
131145

132146
```azurecli
133-
# Delete the Azure Resource Graph shared query
134-
az graph shared-query delete --resource-group 'resource-graph-queries' \
135-
--name 'Summarize resources by location'
147+
az extension remove --name resource-graph
148+
```
136149

137-
# Remove the resource group
138-
# WARNING: This command deletes ALL resources you've added to this resource group without prompting for confirmation
139-
az group delete --resource-group 'resource-graph-queries' --yes
150+
To sign out of your Azure CLI session:
140151

141-
# Remove the Azure Resource Graph extension from the Azure CLI environment
142-
az extension remove -n resource-graph
152+
```azurecli
153+
az logout
143154
```
144155

145156
## Next steps
146157

147-
In this quickstart, you've added the Resource Graph extension to your Azure CLI environment and
158+
In this quickstart, you added the Resource Graph extension to your Azure CLI environment and
148159
created a shared query. To learn more about the Resource Graph language, continue to the query
149160
language details page.
150161

151162
> [!div class="nextstepaction"]
152-
> [Get more information about the query language](./concepts/query-language.md)
163+
> [Understanding the Azure Resource Graph query language](./concepts/query-language.md)

0 commit comments

Comments
 (0)