Share via


Azure Resource Manager 用の Azure Resource Graph サンプル クエリ

このページは、Azure Resource Manager 用の Azure Resource Graph のサンプル クエリのコレクションです。 Azure Resource Graph のサンプルの完全な一覧については、カテゴリ別 Azure Resource Graph サンプルテーブル別 Resource Graph サンプルに関する記事を参照してください。

タグのサンプル クエリ

リソース グループ上の特定の大文字と小文字が区別されないタグを含んだストレージ アカウントを検索する

'リソースグループ クエリで大文字と小文字が区別されるタグを持つストレージアカウントを検索する' ことに似ていますが、大文字と小文字が区別されないタグ名とタグ値を探す必要がある場合は、mv-expand パラメーターを指定して mv-expand を使用します。 このクエリでは、元のクエリよりも多くのクォータが使用されるため、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() であるリソースに制限し、project 内の isnotempty() のみを含めることで、含まれるフィールドを制限します。さらに、mvexpand および extend でプロパティ バッグからペアのデータを取得します。 次に、union を使用して、union の結果を 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 リソースの種類以外のプロパティによる結果を制限することができます。 この例では、Internal という値を持つ、Environment というタグ名の Azure リソースをフィルターで抽出しています。 そのリソースが持っているタグとその値も得る必要がある場合は、project キーワードに tags プロパティを追加します。

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

次のステップ