Resource Graph の初歩的なクエリのサンプル

Azure Resource Graph でクエリを理解する最初の手順は、クエリ言語の基本を理解することです。 Kusto Query Language (KQL) にまだ慣れていない場合は、KQL チュートリアルを参照し、目的のリソースへの要求を作成する方法を理解することをお勧めします。

この記事では、次のスターター クエリを使用します:

Azure サブスクリプションをお持ちでない場合は、始める前に無料アカウントを作成してください。

言語のサポート

Azure CLI (拡張経由) および Azure PowerShell (モジュール経由) は、Azure Resource Graph をサポートします。 次のクエリを実行する前に、環境が準備できていることを確認します。 選択するシェル環境をインストールし、検証する手順については、Azure CLI および Azure PowerShell を参照してください。

Azure リソースの数

このクエリは、あなたがアクセスしたサブスクリプションに存在する Azure リソースの数を返します。 また、選択するシェルに適切な Azure Resource Graph コンポーネントがインストールされ、正常に動作していることを検証するために使用する場合にも有効なクエリです。

Resources
| summarize count()

既定では、Azure CLI はアクセス可能なすべてのサブスクリプションに対してクエリを実行しますが、--subscriptions パラメーターを指定して特定のサブスクリプションに対してクエリを実行できます。

az graph query -q "Resources | summarize count()"

この例では、サブスクリプション ID に 変数を使用します。

subid=$(az account show --query id --output tsv)
az graph query -q "Resources | summarize count()" --subscriptions $subid

管理グループとテナントのスコープでクエリを実行することもできます。 <managementGroupId><tenantId> を独自の値に置き換えます。

az graph query -q "Resources | summarize count()" --management-groups '<managementGroupId>'
az graph query -q "Resources | summarize count()" --management-groups '<tenantId>'

テナント ID に変数を使用することもできます。

tenantid=$(az account show --query tenantId --output tsv)
az graph query -q "Resources | summarize count()" --management-groups $tenantid

Key Vault リソース数をカウントする

このクエリでは、summarize ではなく count を使用して、返されるレコードの数をカウントします。 カウントに含まれるのは、キー コンテナーだけです。

Resources
| where type =~ 'microsoft.keyvault/vaults'
| count
az graph query -q "Resources | where type =~ 'microsoft.keyvault/vaults' | count"

名前で並べ替えられたリソースの一覧表示

このクエリは、あらゆる種類のリソースを返しますが、返されるプロパティは nametypelocation に限られます。 order by を使用することにより、これらのプロパティが order by プロパティの昇順 (asc) で並べ替えらえます。

Resources
| project name, type, location
| order by name asc
az graph query -q "Resources | project name, type, location | order by name asc"

降順の名前で順序付けられたすべての仮想マシンの表示

仮想マシン (Microsoft.Compute/virtualMachines型) のみを一覧表示したければ、結果において Microsoft.Compute/virtualMachines プロパティと突き合わせます。 前のクエリと同様に、descorder by を降順に変更します。 =~の種類一致により、Resource Graph では大文字と小文字が区別されないことを示します。

Resources
| project name, location, type
| where type =~ 'Microsoft.Compute/virtualMachines'
| order by name desc
az graph query -q "Resources | project name, location, type| where type =~ 'Microsoft.Compute/virtualMachines' | order by name desc"

名前とその OS の種類による最初の 5 つの仮想マシンの表示

このクエリでは、名前別に整理された 5 つの一致するレコードを取得するためのみに top が使用されます。 Azure リソースの種類は Microsoft.Compute/virtualMachines です。 project により、Azure Resource Graph に含めるプロパティを説明します。

Resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| project name, properties.storageProfile.osDisk.osType
| top 5 by name desc
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | project name, properties.storageProfile.osDisk.osType | top 5 by name desc"

仮想マシンの数 (OS の種類別)

前のクエリに基づき、Azure リソースの種類 Microsoft.Compute/virtualMachines によりまだ制限していますが、返されるレコード数を制限しなくなっています。 代わりに、プロパティで値をグループ化し、集計する方法を定義するためにsummarize および count() を使用しました。この例では、properties.storageProfile.osDisk.osTypeです。 この文字列が完全なオブジェクトにおいてどのように見えるかという例については、 リソースの確認‐仮想マシンの検出 を参照してください。

Resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| summarize count() by tostring(properties.storageProfile.osDisk.osType)
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by tostring(properties.storageProfile.osDisk.osType)"

同じクエリを記述する別の方法は、プロパティを extend し、クエリ内で使用するために一時的に名前を付けます。この場合は os です。前の例に示すように、ossummarize および count() により使用されます。

Resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| extend os = properties.storageProfile.osDisk.osType
| summarize count() by tostring(os)
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | extend os = properties.storageProfile.osDisk.osType | summarize count() by tostring(os)"

注意

=~ によりプロパティの大文字と小文字を区別しないことが可能である一方、クエリにおけるプロパティの使用 (properties.storageProfile.osDisk.osType 等) には正しいケースを必要とすることにご注意ください。 プロパティが正しくない場合、null 値または間違った値が返され、グループ化または集計が不正確になる可能性があります。

ストレージを含むリソースの表示

一致する種類を明示的に定義する代わりに、このクエリの例では、ストレージ という単語を contains 含む任意の Azure リソースが見つかります。

Resources
| where type contains 'storage' | distinct type
az graph query -q "Resources | where type contains 'storage' | distinct type"

Azure 仮想ネットワーク サブネットの一覧

このクエリは、サブネット名とアドレス プレフィックスを含む Azure 仮想ネットワーク (VNet) の一覧を返します。

Resources
| where type == 'microsoft.network/virtualnetworks'
| extend subnets = properties.subnets
| mv-expand subnets
| project name, subnets.name, subnets.properties.addressPrefix, location, resourceGroup, subscriptionId
az graph query -q "Resources | where type == 'microsoft.network/virtualnetworks' | extend subnets = properties.subnets | mv-expand subnets | project name, subnets.name, subnets.properties.addressPrefix, location, resourceGroup, subscriptionId"

すべてのパブリック IP アドレスの一覧表示

前のクエリと同様に、type に publicIPAddresses という語を含むものをすべて検索します。 このクエリは、そのパターンを拡張して、properties.ipAddressisnotempty の場合にのみ結果を取得し、properties.ipAddress のみを返し、結果を上位 100 件に制限 (limit) します。選択したシェルによって引用符のエスケープが必要となる場合があります。

Resources
| where type contains 'publicIPAddresses' and isnotempty(properties.ipAddress)
| project properties.ipAddress
| limit 100
az graph query -q "Resources | where type contains 'publicIPAddresses' and isnotempty(properties.ipAddress) | project properties.ipAddress | limit 100"

サブスクリプションで構成されている IP アドレスを持つリソースの数

前例のクエリを使用し、summarize および count() を追加して、構成された IP アドレスをもつリソースのサブスクリプションにより一覧を取得できます。

Resources
| where type contains 'publicIPAddresses' and isnotempty(properties.ipAddress)
| summarize count () by subscriptionId
az graph query -q "Resources | where type contains 'publicIPAddresses' and isnotempty(properties.ipAddress) | summarize count () by subscriptionId"

特定のタグ値が付いたリソースの一覧表示

タグなどの Azure リソースの種類以外のプロパティによる結果を制限することができます。 この例では、Internal という値を持つ、Environment というタグ名の Azure リソースをフィルターで抽出しています。

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

そのリソースが持っているタグとその値も得る必要がある場合は、project キーワードに tags プロパティを追加します。

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

特定のタグ値を持つすべてのストレージ アカウントの一覧表示

前の例のフィルター機能を組み合わせて、type プロパティで Azure リソースの種類をフィルター処理してみましょう。 このクエリでは、特定の種類の Azure リソースを抽出する検索が、さらに特定のタグ名と値で制限されています。

Resources
| where type =~ 'Microsoft.Storage/storageAccounts'
| where tags['tag with a space']=='Custom value'
az graph query -q "Resources | where type =~ 'Microsoft.Storage/storageAccounts' | where tags['tag with a space']=='Custom value'"

注意

この例では=~の条件付きの代わりに、==をマッチングに使用しています。 == は大文字小文字が区別します。

すべてのタグとその値の一覧表示

このクエリは、管理グループ、サブスクリプション、およびリソースのタグとその値を一覧表示します。 クエリでは、まず、タグが 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 notempty(tags) | project tags | mvexpand tags | extend tagKey = tostring(bag_keys(tags)[0]) | extend tagValue = tostring(tags[tagKey]) ) | distinct tagKey, tagValue | where tagKey !startswith "hidden-""

関連付けられていないネットワーク セキュリティ グループの表示

このクエリでは、ネットワーク インターフェイスまたはサブネットに関連付けられていないネットワーク セキュリティ グループ (NSG) が返されます。

Resources
| where type =~ "microsoft.network/networksecuritygroups" and isnull(properties.networkInterfaces) and isnull(properties.subnets)
| project name, resourceGroup
| sort by name asc
az graph query -q "Resources | where type =~ 'microsoft.network/networksecuritygroups' and isnull(properties.networkInterfaces) and isnull(properties.subnets) | project name, resourceGroup | sort by name asc"

重大度順に並べ替えられた Azure Monitor アラートを一覧表示する

このクエリでは、alertsmanagementresources テーブルを使用して、重大度別に並べ替えられた Azure Monitor アラートを返します。

alertsmanagementresources
| where type =~ 'microsoft.alertsmanagement/alerts'
| where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now()
| project Severity = tostring(properties.essentials.severity)
| summarize AlertsCount = count() by Severity
az graph query -q "alertsmanagementresources | where type =~ 'microsoft.alertsmanagement/alerts' | where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now() | project Severity = tostring(properties.essentials.severity) | summarize AlertsCount = count() by Severity"

重大度とアラート状態順に並べ替えられた Azure Monitor アラートを一覧表示する

このクエリでは、alertsmanagementresources テーブルを使用して、重大度とアラートの状態別に並べ替えられた Azure Monitor アラートを返します。

alertsmanagementresources
| where type =~ 'microsoft.alertsmanagement/alerts'
| where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now()
| project Severity = tostring(properties.essentials.severity), AlertState = tostring(properties.essentials.alertState)
| summarize AlertsCount = count() by Severity, AlertState
az graph query -q "alertsmanagementresources | where type =~ 'microsoft.alertsmanagement/alerts' | where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now() | project Severity = tostring(properties.essentials.severity), AlertState = tostring(properties.essentials.alertState) | summarize AlertsCount = count() by Severity, AlertState"

重大度、監視サービス、ターゲット リソースの種類別に並べ替えられた Alert Monitor アラートを一覧表示する

このクエリでは、alertsmanagementresources テーブルを使用して、重大度、監視サービス、ターゲット リソースの種類別に並べ替えられた Alert Monitor アラートを返します。

alertsmanagementresources
| where type =~ 'microsoft.alertsmanagement/alerts'
| where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now()
| project Severity = tostring(properties.essentials.severity),
MonitorCondition = tostring(properties.essentials.monitorCondition),
ObjectState = tostring(properties.essentials.alertState),
MonitorService = tostring(properties.essentials.monitorService),
AlertRuleId = tostring(properties.essentials.alertRule),
SignalType = tostring(properties.essentials.signalType),
TargetResource = tostring(properties.essentials.targetResourceName),
TargetResourceType = tostring(properties.essentials.targetResourceName), id
| summarize AlertsCount = count() by Severity, MonitorService , TargetResourceType
az graph query -q "alertsmanagementresources | where type =~ 'microsoft.alertsmanagement/alerts' | where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now() | project Severity = tostring(properties.essentials.severity), MonitorCondition = tostring(properties.essentials.monitorCondition), ObjectState = tostring(properties.essentials.alertState), MonitorService = tostring(properties.essentials.monitorService), AlertRuleId = tostring(properties.essentials.alertRule), SignalType = tostring(properties.essentials.signalType), TargetResource = tostring(properties.essentials.targetResourceName), TargetResourceType = tostring(properties.essentials.targetResourceName), id | summarize AlertsCount = count() by Severity, MonitorService , TargetResourceType"

次のステップ