You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Explore your Azure resources with Resource Graph
10
10
11
-
Azure Resource Graph helps you explore and discover your Azure resources quickly and
12
-
at scale. Engineered for fast responses, it's a great way to learn about your environment and also
13
-
about the properties that exist on your Azure resources.
11
+
Azure Resource Graph helps you explore and discover your Azure resources quickly and at scale. Engineered for fast responses, it's a great way to learn about your environment and also about the properties that exist on your Azure resources.
14
12
15
13
> [!NOTE]
16
14
> Depending on the Resource Graph table, properties will either match the casing as shown in the Azure portal or be lowercased.
@@ -21,14 +19,11 @@ about the properties that exist on your Azure resources.
21
19
22
20
## Explore virtual machines
23
21
24
-
A common resource in Azure is a virtual machine. As a resource type, virtual machines have many
25
-
properties that can be queried. Each property provides an option for filtering or finding exactly
26
-
the resource you're looking for.
22
+
A common resource in Azure is a virtual machine. As a resource type, virtual machines have many properties that can be queried. Each property provides an option for filtering or finding exactly the resource you're looking for.
27
23
28
24
### Virtual machine discovery
29
25
30
-
Let's start with a simple query to get a single virtual machine from our environment and look at the
31
-
properties returned.
26
+
Let's start with a simple query to get a single virtual machine from our environment and look at the properties returned.
32
27
33
28
```kusto
34
29
Resources
@@ -40,14 +35,14 @@ Resources
40
35
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | limit 1"
41
36
```
42
37
43
-
```azurepowershell-interactive
38
+
```azurepowershell
44
39
(Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | limit 1").Data | ConvertTo-Json -Depth 100
45
40
```
46
41
47
42
> [!NOTE]
48
-
> The Azure PowerShell `Search-AzGraph` cmdlet returns a **PSResourceGraphResponse** by default. To
43
+
> The Azure PowerShell `Search-AzGraph` cmdlet returns a `PSResourceGraphResponse` by default. To
49
44
> have the output look the same as what is returned by Azure CLI, the `ConvertTo-Json` cmdlet is
50
-
> used on the **Data** property. The default value for **Depth** is _2_. Setting it to _100_ should
45
+
> used on the `Data` property. The default value for `Depth` is _2_. Setting it to _100_ should
51
46
> convert all returned levels.
52
47
53
48
The JSON results are structured similar to the following example:
@@ -68,7 +63,7 @@ The JSON results are structured similar to the following example:
@@ -115,15 +110,11 @@ The JSON results are structured similar to the following example:
115
110
]
116
111
```
117
112
118
-
The properties tell us additional information about the virtual machine resource itself. These
119
-
properties include: operating system, disks, tags, and the resource group and subscription it's
120
-
a member of.
113
+
The properties tell us additional information about the virtual machine resource itself. These properties include: operating system, disks, tags, and the resource group and subscription it's a member of.
121
114
122
115
### Virtual machines by location
123
116
124
-
Taking what we learned about the virtual machines resource, let's use the **location** property to
125
-
count all virtual machines by location. To update the query, we remove the limit and summarize
126
-
the count of location values.
117
+
Taking what we learned about the virtual machines resource, let's use the `location` property to count all virtual machines by location. To update the query, we remove the limit and summarize the count of location values.
127
118
128
119
```kusto
129
120
Resources
@@ -135,7 +126,7 @@ Resources
135
126
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by location"
136
127
```
137
128
138
-
```azurepowershell-interactive
129
+
```azurepowershell
139
130
(Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by location").Data | ConvertTo-Json
140
131
```
141
132
@@ -162,8 +153,7 @@ We can now see how many virtual machines we have in each Azure region.
162
153
163
154
### Virtual machines by SKU
164
155
165
-
Going back to the original virtual machine properties, let's try to find all the virtual machines
166
-
that have a SKU size of **Standard_B2s**. The returned JSON returned shows that it's stored in **properties.hardwareprofile.vmsize**. We'll update the query to find all VMs that match this size and return just the name of the VM and region.
156
+
Going back to the original virtual machine properties, let's try to find all the virtual machines that have a SKU size of `Standard_B2s`. The returned JSON shows the value is stored in `properties.hardwareprofile.vmsize`. We update the query to find all virtual machines (VM) that match this size and return just the name of the VM and region.
167
157
168
158
```kusto
169
159
Resources
@@ -175,14 +165,13 @@ Resources
175
165
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' and properties.hardwareProfile.vmSize == 'Standard_B2s' | project name, resourceGroup"
176
166
```
177
167
178
-
```azurepowershell-interactive
168
+
```azurepowershell
179
169
(Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Compute/virtualMachines' and properties.hardwareProfile.vmSize == 'Standard_B2s' | project name, resourceGroup").Data | ConvertTo-Json
180
170
```
181
171
182
172
### Virtual machines connected to premium-managed disks
183
173
184
-
To get the details of premium-managed disks that are attached to these **Standard_B2s** virtual
185
-
machines, we expand the query to return the resource ID of those managed disks.
174
+
To get the details of premium-managed disks that are attached to these `Standard_B2s` virtual machines, we expand the query to return the resource ID of those managed disks.
186
175
187
176
```kusto
188
177
Resources
@@ -196,60 +185,54 @@ Resources
196
185
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualmachines' and properties.hardwareProfile.vmSize == 'Standard_B2s' | extend disk = properties.storageProfile.osDisk.managedDisk | where disk.storageAccountType == 'Premium_LRS' | project disk.id"
197
186
```
198
187
199
-
```azurepowershell-interactive
188
+
```azurepowershell
200
189
(Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Compute/virtualmachines' and properties.hardwareProfile.vmSize == 'Standard_B2s' | extend disk = properties.storageProfile.osDisk.managedDisk | where disk.storageAccountType == 'Premium_LRS' | project disk.id").Data | ConvertTo-Json
201
190
```
202
191
203
192
The result is a list of disk IDs.
204
193
205
194
### Managed disk discovery
206
195
207
-
With the first record from the previous query, we'll explore the properties that exist on the
208
-
managed disk that was attached to the first virtual machine. The updated query uses the disk ID and
209
-
changes the type.
196
+
With the first record from the previous query, you explore the properties that exist on the managed disk that was attached to the first virtual machine. The updated query uses the disk ID and changes the type.
210
197
211
198
Example output from the previous query for example:
| where type =~ 'Microsoft.Compute/disks' and id == '/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/ContosoVM1_OsDisk_1_9676b7e1b3c44e2cb672338ebe6f5166'
210
+
| where type =~ 'Microsoft.Compute/disks' and id == '/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/ContosoVM1_OsDisk_1_11111111111111111111111111111111'
224
211
```
225
212
226
-
Before you run the query, how did we know the **type** should now be **Microsoft.Compute/disks**? If
227
-
you look at the full ID, you'll see **/providers/Microsoft.Compute/disks/** as part of the string.
228
-
This string fragment gives you a hint as to what type to search for. An alternative method would be
229
-
to remove the limit by type and instead only search by the ID field. As the ID is unique, only one
230
-
record would be returned and the **type** property on it provides that detail.
213
+
Before you run the query, how did we know the `type` should now be `Microsoft.Compute/disks`? If you look at the full ID, you notice `/providers/Microsoft.Compute/disks/` as part of the string. This string fragment gives you a hint as to what type to search for. An alternative method would be to remove the limit by type and instead only search by the ID field. As the ID is unique, only one record would be returned and the `type` property on it provides that detail.
231
214
232
215
> [!NOTE]
233
216
> For this example to work, you must replace the ID field with a result from your own environment.
234
217
235
218
```azurecli
236
-
az graph query -q "Resources | where type =~ 'Microsoft.Compute/disks' and id == '/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/ContosoVM1_OsDisk_1_9676b7e1b3c44e2cb672338ebe6f5166'"
219
+
az graph query -q "Resources | where type =~ 'Microsoft.Compute/disks' and id == '/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/ContosoVM1_OsDisk_1_11111111111111111111111111111111'"
237
220
```
238
221
239
-
```azurepowershell-interactive
240
-
(Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Compute/disks' and id == '/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/ContosoVM1_OsDisk_1_9676b7e1b3c44e2cb672338ebe6f5166'").Data | ConvertTo-Json
222
+
```azurepowershell
223
+
(Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Compute/disks' and id == '/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/ContosoVM1_OsDisk_1_11111111111111111111111111111111'").Data | ConvertTo-Json
241
224
```
242
225
243
226
The JSON results are structured similar to the following example:
@@ -276,10 +259,7 @@ The JSON results are structured similar to the following example:
276
259
277
260
## Explore virtual machines to find public IP addresses
278
261
279
-
This set of queries first finds and stores all the network interfaces (NIC) resources connected to
280
-
virtual machines. Then the queries use the list of NICs to find each IP address resource that is a
281
-
public IP address and store those values. Finally, the queries provide a list of the public IP
282
-
addresses.
262
+
This set of queries first finds and stores all the network interface cards (NIC) resources connected to virtual machines. Then the queries use the list of NICs to find each IP address resource that is a public IP address and store those values. Finally, the queries provide a list of the public IP addresses.
283
263
284
264
```azurecli
285
265
# Use Resource Graph to get all NICs and store in the 'nics.txt' file
@@ -289,16 +269,15 @@ az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines'
289
269
cat nics.txt
290
270
```
291
271
292
-
```azurepowershell-interactive
272
+
```azurepowershell
293
273
# Use Resource Graph to get all NICs and store in the $nics variable
294
274
$nics = (Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | project nic = tostring(properties['networkProfile']['networkInterfaces'][0]['id']) | where isnotempty(nic) | distinct nic | limit 20").Data
295
275
296
276
# Review the output of the query stored in the variable
297
277
$nics.nic
298
278
```
299
279
300
-
Use the file (Azure CLI) or variable (Azure PowerShell) in the next query to get the related network
301
-
interface resources details where there's a public IP address attached to the NIC.
280
+
In the next query, use the file (Azure CLI) or variable (Azure PowerShell), to get the related network interface card resources details that have a public IP address attached to the NIC.
302
281
303
282
```azurecli
304
283
# Use Resource Graph with the 'nics.txt' file to get all related public IP addresses and store in 'publicIp.txt' file
@@ -308,30 +287,27 @@ az graph query -q="Resources | where type =~ 'Microsoft.Network/networkInterface
308
287
cat ips.txt
309
288
```
310
289
311
-
```azurepowershell-interactive
290
+
```azurepowershell
312
291
# Use Resource Graph with the $nics variable to get all related public IP addresses and store in $ips variable
313
292
$ips = (Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Network/networkInterfaces' | where id in ('$($nics.nic -join "','")') | project publicIp = tostring(properties['ipConfigurations'][0]['properties']['publicIPAddress']['id']) | where isnotempty(publicIp) | distinct publicIp").Data
314
293
315
294
# Review the output of the query stored in the variable
316
295
$ips.publicIp
317
296
```
318
297
319
-
Last, use the list of public IP address resources stored in the file (Azure CLI) or variable (Azure
320
-
PowerShell) to get the actual public IP address from the related object and display.
298
+
Last, use the list of public IP address resources stored in the file (Azure CLI) or variable (Azure PowerShell) to get the actual public IP address from the related object and display.
321
299
322
300
```azurecli
323
301
# Use Resource Graph with the 'ips.txt' file to get the IP address of the public IP address resources
324
302
az graph query -q="Resources | where type =~ 'Microsoft.Network/publicIPAddresses' | where id in ('$(awk -vORS="','" '{print $0}' ips.txt | sed 's/,$//')') | project ip = tostring(properties['ipAddress']) | where isnotempty(ip) | distinct ip" --output table
325
303
```
326
304
327
-
```azurepowershell-interactive
305
+
```azurepowershell
328
306
# Use Resource Graph with the $ips variable to get the IP address of the public IP address resources
329
307
(Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Network/publicIPAddresses' | where id in ('$($ips.publicIp -join "','")') | project ip = tostring(properties['ipAddress']) | where isnotempty(ip) | distinct ip").Data | ConvertTo-Json
330
308
```
331
309
332
-
To see how to accomplish these steps in a single query with the `join` operator, see the
333
-
[List virtual machines with their network interface and public IP](../samples/advanced.md#list-virtual-machines-with-their-network-interface-and-public-ip)
334
-
sample.
310
+
To see how to accomplish these steps in a single query with the `join` operator, go to [List virtual machines with their network interface and public IP](../samples/advanced.md#list-virtual-machines-with-their-network-interface-and-public-ip) sample.
0 commit comments