進階 Resource Graph 查詢範例
使用 Azure Resource Graph 了解查詢的第一個步驟是對查詢語言的基本瞭解。 如果您還 不熟悉 Azure 數據總管,建議您檢閱基本概念,以瞭解如何撰寫您要尋找之資源的要求。
我們將逐步解說下列進階查詢:
- 顯示每個資源類型的 API 版本
- 取得虛擬機擴展集容量和大小
- 從結果中移除數據行
- 列出所有標籤名稱
- regex 比對的虛擬機
- 列出具有特定寫入位置的 Azure Cosmos DB
- 具有訂用帳戶名稱的金鑰保存庫
- 列出 SQL 資料庫 及其彈性集區
- 列出具有其網路介面和公用IP的虛擬機
- 列出虛擬機上安裝的所有擴充功能
- 在資源群組上尋找具有特定標籤的記憶體帳戶
- 將兩個查詢的結果合併成單一結果
- 取得網路介面的虛擬網路和子網
- 依電源狀態擴充屬性摘要虛擬機
如果您沒有 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
將它與 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"
金鑰保存庫與訂用帳戶名稱
下列查詢會顯示使用種類作為 leftouter的 join
複雜用法。 此查詢會將聯結的資料表限制為訂用帳戶資源,並利用 project
使其僅包含原始欄位 subscriptionId 和已重新命名為 SubName 的 name 欄位。 欄位重新命名可避免 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 資料庫 資源及其相關的彈性集區結合在一起,如果有的話。
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>」之前的識別碼部分與虛擬機器識別碼的格式相同,因此我們將此屬性用於 join
。 summarize
接著會與虛擬機器擴充功能名稱上的 make_list
搭配使用,以結合每個擴充功能的名稱,其中 id、OSName、OSType 和 VMSize 相同於單一陣列屬性。 最後,我們會使用 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"
將兩個查詢的結果合併成單一結果
下列查詢會使用 從 ResourceContainers 數據表取得結果,並將其新增至 Resources 數據表的結果。 union
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)"