Skip to content

Commit fa863ba

Browse files
authored
Merge pull request #189165 from jennyhunter-msft/patch-3
Added PS, CLI, and portal instructions
2 parents bed19f9 + 1e4f274 commit fa863ba

File tree

1 file changed

+137
-8
lines changed

1 file changed

+137
-8
lines changed

articles/governance/resource-graph/how-to/get-resource-changes.md

Lines changed: 137 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Get resource changes
33
description: Understand how to find when a resource was changed and query the list of resource configuration changes at scale
4-
ms.date: 01/27/2022
4+
ms.date: 02/18/2022
55
ms.topic: how-to
66
---
77
# Get resource changes
@@ -44,7 +44,7 @@ Monitor.
4444
4545
## Find detected change events and view change details
4646

47-
When a resource is created, updated, or deleted, a new change resource (Microsoft.Resources/changes) is created to extend the modified resource and represent the changed properties.
47+
When a resource is created, updated, or deleted, a new change resource (Microsoft.Resources/changes) is created to extend the modified resource and represent the changed properties. Change records should be available in under five minutes.
4848

4949
Example change resource property bag:
5050

@@ -99,13 +99,142 @@ Each change resource has the following properties:
9999
- **previousResourceSnapshotId** - Contains the ID of the resource snapshot that was used as the previous state of the resource.
100100
- **newResourceSnapshotId** - Contains the ID of the resource snapshot that was used as the new state of the resource.
101101

102-
## Resource Graph Query samples
102+
## How to query changes using Resource Graph
103+
### Prerequisites
104+
- To enable Azure PowerShell to query Azure Resource Graph, the [module must be added](../first-query-powershell.md#add-the-resource-graph-module).
105+
- To enable Azure CLI to query Azure Resource Graph, the [extension must be added](../first-query-azurecli.md#add-the-resource-graph-extension).
103106

104-
With Resource Graph, you can query the **ResourceChanges** table to filter or sort by any of the change resource properties:
107+
### Run your Resource Graph query
108+
It's time to try out a tenant-based Resource Graph query of the **resourcechanges** table. The query returns the first five most recent Azure resource changes with the change time, change type, target resource ID, target resource type, and change details of each change record. To query by
109+
[management group](../../management-groups/overview.md) or subscription, use the `-ManagementGroup`
110+
or `-Subscription` parameters.
111+
112+
1. Run your first Azure Resource Graph query:
113+
114+
# [Azure CLI](#tab/azure-cli)
115+
```azurecli-interactive
116+
# Login first with az login if not using Cloud Shell
117+
118+
# Run Azure Resource Graph query
119+
az graph query -q 'resourcechanges | project properties.changeAttributes.timestamp, properties.changeType, properties.targetResourceId, properties.targetResourceType, properties.changes | limit 5'
120+
```
121+
122+
# [PowerShell](#tab/azure-powershell)
123+
```azurepowershell-interactive
124+
# Login first with Connect-AzAccount if not using Cloud Shell
125+
126+
# Run Azure Resource Graph query
127+
Search-AzGraph -Query 'resourcechanges | project properties.changeAttributes.timestamp, properties.changeType, properties.targetResourceId, properties.targetResourceType, properties.changes | limit 5'
128+
```
129+
130+
# [Portal](#tab/azure-portal)
131+
Open the [Azure portal](https://portal.azure.com) to find and use the Resource Graph Explorer
132+
following these steps to run your first Resource Graph query:
133+
134+
1. Select **All services** in the left pane. Search for and select **Resource Graph Explorer**.
135+
136+
1. In the **Query 1** portion of the window, enter the query
137+
```kusto
138+
resourcechanges
139+
| project properties.changeAttributes.timestamp, properties.changeType, properties.targetResourceId, properties.targetResourceType, properties.changes
140+
| limit 5
141+
```
142+
and select **Run query**.
143+
144+
1. Review the query response in the **Results** tab. Select the **Messages** tab to see details
145+
about the query, including the count of results and duration of the query. Errors, if any, are
146+
displayed under this tab.
147+
148+
---
149+
150+
> [!NOTE]
151+
> As this query example doesn't provide a sort modifier such as `order by`, running this query
152+
> multiple times is likely to yield a different set of resources per request.
153+
154+
155+
2. Update the query to specify a more user-friendly column name for the **timestamp** property:
156+
157+
# [Azure CLI](#tab/azure-cli)
158+
```azurecli-interactive
159+
# Run Azure Resource Graph query with 'extend' to define a user-friendly name for properties.changeAttributes.timestamp
160+
az graph query -q 'resourcechanges | extend changeTime=todatetime(properties.changeAttributes.timestamp) | project changeTime, properties.changeType, properties.targetResourceId, properties.targetResourceType, properties.changes | limit 5'
161+
```
162+
163+
# [PowerShell](#tab/azure-powershell)
164+
```azurepowershell-interactive
165+
# Run Azure Resource Graph query with 'extend' to define a user-friendly name for properties.changeAttributes.timestamp
166+
Search-AzGraph -Query 'resourcechanges | extend changeTime=todatetime(properties.changeAttributes.timestamp) | project changeTime, properties.changeType, properties.targetResourceId, properties.targetResourceType, properties.changes | limit 5'
167+
```
168+
169+
# [Portal](#tab/azure-portal)
170+
```kusto
171+
resourcechanges
172+
| extend changeTime=todatetime(properties.changeAttributes.timestamp)
173+
| project changeTime, properties.changeType, properties.targetResourceId, properties.targetResourceType, properties.changes
174+
| limit 5
175+
```
176+
Then, select **Run query**.
177+
178+
---
179+
180+
181+
3. To get the most recent changes, update the query to `order by` the user-defined **changeTime** property:
182+
183+
# [Azure CLI](#tab/azure-cli)
184+
```azurecli-interactive
185+
# Run Azure Resource Graph query with 'order by'
186+
az graph query -q 'resourcechanges | extend changeTime=todatetime(properties.changeAttributes.timestamp) | project changeTime, properties.changeType, properties.targetResourceId, properties.targetResourceType, properties.changes | order by changeTime desc | limit 5'
187+
```
188+
189+
# [PowerShell](#tab/azure-powershell)
190+
```azurepowershell-interactive
191+
# Run Azure Resource Graph query with 'order by'
192+
Search-AzGraph -Query 'resourcechanges | extend changeTime=todatetime(properties.changeAttributes.timestamp) | project changeTime, properties.changeType, properties.targetResourceId, properties.targetResourceType, properties.changes | order by changeTime desc | limit 5'
193+
```
194+
195+
# [Portal](#tab/azure-portal)
196+
```kusto
197+
resourcechanges
198+
| extend changeTime=todatetime(properties.changeAttributes.timestamp)
199+
| project changeTime, properties.changeType, properties.targetResourceId, properties.targetResourceType, properties.changes
200+
| order by changeTime desc
201+
| limit 5
202+
```
203+
Then, select **Run query**.
204+
205+
---
206+
207+
> [!NOTE]
208+
> The order of the query commands is important. In this example,
209+
> the `order by` must come before the `limit` command. This command order first orders the query results by the change time and
210+
> then limits them to ensure that you get the five *most recent* results.
211+
212+
213+
When the final query is run several times, assuming that nothing in your environment is changing,
214+
the results returned are consistent and ordered by the **properties.changeAttributes.timestamp** (or your user-defined name of **changeTime**) property, but still limited to the
215+
top five results.
216+
217+
218+
> [!NOTE]
219+
> If the query does not return results from a subscription you already have access to, then note
220+
> that the `Search-AzGraph` PowerShell cmdlet defaults to subscriptions in the default context. To see the list of
221+
> subscription IDs which are part of the default context run this
222+
> `(Get-AzContext).Account.ExtendedProperties.Subscriptions` If you wish to search across all the
223+
> subscriptions you have access to, one can set the PSDefaultParameterValues for `Search-AzGraph`
224+
> cmdlet by running
225+
> `$PSDefaultParameterValues=@{"Search-AzGraph:Subscription"= $(Get-AzSubscription).ID}`
226+
227+
Resource Graph Explorer also provides a clean interface for converting the results of some queries into a chart that can be pinned to an Azure dashboard.
228+
- [Create a chart from the Resource Graph query](../first-query-portal.md#create-a-chart-from-the-resource-graph-query)
229+
- [Pin the query visualization to a dashboard](../first-query-portal.md#pin-the-query-visualization-to-a-dashboard)
230+
231+
## Resource Graph query samples
232+
233+
With Resource Graph, you can query the **resourcechanges** table to filter or sort by any of the change resource properties:
105234

106235
### All changes in the past one day
107236
```kusto
108-
ResourceChanges
237+
resourcechanges
109238
| extend changeTime = todatetime(properties.changeAttributes.timestamp), targetResourceId = tostring(properties.targetResourceId),
110239
changeType = tostring(properties.changeType), correlationId = properties.changeAttributes.correlationId, 
111240
changedProperties = properties.changes, changeCount = properties.changeAttributes.changesCount
@@ -116,7 +245,7 @@ changedProperties = properties.changes, changeCount = properties.changeAttr
116245

117246
### Resources deleted in a specific resource group
118247
```kusto
119-
ResourceChanges
248+
resourcechanges
120249
| where resourceGroup == "myResourceGroup"
121250
| extend changeTime = todatetime(properties.changeAttributes.timestamp), targetResourceId = tostring(properties.targetResourceId),
122251
changeType = tostring(properties.changeType), correlationId = properties.changeAttributes.correlationId
@@ -127,7 +256,7 @@ changeType = tostring(properties.changeType), correlationId = properties.ch
127256

128257
### Changes to a specific property value
129258
```kusto
130-
ResourceChanges
259+
resourcechanges
131260
| extend provisioningStateChange = properties.changes["properties.provisioningState"], changeTime = todatetime(properties.changeAttributes.timestamp), targetResourceId = tostring(properties.targetResourceId), changeType = tostring(properties.changeType)
132261
| where isnotempty(provisioningStateChange)and provisioningStateChange.newValue == "Succeeded"
133262
| order by changeTime desc
@@ -136,7 +265,7 @@ ResourceChanges
136265

137266
### Query the latest resource configuration for resources created in the last seven days
138267
```kusto
139-
ResourceChanges
268+
resourcechanges
140269
| extend targetResourceId = tostring(properties.targetResourceId), changeType = tostring(properties.changeType), changeTime = todatetime(properties.changeAttributes.timestamp)
141270
| where changeTime > ago(7d) and changeType == "Create"
142271
| project targetResourceId, changeType, changeTime

0 commit comments

Comments
 (0)