نماذج استعلام Starter Resource Graph

هام

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

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

تستخدم هذه المقالة استعلامات البداية التالية:

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

الدعم اللغوي

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

حساب عدد موارد Azure

يُرجع هذا الاستعلام عدد موارد Azure الموجودة في الاشتراكات التي لديك حق الوصول إليها. إنه أيضًا استعلام جيد للتحقق من أن واجهة shell الذي تختاره يحتوي على مكونات Azure Resource Graph المناسبة المثبتة وموجودة في ترتيب العمل المطلوب.

Resources
| summarize count()

بشكل افتراضي، يستعلم Azure CLI عن جميع الاشتراكات التي يمكن الوصول إليها ولكن يمكنك تحديد المعلمة --subscriptions للاستعلام عن اشتراكات معينة.

az graph query -q "Resources | summarize count()"

يستخدم هذا المثال متغيرا لمعرف الاشتراك.

subid=$(az account show --query id --output tsv)
az graph query -q "Resources | summarize count()" --subscriptions $subid

يمكنك أيضا الاستعلام حسب نطاقات مجموعة الإدارة والمستأجر. الاستبدال <managementGroupId> و<tenantId> وكذلك من خلال القيم الخاصة بك.

az graph query -q "Resources | summarize count()" --management-groups '<managementGroupId>'
az graph query -q "Resources | summarize count()" --management-groups '<tenantId>'

يمكنك أيضا استخدام متغير لمعرف المستأجر.

tenantid=$(az account show --query tenantId --output tsv)
az graph query -q "Resources | summarize count()" --management-groups $tenantid

حساب موارد Key Vault

هذا الاستعلام يستخدم count بدلاً من summarize لحساب عدد السجلات التي تم إرجاعها. يتم تضمين خزائن المفاتيح فقط في الجرد.

Resources
| where type =~ 'microsoft.keyvault/vaults'
| count
az graph query -q "Resources | where type =~ 'microsoft.keyvault/vaults' | count"

سرد الموارد مرتبة حسب الاسم

يعرض هذا الاستعلام أي نوع من الموارد، ولكن فقط خصائص الاسم والنوع والموقع. يستخدم order by لفرز الخصائص حسب خاصية name بترتيب تصاعدي (asc).

Resources
| project name, type, location
| order by name asc
az graph query -q "Resources | project name, type, location | order by name asc"

عرض جميع الأجهزة الظاهرية مرتبة حسب الاسم بترتيب تنازلي

لسرد الأجهزة الظاهرية فقط (من النوع Microsoft.Compute/virtualMachines)، يمكننا مطابقة خاصية النوع في النتائج. على غرار طلب البحث السابق، قام desc بتغيير order by ليكون تنازليًّا. يخبر =~ في النوع المطابق Resource Graph أن يكون غير حساس لحالة الأحرف.

Resources
| project name, location, type
| where type =~ 'Microsoft.Compute/virtualMachines'
| order by name desc
az graph query -q "Resources | project name, location, type| where type =~ 'Microsoft.Compute/virtualMachines' | order by name desc"

عرض أول خمسة أجهزة افتراضية حسب الاسم ونوع نظام التشغيل

يستخدم هذا الاستعلام top لاسترداد خمسة سجلات متطابقة مرتبة حسب الاسم. نوع مورد Azure هو Microsoft.Compute/virtualMachines. يخبر project Azure Resource Graph بالخصائص التي يجب تضمينها.

Resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| project name, properties.storageProfile.osDisk.osType
| top 5 by name desc
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | project name, properties.storageProfile.osDisk.osType | top 5 by name desc"

جرد الأجهزة الظاهرية حسب نوع نظام التشغيل

بناءً على الاستعلام السابق، ما زلنا نقوم بالتقييد حسب موارد Azure من النوع Microsoft.Compute/virtualMachines، لكننا لم نعد نقيد عدد السجلات التي يتم إرجاعها. بدلاً من ذلك، استخدمنا summarize وcount() لتحديد كيفية تجميع القيم وتجميعها حسب الخاصية، والتي في هذا المثال هي properties.storageProfile.osDisk.osType. للحصول على مثال لكيفية ظهور هذه السلسلة في الكائن الكامل، راجع استكشاف الموارد - اكتشاف الجهاز الظاهري.

Resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| summarize count() by tostring(properties.storageProfile.osDisk.osType)
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by tostring(properties.storageProfile.osDisk.osType)"

هناك طريقة مختلفة لكتابة نفس الاستعلام وهي عن طريق extend خاصية وتسميتها باسم مؤقت لاستخدامه داخل الاستعلام، مثل os في هذه الحالة. يُستخدم os بعد ذلك بواسطة summarize وcount() كما في المثال السابق.

Resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| extend os = properties.storageProfile.osDisk.osType
| summarize count() by tostring(os)
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | extend os = properties.storageProfile.osDisk.osType | summarize count() by tostring(os)"

إشعار

اعلم أنه بينما يسمح =~ بمطابقة غير حساسة لحالة الأحرف، فإن استخدام الخصائص (مثل properties.storageProfile.osDisk.osType) في الاستعلام يتطلب أن تكون الحالة صحيحة. إذا كانت الخاصية هي الحالة غير الصحيحة، يتم إرجاع قيمة فارغة أو غير صحيحة وسيكون التجميع أو التلخيص غير صحيح.

إظهار الموارد التي تحتوي على مساحة تخزين

بدلا من تعريف النوع المراد مطابقته بشكل صريح، يبحث هذا الاستعلام المثال عن أي مورد Azure الذي contains تخزنه الكلمة.

Resources
| where type contains 'storage' | distinct type
az graph query -q "Resources | where type contains 'storage' | distinct type"

سرد جميع الشبكات الفرعية لشبكة Azure الظاهرية

يقوم هذا الاستعلام بإرجاع قائمة بشبكات Azure الظاهرية (VNets) بما في ذلك أسماء الشبكات الفرعية وبادئات العناوين.

Resources
| where type == 'microsoft.network/virtualnetworks'
| extend subnets = properties.subnets
| mv-expand subnets
| project name, subnets.name, subnets.properties.addressPrefix, location, resourceGroup, subscriptionId
az graph query -q "Resources | where type == 'microsoft.network/virtualnetworks' | extend subnets = properties.subnets | mv-expand subnets | project name, subnets.name, subnets.properties.addressPrefix, location, resourceGroup, subscriptionId"

وضع قائمة بجميع عناوين IP العامة

على غرار الاستعلام السابق، ابحث عن كل شيء يحتوي على كلمة publicIPAddresses. يتوسع هذا الاستعلام في هذا النمط ليشمل النتائج فقط حيث properties.ipAddressisnotempty، لإرجاع properties.ipAddress فقط، وإلى limit النتائج حسب أعلى 100. قد تحتاج إلى الهروب من علامات الاقتباس اعتمادا على shell الذي اخترته.

Resources
| where type contains 'publicIPAddresses' and isnotempty(properties.ipAddress)
| project properties.ipAddress
| limit 100
az graph query -q "Resources | where type contains 'publicIPAddresses' and isnotempty(properties.ipAddress) | project properties.ipAddress | limit 100"

حساب الموارد التي تم تكوين عناوين IP لها عن طريق الاشتراك

باستخدام استعلام المثال السابق وإضافة summarize وcount()، يمكننا الحصول على قائمة باشتراك الموارد مع عناوين IP التي تم تكوينها.

Resources
| where type contains 'publicIPAddresses' and isnotempty(properties.ipAddress)
| summarize count () by subscriptionId
az graph query -q "Resources | where type contains 'publicIPAddresses' and isnotempty(properties.ipAddress) | summarize count () by subscriptionId"

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

يمكننا تقييد النتائج بخصائص أخرى غير نوع مورد Azure، مثل العلامة. في هذا المثال، نقوم بتصفية موارد Azure باستخدام اسم علامة البيئة التي لها قيمة داخلية.

Resources
| where tags.environment=~'internal'
| project name
az graph query -q "Resources | where tags.environment=~'internal' | project name"

لتقديم العلامات التي يتضمنها المورد وقيمها، أضف خاصية العلامات إلى الكلمة الرئيسية project.

Resources
| where tags.environment=~'internal'
| project name, tags
az graph query -q "Resources | where tags.environment=~'internal' | project name, tags"

سرد جميع حسابات التخزين بقيمة علامة محددة

اجمع بين وظيفة التصفية للمثال السابق وقم بتصفية نوع مورد Azure حسب خاصية النوع. يحد هذا الاستعلام أيضًا من بحثنا عن أنواع معينة من موارد Azure ذات اسم وقيمة علامة محددة.

Resources
| where type =~ 'Microsoft.Storage/storageAccounts'
| where tags['tag with a space']=='Custom value'
az graph query -q "Resources | where type =~ 'Microsoft.Storage/storageAccounts' | where tags['tag with a space']=='Custom value'"

إشعار

يستخدم هذا المثال == للمطابقة بدلاً من =~ الشرطية. == هو تطابق حساس لحالة الأحرف.

ضع قائمة بجميع العلامات وقيمها

يسرد هذا الاستعلام علامات مجموعات الإدارة والاشتراكات والموارد مع قيمها. يحد طلب البحث أولاً من الموارد حيث العلامات isnotempty()، ويحد من الحقول المضمنة فقط بتضمين العلامات في project، و mvexpand و extend للحصول على البيانات المقترنة من حقيبة الملكية. ثم يستخدم union لدمج النتائج من ResourceContainers إلى نفس النتائج من Resources، ما يوفر تغطية واسعة للعلامات التي يتم جلبها. أخيرًا، يقصر النتائج على distinct البيانات المقترنة ويستبعد العلامات المخفية للنظام.

ResourceContainers
| where isnotempty(tags)
| project tags
| mvexpand tags
| extend tagKey = tostring(bag_keys(tags)[0])
| extend tagValue = tostring(tags[tagKey])
| union (
    resources
    | where isnotempty(tags)
    | project tags
    | mvexpand tags
    | extend tagKey = tostring(bag_keys(tags)[0])
    | extend tagValue = tostring(tags[tagKey])
)
| distinct tagKey, tagValue
| where tagKey !startswith "hidden-"
az graph query -q "ResourceContainers | where isnotempty(tags) | project tags | mvexpand tags | extend tagKey = tostring(bag_keys(tags)[0]) | extend tagValue = tostring(tags[tagKey]) | union (resources | where notempty(tags) | project tags | mvexpand tags | extend tagKey = tostring(bag_keys(tags)[0]) | extend tagValue = tostring(tags[tagKey]) ) | distinct tagKey, tagValue | where tagKey !startswith "hidden-""

إظهار مجموعات أمان الشبكة غير المرتبطة

يقوم هذا الاستعلام بإرجاع مجموعات أمان الشبكة (NSGs) غير المرتبطة بواجهة شبكة أو شبكة فرعية.

Resources
| where type =~ "microsoft.network/networksecuritygroups" and isnull(properties.networkInterfaces) and isnull(properties.subnets)
| project name, resourceGroup
| sort by name asc
az graph query -q "Resources | where type =~ 'microsoft.network/networksecuritygroups' and isnull(properties.networkInterfaces) and isnull(properties.subnets) | project name, resourceGroup | sort by name asc"

سرد تنبيهات Azure Monitor مرتبة حسب الخطورة

يستخدم alertsmanagementresources هذا الاستعلام الجدول لإرجاع تنبيهات Azure Monitor مرتبة حسب الخطورة.

alertsmanagementresources
| where type =~ 'microsoft.alertsmanagement/alerts'
| where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now()
| project Severity = tostring(properties.essentials.severity)
| summarize AlertsCount = count() by Severity
az graph query -q "alertsmanagementresources | where type =~ 'microsoft.alertsmanagement/alerts' | where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now() | project Severity = tostring(properties.essentials.severity) | summarize AlertsCount = count() by Severity"

سرد تنبيهات Azure Monitor مرتبة حسب الخطورة وحالة التنبيه

يستخدم alertsmanagementresources هذا الاستعلام الجدول لإرجاع تنبيهات Azure Monitor مرتبة حسب الخطورة وحالة التنبيه.

alertsmanagementresources
| where type =~ 'microsoft.alertsmanagement/alerts'
| where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now()
| project Severity = tostring(properties.essentials.severity), AlertState = tostring(properties.essentials.alertState)
| summarize AlertsCount = count() by Severity, AlertState
az graph query -q "alertsmanagementresources | where type =~ 'microsoft.alertsmanagement/alerts' | where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now() | project Severity = tostring(properties.essentials.severity), AlertState = tostring(properties.essentials.alertState) | summarize AlertsCount = count() by Severity, AlertState"

سرد تنبيهات Azure Monitor مرتبة حسب الخطورة وخدمة المراقبة ونوع المورد المستهدف

يستخدم alertsmanagementresources هذا الاستعلام الجدول لإرجاع تنبيهات Azure Monitor مرتبة حسب الخطورة وخدمة المراقبة ونوع المورد الهدف.

alertsmanagementresources
| where type =~ 'microsoft.alertsmanagement/alerts'
| where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now()
| project Severity = tostring(properties.essentials.severity),
MonitorCondition = tostring(properties.essentials.monitorCondition),
ObjectState = tostring(properties.essentials.alertState),
MonitorService = tostring(properties.essentials.monitorService),
AlertRuleId = tostring(properties.essentials.alertRule),
SignalType = tostring(properties.essentials.signalType),
TargetResource = tostring(properties.essentials.targetResourceName),
TargetResourceType = tostring(properties.essentials.targetResourceName), id
| summarize AlertsCount = count() by Severity, MonitorService , TargetResourceType
az graph query -q "alertsmanagementresources | where type =~ 'microsoft.alertsmanagement/alerts' | where todatetime(properties.essentials.startDateTime) >= ago(2h) and todatetime(properties.essentials.startDateTime) < now() | project Severity = tostring(properties.essentials.severity), MonitorCondition = tostring(properties.essentials.monitorCondition), ObjectState = tostring(properties.essentials.alertState), MonitorService = tostring(properties.essentials.monitorService), AlertRuleId = tostring(properties.essentials.alertRule), SignalType = tostring(properties.essentials.signalType), TargetResource = tostring(properties.essentials.targetResourceName), TargetResourceType = tostring(properties.essentials.targetResourceName), id | summarize AlertsCount = count() by Severity, MonitorService , TargetResourceType"

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