نماذج استعلام بيان الموارد المتقدمة

هام

لم يعد يتم تحديث نماذج الاستعلام والمحتوى الخاص بهذه المقالة أو دعمه. يتضمن Azure Resource Graph Explorer الخاص بالمدخل الفئات والجداول المدعومة، وتتضمن علامة التبويب بدء الاستخدام أمثلة على الاستعلامات والاستعلامات المتقدمة التي يمكنك فتحها وتشغيلها في ARG Explorer.

الخطوة الأولى لفهم الاستعلامات باستخدام بيان مورد Azure هي الفهم الأساسي للغة الاستعلام . إذا لم تكن مطلع على مستكشف بيانات Azure بالفعل، فمن المستحسن مراجعة الأساسيات لفهم كيفية إنشاء طلبات الموارد التي تبحث عنها.

سنتصفح الاستعلامات المتقدمة التالية:

في حال لم يكن لديك اشتراك Azure، فأنشئ حساباً مجانيّاً قبل البدء.

الدعم اللغوي

يدعم Azure CLI (من خلال ملحق) وAzure PowerShell (من خلال وحدة) بيان مورد Azure. قبل تشغيل أي من الاستعلامات التالية، تحقق من أن البيئة جاهزة. راجع Azure CLI وAzure PowerShellلمعرفة خطوات التثبيت والتحقق من صحة بيئة shell التي تختارها.

إظهار أنواع الموارد وإصدارات 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"

خزائن المفاتيح باسم الاشتراك

يوضح الاستعلام التالي استخدامًا معقدًا لـ⁧join⁩ مع نوع ⁧⁩⁧⁩ في ⁧⁩leftouter⁧⁩. يحد الاستعلام من الجدول المرتبط بموارد الاشتراكات مع project لتضمين الحقل الأصلي subscriptionId وحقل الاسم الذي تمت إعادة تسميته إلى SubName. تتجنب إعادة تسمية الحقل ⁧join⁩ إضافته كـ ⁧⁩name1⁧⁩؛ لأن الحقل موجود بالفعل في ⁧⁩الموارد⁧⁩. تمت تصفية الجدول الأصلي باستخدام 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 وتجمعاتها المرنة

يستخدم الاستعلام التالي leftouter join لتجميع موارد قاعدة بيانات 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 متشابهة في خاصية صفيف واحد. وأخيرًا، نحن order byبالأحرف الصغيرةOSNameمع تصاعدي. بشكل افتراضي، 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"

البحث عن حسابات تخزين ذات علامة معينة في مجموعة الموارد

يستخدم الاستعلام التالي دالة داخلية join لربط حسابات التخزين بمجموعات الموارد ذات العلامة الحساسة للأحرف المحددة وقيمة العلامة.

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"

دمج النتائج من استعلامين في نتيجة واحدة

يستخدم الاستعلام التالي 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)"

الحصول على شبكات ظاهرية وشبكات فرعية لواجهات الشبكة

استخدم أحد التعبيرات العادية ⁧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)"

الخطوات التالية