取得資源變更

資源會隨著每日使用、重新設定,甚至重新部署而變更。 大部分的變更是設計方式,但有時不是。 您可以:

  • 了解何時在 Azure Resource Manager 屬性上偵測到變更。
  • 檢視屬性變更詳細數據。
  • 在訂用帳戶、管理群組或租用戶之間大規模查詢變更。

在本文章中,您將了解:

  • 承載 JSON 的外觀。
  • 如何使用 CLI、PowerShell 或 Azure 入口網站,透過 Resource Graph 查詢資源變更。
  • 查詢資源變更的查詢範例和最佳做法。

必要條件

瞭解變更事件屬性

建立、更新或刪除資源時,會建立新的變更資源 (Microsoft.Resources/changes) 來擴充修改的資源,並代表已變更的屬性。 變更記錄應該在五分鐘內可用。 下列範例 JSON 承載示範變更資源屬性:

{
  "targetResourceId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/microsoft.compute/virtualmachines/myVM",
  "targetResourceType": "microsoft.compute/virtualmachines",
  "changeType": "Update",
  "changeAttributes": {
    "previousResourceSnapshotId": "08584889383111245807_37592049-3996-ece7-c583-3008aef9e0e1_4043682982_1712668574",
    "newResourceSnapshotId": "08584889377081305807_38788020-eeee-ffff-028f-6121bdac9cfe_4213468768_1712669177",
    "correlationId": "04ff69b3-e162-4583-9cd7-1a14a1ec2c61",
    "changedByType": "User",
    "changesCount": 2,
    "clientType": "ARM Template",
    "changedBy": "john@contoso.com",
    "operation": "microsoft.compute/virtualmachines/write",
    "timestamp": "2024-04-09T13:26:17.347+00:00"
  },
  "changes": {
    "properties.provisioningState": {
      "newValue": "Succeeded",
      "previousValue": "Updating",
      "changeCategory": "System",
      "propertyChangeType": "Update",
      "isTruncated": "true"
    },
    "tags.key1": {
      "newValue": "NewTagValue",
      "previousValue": "null",
      "changeCategory": "User",
      "propertyChangeType": "Insert"
    }
  }
}

請參閱變更資源屬性的完整參考指南。

執行查詢

試用數據表的 resourcechanges 租使用者型 Resource Graph 查詢。 此查詢會傳回前五個最新的 Azure 資源變更,其中包含變更時間、變更類型、目標資源識別碼、目標資源類型,以及每個變更記錄的變更詳細數據。

# Login first with az login if not using Cloud Shell
 
# Run Azure Resource Graph query
az graph query -q 'resourcechanges | project properties.changeAttributes.timestamp, properties.changeType, properties.targetResourceId, properties.targetResourceType, properties.changes | limit 5'

您可以更新此查詢,為 timestamp 屬性指定更方便使用的數據行名稱

# Run Azure Resource Graph query with 'extend'
az graph query -q 'resourcechanges | extend changeTime=todatetime(properties.changeAttributes.timestamp) | project changeTime, properties.changeType, properties.targetResourceId, properties.targetResourceType, properties.changes | limit 5'

若要將查詢結果限製為最新的變更,請將查詢更新為 order by 使用者定義的 changeTime 屬性。

# Run Azure Resource Graph query with 'order by'
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'

您也可以分別使用 或 -Subscription 參數,依管理群組或訂用-ManagementGroup帳戶進行查詢。

注意

如果查詢未從您已有存取權的訂用帳戶傳回結果,則 Search-AzGraph PowerShell Cmdlet 預設為預設內容中的訂用帳戶。

Resource Graph 總管也提供一個全新的介面,可將某些查詢的結果轉換成可釘選到 Azure 儀錶板的圖表。

查詢資源變更

使用 Resource Graph,您可以查詢 resourcechangesresourcecontainerchangeshealthresourcechanges 資料表,以依任何變更資源屬性進行篩選或排序。 下列範例會查詢 resourcechanges 數據表,但也可以套用至 resourcecontainerchangeshealthresourcechanges 數據表。

注意

深入瞭解 healthresourcechanges Project Flash 檔中的數據

範例

在查詢和分析資源中的變更之前,請先檢閱下列最佳做法。

  • 在特定時段內查詢變更事件,並評估變更詳細數據。
    • 此查詢在事件管理期間最適合用來瞭解 潛在的 相關變更。
  • 讓最新設定管理資料庫 (CMDB) 保持最新狀態。
    • 您只會收到其變更,而不是以排程的頻率重新整理所有資源及其完整屬性集。
  • 瞭解資源變更「合規性狀態」時可能已變更哪些其他屬性。
    • 這些額外屬性的評估可提供其他可能需要透過 Azure 原則 定義管理的屬性見解。
  • 查詢命令的順序很重要。 在下列範例中, order by 必須位於 命令之前 limit
    • 命令 order by 會依變更時間排序查詢結果。
    • 命令 limit 接著會限制已排序的結果,以確保您得到五個最新的結果。

過去24小時期間的所有變更

resourcechanges 
| extend changeTime = todatetime(properties.changeAttributes.timestamp), targetResourceId = tostring(properties.targetResourceId),
changeType = tostring(properties.changeType), correlationId = properties.changeAttributes.correlationId, 
changedProperties = properties.changes, changeCount = properties.changeAttributes.changesCount
| where changeTime > ago(1d)
| order by changeTime desc
| project changeTime, targetResourceId, changeType, correlationId, changeCount, changedProperties

在特定資源群組中刪除的資源

resourcechanges
| where resourceGroup == "myResourceGroup"
| extend changeTime = todatetime(properties.changeAttributes.timestamp), targetResourceId = tostring(properties.targetResourceId),
changeType = tostring(properties.changeType), correlationId = properties.changeAttributes.correlationId
| where changeType == "Delete"
| order by changeTime desc
| project changeTime, resourceGroup, targetResourceId, changeType, correlationId

特定屬性值的變更

resourcechanges
| extend provisioningStateChange = properties.changes["properties.provisioningState"], changeTime = todatetime(properties.changeAttributes.timestamp), targetResourceId = tostring(properties.targetResourceId), changeType = tostring(properties.changeType)
| where isnotempty(provisioningStateChange)and provisioningStateChange.newValue == "Succeeded"
| order by changeTime desc
| project changeTime, targetResourceId, changeType, provisioningStateChange.previousValue, provisioningStateChange.newValue

過去七天內所建立資源的最新資源變更

resourcechanges
| extend targetResourceId = tostring(properties.targetResourceId), changeType = tostring(properties.changeType), changeTime = todatetime(properties.changeAttributes.timestamp)
| where changeTime > ago(7d) and changeType == "Create"
| project  targetResourceId, changeType, changeTime
| join ( Resources | extend targetResourceId=id) on targetResourceId
| order by changeTime desc
| project changeTime, changeType, id, resourceGroup, type, properties

虛擬機大小的變更

resourcechanges
|extend vmSize = properties.changes["properties.hardwareProfile.vmSize"], changeTime = todatetime(properties.changeAttributes.timestamp), targetResourceId = tostring(properties.targetResourceId), changeType = tostring(properties.changeType) 
| where isnotempty(vmSize) 
| order by changeTime desc 
| project changeTime, targetResourceId, changeType, properties.changes, previousSize = vmSize.previousValue, newSize = vmSize.newValue

變更類型和訂用帳戶名稱的變更計數

resourcechanges  
|extend changeType = tostring(properties.changeType), changeTime = todatetime(properties.changeAttributes.timestamp), targetResourceType=tostring(properties.targetResourceType)  
| summarize count() by changeType, subscriptionId 
| join (resourcecontainers | where type=='microsoft.resources/subscriptions' | project SubscriptionName=name, subscriptionId) on subscriptionId 
| project-away subscriptionId, subscriptionId1
| order by count_ desc  

使用特定標籤建立之資源的最新資源變更

resourcechanges 
|extend targetResourceId = tostring(properties.targetResourceId), changeType = tostring(properties.changeType), createTime = todatetime(properties.changeAttributes.timestamp) 
| where createTime > ago(7d) and changeType == "Create" or changeType == "Update" or changeType == "Delete"
| project  targetResourceId, changeType, createTime 
| join ( resources | extend targetResourceId=id) on targetResourceId
| where tags ['Environment'] =~ 'prod' 
| order by createTime desc 
| project createTime, id, resourceGroup, type

下一步