Share via


Azure Resource Manager 的 Azure Resource Graph 查詢範例

此頁面為 Azure Resource Manager 的 Azure Resource Graph 查詢範例集合。 如需 Azure Resource Graph 範例的完整清單,請參閱依類別 Resource Graph 範例依資料表 Resource Graph 範例

標籤的查詢範例

在資源群組上尋找具有特定標籤 (不區分大小寫) 的儲存體帳戶

類似「在資源群組上尋找具有特定標籤 (不區分大小寫) 的儲存體帳戶」查詢,但必須尋找不區分大小寫的標籤名稱和標籤值時,請使用 mv-expand 搭配 bagexpansion 參數。 此查詢使用的配額高於原始查詢,因此只在必要時才使用 mv-expand

Resources
| where type =~ 'microsoft.storage/storageaccounts'
| join kind=inner (
	ResourceContainers
	| where type =~ 'microsoft.resources/subscriptions/resourcegroups'
	| mv-expand bagexpansion=array tags
	| where isnotempty(tags)
	| where tags[0] =~ 'key1' and tags[1] =~ 'value1'
	| project subscriptionId, resourceGroup)
on subscriptionId, resourceGroup
| project-away subscriptionId1, resourceGroup1
az graph query -q "Resources | where type =~ 'microsoft.storage/storageaccounts' | join kind=inner ( ResourceContainers | where type =~ 'microsoft.resources/subscriptions/resourcegroups' | mv-expand bagexpansion=array tags | where isnotempty(tags) | where tags[0] =~ 'key1' and tags[1] =~ 'value1' | project subscriptionId, resourceGroup) on subscriptionId, resourceGroup | project-away subscriptionId1, resourceGroup1"

在資源群組上尋找具有特定標籤 (區分大小寫) 的儲存體帳戶

下列查詢使用 innerjoin 來連接儲存體帳戶與具有指定標籤名稱 (區分大小寫) 和標籤值的資源群組。

Resources
| where type =~ 'microsoft.storage/storageaccounts'
| join kind=inner (
	ResourceContainers
	| where type =~ 'microsoft.resources/subscriptions/resourcegroups'
	| where tags['Key1'] =~ 'Value1'
	| project subscriptionId, resourceGroup)
on subscriptionId, resourceGroup
| project-away subscriptionId1, resourceGroup1
az graph query -q "Resources | where type =~ 'microsoft.storage/storageaccounts' | join kind=inner ( ResourceContainers | where type =~ 'microsoft.resources/subscriptions/resourcegroups' | where tags['Key1'] =~ 'Value1' | project subscriptionId, resourceGroup) on subscriptionId, resourceGroup | project-away subscriptionId1, resourceGroup1"

列出所有標記名稱

此查詢的開頭為標記,而且會建置 JSON 物件,列出所有的唯一標記名稱及其對應的類型。

Resources
| project tags
| summarize buildschema(tags)
az graph query -q "Resources | project tags | summarize buildschema(tags)"

列出所有標籤及其值

此查詢會列出管理群組、訂用帳戶和資源上的標籤及其值。 首先,查詢會限制標籤為 isnotempty() 的資源,只在 projectmvexpandextend 中包含 tags 來限制內含的欄位,然後從屬性包取得配對資料。 接著使用 union,合併來自 ResourceContainers 的結果與來自 Resources 的相同結果,提供可擷取標籤的廣泛範圍。 最後,查詢會將結果限制為 distinct 配對資料,並排除系統隱藏的標籤。

ResourceContainers
| where isnotempty(tags)
| project tags
| mvexpand tags
| extend tagKey = tostring(bag_keys(tags)[0])
| extend tagValue = tostring(tags[tagKey])
| union (
	resources
	| where isnotempty(tags)
	| project tags
	| mvexpand tags
	| extend tagKey = tostring(bag_keys(tags)[0])
	| extend tagValue = tostring(tags[tagKey])
)
| distinct tagKey, tagValue
| where tagKey !startswith "hidden-"
az graph query -q "ResourceContainers | where isnotempty(tags) | project tags | mvexpand tags | extend tagKey = tostring(bag_keys(tags)[0]) | extend tagValue = tostring(tags[tagKey]) | union ( resources | where isnotempty(tags) | project tags | mvexpand tags | extend tagKey = tostring(bag_keys(tags)[0]) | extend tagValue = tostring(tags[tagKey]) ) | distinct tagKey, tagValue | where tagKey !startswith "hidden-""

列出具有特定標籤值的資源

我們可以根據 Azure 資源類型以外的屬性限制結果,例如標籤。 在此範例中,我們會篩選標籤名稱為 Environment 的 Azure 資源進行篩選,其值為 Internal。 若也要提供資源具有哪些標籤和其值,請將 tags 屬性新增至 project 關鍵字。

Resources
| where tags.environment=~'internal'
| project name, tags
az graph query -q "Resources | where tags.environment=~'internal' | project name, tags"

下一步