|
1 | 1 | ---
|
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 |
5 | 5 | ms.topic: quickstart
|
6 | 6 | ms.custom: devx-track-azurepowershell, mode-api
|
7 | 7 | ---
|
| 8 | + |
8 | 9 | # Quickstart: Create a Resource Graph shared query using Azure PowerShell
|
9 | 10 |
|
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. |
12 | 14 |
|
13 | 15 | ## Prerequisites
|
14 | 16 |
|
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 |
17 | 22 |
|
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. |
19 | 24 |
|
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 |
23 | 26 |
|
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. |
27 | 28 |
|
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: |
31 | 30 |
|
32 |
| - ```azurepowershell-interactive |
33 |
| - Set-AzContext -SubscriptionId 00000000-0000-0000-0000-000000000000 |
34 |
| - ``` |
| 31 | + ```azurepowershell |
| 32 | + Get-Module -Name PowerShellGet |
| 33 | + ``` |
35 | 34 |
|
36 |
| -## Create a Resource Graph shared query |
| 35 | + If you need to update, go to [PowerShellGet](/powershell/gallery/powershellget/install-powershellget). |
37 | 36 |
|
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 | + ``` |
42 | 42 |
|
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. |
47 | 44 |
|
48 |
| - ```azurepowershell-interactive |
49 |
| - # Login first with `Connect-AzAccount` if not using Cloud Shell |
| 45 | +1. Verify the module was installed: |
50 | 46 |
|
51 |
| - # Create the resource group |
52 |
| - New-AzResourceGroup -Name resource-graph-queries -Location westus2 |
| 47 | + ```azurepowershell |
| 48 | + Get-Command -Module Az.ResourceGraph -CommandType Cmdlet |
53 | 49 | ```
|
54 | 50 |
|
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 | +``` |
58 | 64 |
|
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 = @{ |
62 | 79 | Name = 'Summarize resources by location'
|
63 |
| - ResourceGroupName = 'resource-graph-queries' |
| 80 | + ResourceGroupName = 'demoSharedQuery' |
64 | 81 | Location = 'westus2'
|
65 | 82 | Description = 'This shared query summarizes resources by location for a pinnable map graphic.'
|
66 | 83 | Query = 'Resources | summarize count() by location'
|
67 | 84 | }
|
68 |
| - New-AzResourceGraphQuery @Params |
| 85 | +
|
| 86 | + New-AzResourceGraphQuery @params |
69 | 87 | ```
|
70 | 88 |
|
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. |
74 | 90 |
|
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 |
78 | 95 | ```
|
79 | 96 |
|
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. |
81 | 98 |
|
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' |
85 | 101 | ```
|
86 | 102 |
|
| 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 | + |
87 | 123 | ## Clean up resources
|
88 | 124 |
|
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 |
91 | 146 |
|
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. |
94 | 148 |
|
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: |
98 | 150 |
|
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 |
102 | 153 | ```
|
103 | 154 |
|
| 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 | + |
104 | 163 | ## Next steps
|
105 | 164 |
|
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. |
108 | 166 |
|
109 | 167 | > [!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