Skip to content

Commit 1f39a7a

Browse files
authored
Merge pull request #279487 from davidsmatlak/ds-updates-arg-shared-powershell-20240627
Updates ARG Azure PowerShell shared query
2 parents 5f744b4 + c91edfe commit 1f39a7a

File tree

2 files changed

+142
-75
lines changed

2 files changed

+142
-75
lines changed

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
---
22
title: "Quickstart: Run Resource Graph query using Azure PowerShell"
33
description: In this quickstart, you run an Azure Resource Graph query using the module for Azure PowerShell.
4-
ms.date: 04/24/2024
4+
ms.date: 06/27/2024
55
ms.topic: quickstart
66
ms.custom: mode-api, devx-track-azurepowershell
77
---
88

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

11-
This quickstart describes how to run an Azure Resource Graph query using the `Az.ResourceGraph` module for Azure PowerShell. 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 module.
11+
This quickstart describes how to run an Azure Resource Graph query using the `Az.ResourceGraph` module for Azure PowerShell. The module is included with the latest version of Azure PowerShell and adds [cmdlets](/powershell/module/az.resourcegraph) for Resource Graph.
12+
13+
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.
1214

1315
## Prerequisites
1416

1517
- 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-
- [PowerShell](/powershell/scripting/install/installing-powershell).
17-
- [Azure PowerShell](/powershell/azure/install-azure-powershell).
18+
- Latest versions of [PowerShell](/powershell/scripting/install/installing-powershell) and [Azure PowerShell](/powershell/azure/install-azure-powershell).
1819
- [Visual Studio Code](https://code.visualstudio.com/).
1920

2021
## Install the module
2122

22-
Install the `Az.ResourceGraph` module so that you can use Azure PowerShell to run Azure Resource Graph queries. The Azure Resource Graph module requires PowerShellGet version 2.0.1 or higher. If you installed the latest versions of PowerShell and Azure PowerShell, you already have the required version.
23+
If you installed the latest versions of PowerShell and Azure PowerShell, you already have the `Az.ResourceGraph` module and required version of PowerShellGet.
24+
25+
### Optional module installation
26+
27+
Use the following steps to install the `Az.ResourceGraph` module so that you can use Azure PowerShell to run Azure Resource Graph queries. The Azure Resource Graph module requires PowerShellGet version 2.0.1 or higher.
2328

2429
1. Verify your PowerShellGet version:
2530

@@ -89,6 +94,16 @@ If a query doesn't return results from a subscription you already have access to
8994

9095
## Clean up resources
9196

97+
To sign out of your Azure PowerShell session:
98+
99+
```azurepowershell
100+
Disconnect-AzAccount
101+
```
102+
103+
### Optional clean up steps
104+
105+
If you installed the latest version of Azure PowerShell, the `Az.ResourceGraph` module is included and shouldn't be removed. The following steps are optional if you did a manual install of the `Az.ResourceGraph` module and want to remove the module.
106+
92107
To remove the `Az.ResourceGraph` module from your PowerShell session, run the following command:
93108

94109
```azurepowershell
@@ -103,12 +118,6 @@ Uninstall-Module -Name Az.ResourceGraph
103118

104119
A message might be displayed that _module Az.ResourceGraph is currently in use_. If so, you need to shut down your PowerShell session and start a new session. Then run the command to uninstall the module from your computer.
105120

106-
To sign out of your Azure PowerShell session:
107-
108-
```azurepowershell
109-
Disconnect-AzAccount
110-
```
111-
112121
## Next steps
113122

114123
In this quickstart, you added the Resource Graph module to your Azure PowerShell environment and ran a query. To learn more, go to the query language details page.
Lines changed: 122 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,168 @@
11
---
2-
title: 'Quickstart: Create a shared query with Azure PowerShell'
3-
description: In this quickstart, you follow the steps to create a Resource Graph shared query using Azure PowerShell.
4-
ms.date: 11/09/2022
2+
title: "Quickstart: Create a Resource Graph shared query using Azure PowerShell"
3+
description: In this quickstart, you create a Resource Graph shared query using Azure PowerShell.
4+
ms.date: 06/27/2024
55
ms.topic: quickstart
66
ms.custom: devx-track-azurepowershell, mode-api
77
---
8+
89
# Quickstart: Create a Resource Graph shared query using Azure PowerShell
910

10-
This article describes how you can create an Azure Resource Graph shared query using the
11-
[Az.ResourceGraph](/powershell/module/az.resourcegraph) PowerShell module.
11+
In this quickstart, you create an Azure Resource Graph shared query using the `Az.ResourceGraph` Azure PowerShell module. The module is included with the latest version of Azure PowerShell and adds [cmdlets](/powershell/module/az.resourcegraph) for Resource Graph.
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.
1214

1315
## Prerequisites
1416

15-
- If you don't have an Azure subscription, create a [free](https://azure.microsoft.com/free/) account
16-
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+
- Latest versions of [PowerShell](/powershell/scripting/install/installing-powershell) and [Azure PowerShell](/powershell/azure/install-azure-powershell).
19+
- [Visual Studio Code](https://code.visualstudio.com/).
20+
21+
## Install the module
1722

18-
[!INCLUDE [azure-powershell-requirements-no-header.md](~/reusable-content/ce-skilling/azure/includes/azure-powershell-requirements-no-header.md)]
23+
If you installed the latest versions of PowerShell and Azure PowerShell, you already have the `Az.ResourceGraph` module and required version of PowerShellGet.
1924

20-
> [!IMPORTANT]
21-
> While the **Az.ResourceGraph** PowerShell module is in preview, you must install it separately
22-
> using the `Install-Module` cmdlet.
25+
### Optional module installation
2326

24-
```azurepowershell-interactive
25-
Install-Module -Name Az.ResourceGraph -Scope CurrentUser -Repository PSGallery -Force
26-
```
27+
Use the following steps to install the `Az.ResourceGraph` module so that you can use Azure PowerShell to run Azure Resource Graph queries. The Azure Resource Graph module requires PowerShellGet version 2.0.1 or higher.
2728

28-
- If you have multiple Azure subscriptions, choose the appropriate subscription in which the
29-
resources should be billed. Select a specific subscription using the
30-
[Set-AzContext](/powershell/module/az.accounts/set-azcontext) cmdlet.
29+
1. Verify your PowerShellGet version:
3130

32-
```azurepowershell-interactive
33-
Set-AzContext -SubscriptionId 00000000-0000-0000-0000-000000000000
34-
```
31+
```azurepowershell
32+
Get-Module -Name PowerShellGet
33+
```
3534
36-
## Create a Resource Graph shared query
35+
If you need to update, go to [PowerShellGet](/powershell/gallery/powershellget/install-powershellget).
3736
38-
With the **Az.ResourceGraph** PowerShell module added to your environment of choice, it's time to create
39-
a Resource Graph shared query. The shared query is an Azure Resource Manager object that you can
40-
grant permission to or run in Azure Resource Graph Explorer. The query summarizes the count of all
41-
resources grouped by _location_.
37+
1. Install the module:
38+
39+
```azurepowershell
40+
Install-Module -Name Az.ResourceGraph -Repository PSGallery -Scope CurrentUser
41+
```
4242

43-
1. Create a resource group with
44-
[New-AzResourceGroup](/powershell/module/az.resources/new-azresourcegroup) to store the Azure
45-
Resource Graph shared query. This resource group is named `resource-graph-queries` and the
46-
location is `westus2`.
43+
The command installs the module in the `CurrentUser` scope. If you need to install in the `AllUsers` scope, run the installation from an administrative PowerShell session.
4744

48-
```azurepowershell-interactive
49-
# Login first with `Connect-AzAccount` if not using Cloud Shell
45+
1. Verify the module was installed:
5046

51-
# Create the resource group
52-
New-AzResourceGroup -Name resource-graph-queries -Location westus2
47+
```azurepowershell
48+
Get-Command -Module Az.ResourceGraph -CommandType Cmdlet
5349
```
5450

55-
1. Create the Azure Resource Graph shared query using the **Az.ResourceGraph** PowerShell module and
56-
[New-AzResourceGraphQuery](/powershell/module/az.resourcegraph/new-azresourcegraphquery)
57-
cmdlet:
51+
The command displays the `Search-AzGraph` cmdlet version and loads the module into your PowerShell session.
52+
53+
## Connect to Azure
54+
55+
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.
56+
57+
```azurepowershell
58+
Connect-AzAccount
59+
60+
# Run these commands if you have multiple subscriptions
61+
Get-AzSubScription
62+
Set-AzContext -Subscription <subscriptionID>
63+
```
5864

59-
```azurepowershell-interactive
60-
# Create the Azure Resource Graph shared query
61-
$Params = @{
65+
## Create a shared query
66+
67+
The shared query is an Azure Resource Manager object that you can grant permission to or run in Azure Resource Graph Explorer. The query summarizes the count of all resources grouped by location.
68+
69+
1. Create a resource group to store the Azure Resource Graph shared query.
70+
71+
```azurepowershell
72+
New-AzResourceGroup -Name demoSharedQuery -Location westus2
73+
```
74+
75+
1. Create the Azure Resource Graph shared query.
76+
77+
```azurepowershell
78+
$params = @{
6279
Name = 'Summarize resources by location'
63-
ResourceGroupName = 'resource-graph-queries'
80+
ResourceGroupName = 'demoSharedQuery'
6481
Location = 'westus2'
6582
Description = 'This shared query summarizes resources by location for a pinnable map graphic.'
6683
Query = 'Resources | summarize count() by location'
6784
}
68-
New-AzResourceGraphQuery @Params
85+
86+
New-AzResourceGraphQuery @params
6987
```
7088

71-
1. List the shared queries in the new resource group. The
72-
[Get-AzResourceGraphQuery](/powershell/module/az.resourcegraph/get-azresourcegraphquery)
73-
cmdlet returns an array of values.
89+
The `$params` variable uses PowerShell [splatting](/powershell/module/microsoft.powershell.core/about/about_splatting) to improve readability for the parameter values used in the command to create the shared query.
7490

75-
```azurepowershell-interactive
76-
# List all the Azure Resource Graph shared queries in a resource group
77-
Get-AzResourceGraphQuery -ResourceGroupName resource-graph-queries
91+
1. List all shared queries in the resource group.
92+
93+
```azurepowershell
94+
Get-AzResourceGraphQuery -ResourceGroupName demoSharedQuery
7895
```
7996

80-
1. To get just a single shared query result, use `Get-AzResourceGraphQuery` with its `Name` parameter.
97+
1. Limit the results to a specific shared query.
8198

82-
```azurepowershell-interactive
83-
# Show a specific Azure Resource Graph shared query
84-
Get-AzResourceGraphQuery -ResourceGroupName resource-graph-queries -Name 'Summarize resources by location'
99+
```azurepowershell
100+
Get-AzResourceGraphQuery -ResourceGroupName demoSharedQuery -Name 'Summarize resources by location'
85101
```
86102

103+
## Run the shared query
104+
105+
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.
106+
107+
1. Sign in to [Azure portal](https://portal.azure.com).
108+
1. Enter _resource graph_ into the search field at the top of the page.
109+
1. Select **Resource Graph Explorer**.
110+
1. Select **Open query**.
111+
1. Change **Type** to _Shared queries_.
112+
1. Select the query _Count VMs by OS_.
113+
1. Select **Run query** and the view output in the **Results** tab.
114+
1. Select **Charts** and then select **Map** to view the location map.
115+
116+
You can also run the query from your resource group.
117+
118+
1. In Azure, go to the resource group, _demoSharedQuery_.
119+
1. From the **Overview** tab, select the query _Count VMs by OS_.
120+
1. Select the **Results** tab to view a list.
121+
1. Select **Charts** and then select **Map** to view the location map.
122+
87123
## Clean up resources
88124

89-
If you wish to remove the Resource Graph shared query and resource group from your Azure
90-
environment, you can do so by using the following commands:
125+
When you finish, you can remove the Resource Graph shared query and resource group from your Azure environment. When a resource group is deleted, the resource group and all its resources are deleted.
126+
127+
Remove the shared query:
128+
129+
```azurepowershell
130+
Remove-AzResourceGraphQuery -ResourceGroupName demoSharedQuery -Name 'Summarize resources by location'
131+
```
132+
133+
Delete the resource group:
134+
135+
```azurepowershell
136+
Remove-AzResourceGroup -Name demoSharedQuery
137+
```
138+
139+
To sign out of your Azure PowerShell session:
140+
141+
```azurepowershell
142+
Disconnect-AzAccount
143+
```
144+
145+
### Optional clean up steps
91146

92-
- [Remove-AzResourceGraphQuery](/powershell/module/az.resourcegraph/remove-azresourcegraphquery)
93-
- [Remove-AzResourceGroup](/powershell/module/az.resources/remove-azresourcegroup)
147+
If you installed the latest version of Azure PowerShell, the `Az.ResourceGraph` module is included and shouldn't be removed. The following steps are optional if you did a manual install of the `Az.ResourceGraph` module and want to remove the module.
94148

95-
```azurepowershell-interactive
96-
# Delete the Azure Resource Graph shared query
97-
Remove-AzResourceGraphQuery -ResourceGroupName resource-graph-queries -Name 'Summarize resources by location'
149+
To remove the `Az.ResourceGraph` module from your PowerShell session, run the following command:
98150

99-
# Remove the resource group
100-
# WARNING: This command deletes ALL resources you've added to this resource group
101-
Remove-AzResourceGroup -Name resource-graph-queries
151+
```azurepowershell
152+
Remove-Module -Name Az.ResourceGraph
102153
```
103154

155+
To uninstall the `Az.ResourceGraph` module from your computer, run the following command:
156+
157+
```azurepowershell
158+
Uninstall-Module -Name Az.ResourceGraph
159+
```
160+
161+
A message might be displayed that _module Az.ResourceGraph is currently in use_. If so, you need to shut down your PowerShell session and start a new session. Then run the command to uninstall the module from your computer.
162+
104163
## Next steps
105164

106-
In this quickstart, you've created a Resource Graph shared query using Azure PowerShell. To learn
107-
more about the Resource Graph language, continue to the query language details page.
165+
In this quickstart, you created a Resource Graph shared query using Azure PowerShell. To learn more about the Resource Graph language, continue to the query language details page.
108166

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

0 commit comments

Comments
 (0)