Resource Graph の高度なクエリのサンプル

Azure Resource Graph でクエリを理解する最初の手順は、クエリ言語の基本を理解することです。 Azure Data Explorer にまだ精通していない場合、探しているリソースに対する要求を作成する方法を理解するための基礎を確認することをお勧めします。

次の高度なクエリを説明します。

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

言語のサポート

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

リソースの種類と API バージョンを表示する

Resource Graph は、主にリソース プロバイダー API の最新の非プレビュー バージョンを使用して、更新中にリソース プロパティの GET を行います。 場合によっては、使用される API バージョンがオーバーライドされ、最新のプロパティまたは広く使用されているプロパティが結果に提供されます。 次のクエリは、リソースの種類ごとにプロパティを収集するために使用される API バージョンの詳細を示しています。

Resources
| distinct type, apiVersion
| where isnotnull(apiVersion)
| order by type asc
az graph query -q "Resources | distinct type, apiVersion | where isnotnull(apiVersion) | order by type asc"

仮想マシン スケール セットの容量とサイズを取得する

このクエリでは、仮想マシン スケール セットのリソースを検索し、仮想マシンのサイズ、スケール セットの容量などのさまざまな詳細情報を取得します。 このクエリは、toint() 関数を使用して、容量を分類できるよう数値にキャストしています。 最後に、列の名前をカスタムの名前付きプロパティに変更します。

Resources
| where type=~ 'microsoft.compute/virtualmachinescalesets'
| where name contains 'contoso'
| project subscriptionId, name, location, resourceGroup, Capacity = toint(sku.capacity), Tier = sku.name
| order by Capacity desc
az graph query -q "Resources | where type=~ 'microsoft.compute/virtualmachinescalesets' | where name contains 'contoso' | project subscriptionId, name, location, resourceGroup, Capacity = toint(sku.capacity), Tier = sku.name | order by Capacity desc"

結果から列を除外する

次のクエリは、summarize を使用してサブスクリプションごとにリソースをカウントし、join を使用して、summarize テーブルに格納されているサブスクリプションの詳細と結合した後、project-away を使用して一部の列を除外しています。

Resources
| summarize resourceCount=count() by subscriptionId
| join (ResourceContainers | where type=='microsoft.resources/subscriptions' | project SubName=name, subscriptionId) on subscriptionId
| project-away subscriptionId, subscriptionId1
az graph query -q "Resources | summarize resourceCount=count() by subscriptionId | join (ResourceContainers | where type=='microsoft.resources/subscriptions' | project SubName=name, subscriptionId) on subscriptionId| project-away subscriptionId, subscriptionId1"

すべてのタグ名を一覧表示する

このクエリは、タグを使用して開始し、全ての独自のタグ名とその対応する種類を一覧表示する JSON オブジェクトを構築します。

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

正規表現に一致する仮想マシン

このクエリは、正規表現 (regex と呼ばれる) に一致する仮想マシンを検索します。 matches regex @ では、一致させる regex を定義することができます。ここでは ^Contoso(.*)[0-9]+$ を指定しています。 その regex の定義は以下のように説明されています。

  • ^ - 一致は、文字列の先頭から始まる必要があります。
  • Contoso - 文字列。大文字と小文字は区別されます。
  • (.*) - 部分式一致:
    • . - 任意の 1 文字との一致 (新しい行を除く)。
    • * - 「直前の要素 0 回以上」との一致。
  • [0-9] - 文字グループは 0 ~ 9 の数字に一致。
  • + - 「直前の要素 1 回以上」との一致。
  • $ - 直前の要素との一致は、文字列の最後に発生する必要があります。

名前で一致した後、クエリはプロジェクトの名前を提示し、名前の昇順で順序付けします。

Resources
| where type =~ 'microsoft.compute/virtualmachines' and name matches regex @'^Contoso(.*)[0-9]+$'
| project name
| order by name asc
az graph query -q "Resources | where type =~ 'microsoft.compute/virtualmachines' and name matches regex @'^Contoso(.*)[0-9]+\$' | project name | order by name asc"

特定の書き込み場所を含む Azure Cosmos DB を一覧表示する

次のクエリは、Azure Cosmos DB リソースに対象を限定し、mv-expand を使用して mv-expand のプロパティ バッグを展開した後、特定のフィールドを投影して、さらに "East US" または "West US" と一致する properties.writeLocations.locationName 値に結果を限定します。

Resources
| where type =~ 'microsoft.documentdb/databaseaccounts'
| project id, name, writeLocations = (properties.writeLocations)
| mv-expand writeLocations
| project id, name, writeLocation = tostring(writeLocations.locationName)
| where writeLocation in ('East US', 'West US')
| summarize by id, name
az graph query -q "Resources | where type =~ 'microsoft.documentdb/databaseaccounts' | project id, name, writeLocations = (properties.writeLocations) | mv-expand writeLocations | project id, name, writeLocation = tostring(writeLocations.locationName) | where writeLocation in ('East US', 'West US') | summarize by id, name"

サブスクリプション名を含むキー コンテナー

次のクエリは、joinleftouterである join の複雑な使用方法を示しています。 このクエリは、結合対象のテーブルをサブスクリプション リソースに制限し、さらに、project を使用して、元のフィールドである project と、SubName という名前に変更された name フィールドのみが含まれるように制限しています。 フィールド名を変更することにより、join でフィールドが join として追加されるのを防いでいます。resources には、このフィールドが既に存在するためです。 元のテーブルは where でフィルター処理され、次の project には両方のテーブルの列が含まれます。 クエリの結果として、すべてのキー コンテナーが返されると共に、そのタイプと名前、そしてそれが存在するサブスクリプションの名前が表示されます。

Resources
| join kind=leftouter (ResourceContainers | where type=='microsoft.resources/subscriptions' | project SubName=name, subscriptionId) on subscriptionId
| where type == 'microsoft.keyvault/vaults'
| project type, name, SubName
az graph query -q "Resources | join kind=leftouter (ResourceContainers | where type=='microsoft.resources/subscriptions' | project SubName=name, subscriptionId) on subscriptionId | where type == 'microsoft.keyvault/vaults' | project type, name, SubName"

SQL データベースとそのエラスティック プールを一覧表示する

次のクエリでは、leftouterjoin を使用して、SQL Database リソースと (存在する場合) それに関連するエラスティック プールを結合します。

Resources
| where type =~ 'microsoft.sql/servers/databases'
| project databaseId = id, databaseName = name, elasticPoolId = tolower(tostring(properties.elasticPoolId))
| join kind=leftouter (
    Resources
    | where type =~ 'microsoft.sql/servers/elasticpools'
    | project elasticPoolId = tolower(id), elasticPoolName = name, elasticPoolState = properties.state)
on elasticPoolId
| project-away elasticPoolId1
az graph query -q "Resources | where type =~ 'microsoft.sql/servers/databases' | project databaseId = id, databaseName = name, elasticPoolId = tolower(tostring(properties.elasticPoolId)) | join kind=leftouter ( Resources | where type =~ 'microsoft.sql/servers/elasticpools' | project elasticPoolId = tolower(id), elasticPoolName = name, elasticPoolState = properties.state) on elasticPoolId | project-away elasticPoolId1"

仮想マシンとそのネットワーク インターフェイスおよびパブリック IP を一覧表示する

このクエリでは、2 つの leftouterjoin コマンドを使用して、Resource Manager デプロイ モデルで作成された仮想マシン、それに関連するネットワーク インターフェイス、さらに、それらのネットワーク インターフェイスに関連付けられたパブリック IP アドレスを結合します。

Resources
| where type =~ 'microsoft.compute/virtualmachines'
| extend nics=array_length(properties.networkProfile.networkInterfaces)
| mv-expand nic=properties.networkProfile.networkInterfaces
| where nics == 1 or nic.properties.primary =~ 'true' or isempty(nic)
| project vmId = id, vmName = name, vmSize=tostring(properties.hardwareProfile.vmSize), nicId = tostring(nic.id)
| join kind=leftouter (
    Resources
    | where type =~ 'microsoft.network/networkinterfaces'
    | extend ipConfigsCount=array_length(properties.ipConfigurations)
    | mv-expand ipconfig=properties.ipConfigurations
    | where ipConfigsCount == 1 or ipconfig.properties.primary =~ 'true'
    | project nicId = id, publicIpId = tostring(ipconfig.properties.publicIPAddress.id))
on nicId
| project-away nicId1
| summarize by vmId, vmName, vmSize, nicId, publicIpId
| join kind=leftouter (
    Resources
    | where type =~ 'microsoft.network/publicipaddresses'
    | project publicIpId = id, publicIpAddress = properties.ipAddress)
on publicIpId
| project-away publicIpId1
az graph query -q "Resources | where type =~ 'microsoft.compute/virtualmachines' | extend nics=array_length(properties.networkProfile.networkInterfaces) | mv-expand nic=properties.networkProfile.networkInterfaces | where nics == 1 or nic.properties.primary =~ 'true' or isempty(nic) | project vmId = id, vmName = name, vmSize=tostring(properties.hardwareProfile.vmSize), nicId = tostring(nic.id) | join kind=leftouter ( Resources | where type =~ 'microsoft.network/networkinterfaces' | extend ipConfigsCount=array_length(properties.ipConfigurations) | mv-expand ipconfig=properties.ipConfigurations | where ipConfigsCount == 1 or ipconfig.properties.primary =~ 'true' | project nicId = id, publicIpId = tostring(ipconfig.properties.publicIPAddress.id)) on nicId | project-away nicId1 | summarize by vmId, vmName, vmSize, nicId, publicIpId | join kind=leftouter ( Resources | where type =~ 'microsoft.network/publicipaddresses' | project publicIpId = id, publicIpAddress = properties.ipAddress) on publicIpId | project-away publicIpId1"

仮想マシンにインストールされているすべての拡張機能を一覧表示する

まず、このクエリでは、仮想マシンのリソースの種類で extend を使用して大文字 (toupper()) で ID を取得し、オペレーティング システムの名前と種類を取得し、仮想マシンのサイズを取得します。 他のプロパティとの結合を準備するには、大文字のリソース ID を取得することをお勧めします。 次に、このクエリでは、kindleftouter を指定した join を使用して、拡張機能 ID の大文字の substring と一致させることによって、仮想マシン拡張機能を取得します。 "/extensions/<ExtensionName>" の前の ID の部分は仮想マシン ID と同じ形式であるため、join にはこのプロパティを使用します。 次に、仮想マシン拡張機能の名前に対して summarizemake_list と共に使用して、同じ idOSNameOSType、および VMSize を持つ各拡張機能の名前を 1 つの配列プロパティに結合します。 最後に、asc を指定して、小文字の OSName に対して order by を使用します。 既定では、order by は降順です。

Resources
| where type == 'microsoft.compute/virtualmachines'
| extend
    JoinID = toupper(id),
    OSName = tostring(properties.osProfile.computerName),
    OSType = tostring(properties.storageProfile.osDisk.osType),
    VMSize = tostring(properties.hardwareProfile.vmSize)
| join kind=leftouter(
    Resources
    | where type == 'microsoft.compute/virtualmachines/extensions'
    | extend
        VMId = toupper(substring(id, 0, indexof(id, '/extensions'))),
        ExtensionName = name
) on $left.JoinID == $right.VMId
| summarize Extensions = make_list(ExtensionName) by id, OSName, OSType, VMSize
| order by tolower(OSName) asc
az graph query -q "Resources | where type == 'microsoft.compute/virtualmachines' | extend JoinID = toupper(id), OSName = tostring(properties.osProfile.computerName), OSType = tostring(properties.storageProfile.osDisk.osType), VMSize = tostring(properties.hardwareProfile.vmSize) | join kind=leftouter( Resources | where type == 'microsoft.compute/virtualmachines/extensions' | extend VMId = toupper(substring(id, 0, indexof(id, '/extensions'))), ExtensionName = name ) on \$left.JoinID == \$right.VMId | summarize Extensions = make_list(ExtensionName) by id, OSName, OSType, VMSize | order by tolower(OSName) asc"

リソース グループ上の特定のタグを含んだストレージ アカウントを検索する

次のクエリでは、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"

大文字と小文字が区別されないタグ名とタグ値を探す必要がある場合は、bagexpansion パラメーターを指定して 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"

2 つのクエリの結果を結合して 1 つの結果にする

次のクエリは、union を使用して ResourceContainers テーブルから結果を取得し、Resources テーブルから得た結果にそれらを追加します。

ResourceContainers
| where type=='microsoft.resources/subscriptions/resourcegroups' | project name, type  | limit 5
| union  (Resources | project name, type | limit 5)
az graph query -q "ResourceContainers | where type=='microsoft.resources/subscriptions/resourcegroups' | project name, type  | limit 5 | union  (Resources | project name, type | limit 5)"

ネットワーク インターフェイスの仮想ネットワークとサブネットを取得する

リソース ID プロパティから仮想ネットワークとサブネットの名前を取得するには、正規表現 parse を使用します。 parse を使用すると、複雑なフィールドからデータを取得することができますが、プロパティが存在する場合は、parse を使用せずに直接アクセスすることをお勧めします。

Resources
| where type =~ 'microsoft.network/networkinterfaces'
| project id, ipConfigurations = properties.ipConfigurations
| mvexpand ipConfigurations
| project id, subnetId = tostring(ipConfigurations.properties.subnet.id)
| parse kind=regex subnetId with '/virtualNetworks/' virtualNetwork '/subnets/' subnet
| project id, virtualNetwork, subnet
az graph query -q "Resources | where type =~ 'microsoft.network/networkinterfaces' | project id, ipConfigurations = properties.ipConfigurations | mvexpand ipConfigurations | project id, subnetId = tostring(ipConfigurations.properties.subnet.id) | parse kind=regex subnetId with '/virtualNetworks/' virtualNetwork '/subnets/' subnet | project id, virtualNetwork, subnet"

電源状態の拡張プロパティ別に仮想マシンを集計する

このクエリは、仮想マシンに対する拡張プロパティを使用して、電源状態別に集計します。

Resources
| where type == 'microsoft.compute/virtualmachines'
| summarize count() by tostring(properties.extended.instanceView.powerState.code)
az graph query -q "Resources | where type == 'microsoft.compute/virtualmachines' | summarize count() by tostring(properties.extended.instanceView.powerState.code)"

次のステップ