次の方法で共有


Azure Resource Graph クエリ言語の概要

Azure Resource Graph のクエリ言語では、さまざまな演算子と関数がサポートされています。 それぞれは、Kusto Query Language (KQL) に基づいて動作します。 Resource Graph で使用されるクエリ言語の詳細については、KQL のチュートリアルを参照してください。

この記事では、Resource Graph でサポートされる言語コンポーネントについて説明します。

Resource Graph テーブル

Resource Graph には、Azure Resource Manager のリソースの種類とそのプロパティに関するデータ用のテーブルがいくつか用意されています。 join 演算子で Resource Graph テーブルを使用すると、関連するリソースの種類からプロパティを取得できます。

Resource Graph テーブルでは、次の join フレーバーがサポートされています。

Resource Graph テーブル 他のテーブルに join 可能か Description
AdvisorResources Yes Includes resources related to Microsoft.Advisor.
AlertsManagementResources Yes Includes resources related to Microsoft.AlertsManagement.
AppServiceResources Yes Includes resources related to Microsoft.Web.
AuthorizationResources Yes Includes resources related to Microsoft.Authorization.
AWSResources Yes Includes resources related to Microsoft.AwsConnector.
AzureBusinessContinuityResources Yes Includes resources related to Microsoft.AzureBusinessContinuity.
ChaosResources Yes Includes resources related to Microsoft.Chaos.
CommunityGalleryResources Yes Includes resources related to Microsoft.Compute.
ComputeResources Yes Microsoft.Compute Virtual Machine Scale Sets に関連するリソースが含まれています。
DesktopVirtualizationResources Yes Includes resources related to Microsoft.DesktopVirtualization.
DnsResources Yes Includes resources related to Microsoft.Network.
EdgeOrderResources Yes Includes resources related to Microsoft.EdgeOrder.
ElasticsanResources Yes Includes resources related to Microsoft.ElasticSan.
ExtendedLocationResources Yes Includes resources related to Microsoft.ExtendedLocation.
FeatureResources Yes Includes resources related to Microsoft.Features.
GuestConfigurationResources Yes Includes resources related to Microsoft.GuestConfiguration.
HealthResourceChanges Yes Includes resources related to Microsoft.Resources.
HealthResources Yes Includes resources related to Microsoft.ResourceHealth.
InsightsResources Yes Includes resources related to Microsoft.Insights.
IoTSecurityResources Yes Includes resources related to Microsoft.IoTSecurity and Microsoft.IoTFirmwareDefense.
KubernetesConfigurationResources Yes Includes resources related to Microsoft.KubernetesConfiguration.
KustoResources Yes Includes resources related to Microsoft.Kusto.
MaintenanceResources Yes Includes resources related to Microsoft.Maintenance.
ManagedServicesResources Yes Includes resources related to Microsoft.ManagedServices.
MigrateResources Yes Includes resources related to Microsoft.OffAzure.
NetworkResources Yes Includes resources related to Microsoft.Network.
PatchAssessmentResources Yes Includes resources related to Azure Virtual Machines patch assessment Microsoft.Compute and Microsoft.HybridCompute.
PatchInstallationResources Yes Includes resources related to Azure Virtual Machines patch installation Microsoft.Compute and Microsoft.HybridCompute.
PolicyResources Yes Includes resources related to Microsoft.PolicyInsights.
RecoveryServicesResources Yes Includes resources related to Microsoft.DataProtection and Microsoft.RecoveryServices.
ResourceChanges Yes Includes resources related to Microsoft.Resources.
ResourceContainerChanges Yes Includes resources related to Microsoft.Resources.
ResourceContainers Yes 管理グループ (Microsoft.Management/managementGroups)、サブスクリプション (Microsoft.Resources/subscriptions)、およびリソース グループ (Microsoft.Resources/subscriptions/resourcegroups) のリソースの種類とデータが含まれています。
Resources Yes クエリでテーブルが定義されていない場合の既定のテーブル。 Resource Manager のリソースの種類とプロパティのほとんどはここにあります。
SecurityResources Yes Includes resources related to Microsoft.Security.
ServiceFabricResources Yes Includes resources related to Microsoft.ServiceFabric.
ServiceHealthResources Yes Includes resources related to Microsoft.ResourceHealth/events.
SpotResources Yes Includes resources related to Microsoft.Compute.
SupportResources Yes Includes resources related to Microsoft.Support.
TagsResources Yes Includes resources related to Microsoft.Resources/tagnamespaces.

リソースの種類を含むテーブルの一覧については、「Azure Resource Graph のテーブルとリソースの種類のリファレンス」を参照してください。

Note

Resources は既定のテーブルです。 Resources テーブルに対してクエリを実行する場合、join または union を使用しない限り、テーブル名を指定する必要はありません。 ただし、最初のテーブルを常にクエリに含めることをお勧めします。

各テーブルで使用できるリソースの種類を検出するには、ポータルで Resource Graph Explorer を使用します。 別の方法として、<tableName> | distinct type などのクエリを使用して、指定した Resource Graph テーブルでサポートされている、環境内に存在するリソースの種類の一覧を取得することもできます。

次のクエリは、単純な join を示しています。 The query result blends the columns together and any duplicate column names from the joined table, ResourceContainers in this example, are appended with 1. As ResourceContainers table has types for both subscriptions and resource groups, either type might be used to join to the resource from Resources table.

Resources
| join ResourceContainers on subscriptionId
| limit 1

次のクエリは、join のより複雑な使用方法を示しています。 まず、このクエリは project を使用して、Azure Key Vault コンテナーのリソースの種類の Resources からフィールドを取得します。 The next step uses join to merge the results with ResourceContainers where the type is a subscription on a property that is both in the first table's project and the joined table's project. The field rename avoids join adding it as name1 since the property already is projected from Resources. クエリの結果として、キー コンテナーが 1 つ返され、その種類と名前、場所、キー コンテナーのリソース グループ、およびそれが存在するサブスクリプションの名前が表示されます。

Resources
| where type == 'microsoft.keyvault/vaults'
| project name, type, location, subscriptionId, resourceGroup
| join (ResourceContainers | where type=='microsoft.resources/subscriptions' | project SubName=name, subscriptionId) on subscriptionId
| project type, name, location, resourceGroup, SubName
| limit 1

Note

When limiting the join results with project, the property used by join to relate the two tables, subscriptionId in the above example, must be included in project.

Extended properties

As a preview feature, some of the resource types in Resource Graph have more type-related properties available to query beyond the properties provided by Azure Resource Manager. This set of values, known as extended properties, exists on a supported resource type in properties.extended. To show resource types with extended properties, use the following query:

Resources
| where isnotnull(properties.extended)
| distinct type
| order by type asc

例:instanceView.powerState.code によって、仮想マシンの数を取得します。

Resources
| where type == 'microsoft.compute/virtualmachines'
| summarize count() by tostring(properties.extended.instanceView.powerState.code)

Resource Graph のカスタム言語要素

共有クエリ構文 (プレビュー)

As a preview feature, a shared query can be accessed directly in a Resource Graph query. このシナリオにより、標準クエリを共有クエリとして作成し、再利用することができます。 Resource Graph クエリ内で共有クエリを呼び出すには、{{shared-query-uri}} 構文を使用します。 The URI of the shared query is the Resource ID of the shared query on the Settings page for that query. この例では、共有クエリ URI は /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/SharedQueries/providers/Microsoft.ResourceGraph/queries/Count VMs by OS です。 この URI は、サブスクリプション、リソース グループ、および別のクエリで参照する共有クエリの完全な名前を指します。 このクエリは、チュートリアル:クエリの作成と共有で作成したものと同じです。

Note

共有クエリを参照するクエリを共有クエリとして保存することはできません。

例 1: 共有クエリのみを使用する:

この Resource Graph クエリの結果は、共有クエリに格納されているクエリと同じです。

{{/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/SharedQueries/providers/Microsoft.ResourceGraph/queries/Count VMs by OS}}

例 2: より大きいクエリの一部として共有クエリを含める:

このクエリでは、最初に共有クエリを使用し、次に limit を使用して結果をさらに制限します。

{{/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/SharedQueries/providers/Microsoft.ResourceGraph/queries/Count VMs by OS}}
| where properties_storageProfile_osDisk_osType =~ 'Windows'

サポートされる KQL 言語要素

Resource Graph supports a subset of KQL data types, scalar functions, scalar operators, and aggregation functions. Specific tabular operators are supported by Resource Graph, some of which have different behaviors.

サポートされているテーブル演算子/上位レベルの演算子

次に示すのは、Resource Graph でサポートされる KQL のテーブル演算子の一覧と具体的なサンプルです。

KQL Resource Graph のサンプル クエリ Notes
count カウント キー コンテナー
distinct ストレージを含むリソースの表示
extend 仮想マシンの数 (OS の種類別)
join サブスクリプション名を含むキー コンテナー Join flavors supported: innerunique, inner, leftouter, and fullouter. 1 つのクエリで 3 つの join 操作または union 操作 (または 2 つの操作の組み合わせ) を一緒にカウントする制限。そのうちの 1 つはテーブル間結合である可能性があります。 If all cross-table join use is between Resource and ResourceContainers, then three cross-table join are allowed. ブロードキャスト結合などのカスタム結合方法は使用できません。 join を使用できるテーブルについては、「Resource Graph テーブル」を参照してください。
limit パブリック IP アドレスの一覧表示 take のシノニム。 Doesn't work with Skip.
mvexpand レガシ演算子では、代わりに mv-expand を使用します。 RowLimit max of 2,000. 既定値は 128 です。
mv-expand 特定の書き込み場所を含む Azure Cosmos DB を一覧表示する RowLimit max of 2,000. 既定値は 128 です。 1 つのクエリでは mv-expand が 3 つに制限されます。
order 名前で並べ替えられたリソースの一覧表示 sort のシノニム
parse ネットワーク インターフェイスの仮想ネットワークとサブネットを取得する プロパティがある場合は、parse を使用する代わりに、プロパティに直接アクセスするのが最適です。
project 名前で並べ替えられたリソースの一覧表示
project-away 結果から列を除外する
sort 名前で並べ替えられたリソースの一覧表示 order のシノニム
summarize Azure リソースの数 簡略化された最初のページのみ
take パブリック IP アドレスの一覧表示 limit のシノニム。 Doesn't work with Skip.
top 名前とその OS の種類による最初の 5 つの仮想マシンの表示
union 2 つのクエリの結果を結合して 1 つの結果にする 使用できる単一テーブル: | union [kind= inner|outer] [withsource=ColumnName] Table。 1 つのクエリに含めることができる union レッグの数は 3 つに制限されます。 union 分岐テーブルのあいまい解決は許可されていません。 Might be used within a single table or between the Resources and ResourceContainers tables.
where ストレージを含むリソースの表示

1 つの Resource Graph SDK クエリには、3 つの join と 3 つの mv-expand 演算子という既定の制限があります。 テナントに対するこれらの制限の引き上げを要求するには、ヘルプとサポートを使用します。

To support the Open Query portal experience, Azure Resource Graph Explorer has a higher global limit than Resource Graph SDK.

Note

テーブルを右テーブルとして複数回参照することはできません。これは上限の 1 を超えています。 これを行うと、コード DisallowedMaxNumberOfRemoteTables でエラーが発生します。

Query scope

The scope of the subscriptions or management groups from which resources are returned by a query defaults to a list of subscriptions based on the context of the authorized user. If a management group or a subscription list isn't defined, the query scope is all resources, and includes Azure Lighthouse delegated resources.

クエリ対象のサブスクリプションまたは管理グループの一覧は、結果のスコープを変更するために、手動で定義できます。 たとえば、REST API の managementGroups プロパティは、管理グループ ID を受け取ります。これは、管理グループの名前とは異なります。 managementGroups を指定すると、指定した管理グループ階層内、またはその下にある最初の 10,000 件のサブスクリプションのリソースが含まれます。 managementGroupssubscriptions と同時に使用することはできません。

例: ID が My Management GroupmyMG という管理グループの階層内のすべてのリソースのクエリを実行します。

  • REST API URI

    POST https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2021-03-01
    
  • Request Body

    {
      "query": "Resources | summarize count()",
      "managementGroups": ["myMG"]
    }
    

AuthorizationScopeFilter パラメーターを使用すると、上位スコープから継承された AuthorizationResources テーブル内の Azure Policy の割り当てと Azure ロールベースのアクセス制御 (Azure RBAC) のロールの割り当てを一覧表示できます。 AuthorizationScopeFilter パラメーターには、PolicyResources テーブルと AuthorizationResources テーブルに対して次の値を使用できます。

  • AtScopeAndBelow (default if not specified): Returns assignments for the given scope and all child scopes.
  • AtScopeAndAbove: Returns assignments for the given scope and all parent scopes, but not child scopes.
  • AtScopeAboveAndBelow: Returns assignments for the given scope, all parent scopes, and all child scopes.
  • AtScopeExact: Returns assignments only for the given scope; no parent or child scopes are included.

Note

To use the AuthorizationScopeFilter parameter, be sure to use the 2021-06-01-preview or later API version in your requests.

Example: Get all policy assignments at the myMG management group and Tenant Root (parent) scopes.

  • REST API URI

    POST https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2021-06-01-preview
    
  • 要求本文の例

    {
      "options": {
        "authorizationScopeFilter": "AtScopeAndAbove"
      },
      "query": "PolicyResources | where type =~ 'Microsoft.Authorization/PolicyAssignments'",
      "managementGroups": ["myMG"]
    }
    

Example: Get all policy assignments at the mySubscriptionId subscription, management group, and Tenant Root scopes.

  • REST API URI

    POST https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2021-06-01-preview
    
  • 要求本文の例

    {
      "options": {
        "authorizationScopeFilter": "AtScopeAndAbove"
      },
      "query": "PolicyResources | where type =~ 'Microsoft.Authorization/PolicyAssignments'",
      "subscriptions": ["mySubscriptionId"]
    }
    

Escape characters

.$ を含むものなど、一部のプロパティ名はクエリ内でラップまたはエスケープする必要があります。そうしないと、プロパティ名が正しく解釈されず、期待する結果が得られません。

  • ドット (.): 角かっこを使用してプロパティ名 ['propertyname.withaperiod'] をラップします。

    Example query that wraps the property odata.type:

    where type=~'Microsoft.Insights/alertRules' | project name, properties.condition.['odata.type']
    
  • ドル記号 ($): プロパティ名の文字をエスケープします。 使うエスケープ文字は、Resource Graph を実行するシェルによって異なります。

    • Bash: Use a backslash (\) as the escape character.

      Example query that escapes the property $type in Bash:

      where type=~'Microsoft.Insights/alertRules' | project name, properties.condition.\$type
      
    • cmd: Don't escape the dollar sign ($) character.

    • PowerShell: Use a backtick (`) as the escape character.

      Example query that escapes the property $type in PowerShell:

      where type=~'Microsoft.Insights/alertRules' | project name, properties.condition.`$type
      

Next steps