共用方式為


進階 Resource Graph 查詢範例

重要

本文的查詢範例和內容不再更新或支援。 入口網站的 Azure Resource Graph 總管包含支援的類別數據表,而 [開始使用] 索引標籤包含您可以在 ARG 總管中開啟和執行的範例查詢和進階查詢

透過 Azure Resource Graph 了解查詢的第一個步驟是對查詢語言進行基本的認識。 如果您還不熟悉 Azure 資料總管,建議檢閱基本概念,以了解如何撰寫所需資源的要求。

我們將逐步解說下列進階查詢︰

如尚未擁有 Azure 訂用帳戶,請在開始之前先建立免費帳戶

語言支援

Azure CLI (透過擴充功能) 與 Azure PowerShell (透過模組) 支援 Azure Resource Graph。 在執行下列任何查詢之前,請檢查您的環境已準備就緒。 請參閱 Azure CLIAzure 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 來與 ResourceContainers 資料表中的訂用帳戶詳細資料結合,然後使用 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 比對虛擬機器

此查詢會尋找符合規則運算式 (亦稱為 RegEx) 的虛擬機器。 matches regex @ 可讓我們定義要比對的 RegEx,也就是 ^Contoso(.*)[0-9]+$。 該 RegEx 定義說明如下:

  • ^:比對必須從字串的開頭開始。
  • Contoso:區分大小寫的字串。
  • (.*):子運算式比對:
    • .:比對任何單一字元 (除了新行符號)。
    • *:比對前一個元素零或多次。
  • [0-9]:比對從 0 到 9 的字元群組。
  • +:比對前一個元素一或多次。
  • $:前一個元素的比對必須發生在字串結尾。

在依名稱比對之後,查詢會投射名稱,並依名稱遞增排序。

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 來展開 properties.writeLocations 的屬性包,然後預測特定欄位並將結果進一步限制為符合「美國東部」或「美國西部」的 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"

金鑰保存庫與訂用帳戶名稱

下列查詢會顯示使用種類作為 leftouterjoin 複雜用法。 此查詢會將聯結的資料表限制為訂用帳戶資源,並利用 project 使其僅包含原始欄位 subscriptionId 和已重新命名為 SubNamename 欄位。 欄位重新命名可避免 join 將它新增為 name1,因為此欄位已經存在於 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 Database 和其彈性集區

下列查詢使用 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

此查詢會使用兩個 [leftouter]join 命令,將使用 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()) 識別碼取得識別碼、取得作業系統名稱和類型,以及取得虛擬機器大小。 以大寫取得資源識別碼是準備加入另一個屬性的好方法。 然後,此查詢會使用 join 搭配 kind 做為 leftouter,藉由比對擴充功能識別碼的大寫 substring 來取得虛擬機器擴充功能。 「/extensions/<ExtensionName>」之前的識別碼部分與虛擬機器識別碼的格式相同,因此我們將此屬性用於 joinsummarize 接著會與虛擬機器擴充功能名稱上的 make_list 搭配使用,以結合每個擴充功能的名稱,其中 idOSNameOSTypeVMSize 相同於單一陣列屬性。 最後,我們 order by 小寫的 OSNameasc。 根據預設,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"

若需要尋找不區分大小寫的標籤名稱和標籤值,請使用 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"

將兩個查詢的結果合併成單一結果

下列查詢會使用 unionResourceContainers 資料表取得結果,然後將其新增至 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)"

取得網路介面的虛擬網路和子網路

使用規則運算式 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)"

下一步