استكشف موارد Azure خاصتك باستخدام Resource Graph

يساعدك Azure Resource Graph على استكشاف موارد Azure واكتشافها بسرعة وعلى نطاق واسع. تم تصميمه للاستجابات السريعة، وهي طريقة رائعة للتعرف على البيئة لديك وكذلك حول الخصائص الموجودة على موارد Azure.

إشعار

اعتمادا على جدول Resource Graph، ستتطابق الخصائص إما مع الغلاف كما هو موضح في مدخل Microsoft Azure أو تكون صغيرة. على سبيل المثال، سيتطابق اسم مجموعة الموارد عند الاستعلام عن resourceContainers الجدول مع المدخل، ولكن resourceGroup ستكون خاصية الموارد من resources الجدول بأحرف صغيرة. قد يؤدي هذا إلى نتائج غير متوقعة ويمكن حسابها في استعلاماتك باستخدام عوامل المقارنة غير الحساسة لحالة الأحرف مثل =~ بدلا من == وتحويل الخصائص إلى أحرف صغيرة في الصلات مع الدالة tolower() .

استكشاف الأجهزة الظاهرية

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

اكتشاف الجهاز الظاهري

لنبدأ باستعلام بسيط للحصول على جهاز ظاهري واحد من البيئة لدينا ونطلع على الخصائص التي تم إرجاعها.

Resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| limit 1
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | limit 1"
(Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | limit 1").Data | ConvertTo-Json -Depth 100

إشعار

يباشر Azure PowerShell Search-AzGraph cmdlet إرجاع PSResourceGraphResponse بشكل افتراضي. لجعل الإخراج يبدو مماثلا لما يتم إرجاعه بواسطة Azure CLI، يتم استخدام cmdlet ConvertTo-Json على خاصية «البيانات». القيمة الافتراضية للعمق هي 2. يجب أن يتسبب تعيينه إلى 100 في تحويل جميع المستويات التي تم إرجاعها.

يتم تنظيم نتائج JSON بشكل مشابه للمثال التالي:

[
  {
    "id": "/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/ContosoVM1",
    "kind": "",
    "location": "westus2",
    "managedBy": "",
    "name": "ContosoVM1",
    "plan": {},
    "properties": {
      "hardwareProfile": {
        "vmSize": "Standard_B2s"
      },
      "networkProfile": {
        "networkInterfaces": [
          {
            "id": "/subscriptions/<subscriptionId>/MyResourceGroup/providers/Microsoft.Network/networkInterfaces/contosovm1535",
            "resourceGroup": "MyResourceGroup"
          }
        ]
      },
      "osProfile": {
        "adminUsername": "localAdmin",
        "computerName": "ContosoVM1",
        "secrets": [],
        "windowsConfiguration": {
          "enableAutomaticUpdates": true,
          "provisionVMAgent": true
        }
      },
      "provisioningState": "Succeeded",
      "storageProfile": {
        "dataDisks": [],
        "imageReference": {
          "offer": "WindowsServer",
          "publisher": "MicrosoftWindowsServer",
          "sku": "2016-Datacenter",
          "version": "latest"
        },
        "osDisk": {
          "caching": "ReadWrite",
          "createOption": "FromImage",
          "diskSizeGB": 127,
          "managedDisk": {
            "id": "/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/ContosoVM1_OsDisk_1_9676b7e1b3c44e2cb672338ebe6f5166",
            "resourceGroup": "MyResourceGroup",
            "storageAccountType": "Premium_LRS"
          },
          "name": "ContosoVM1_OsDisk_1_9676b7e1b3c44e2cb672338ebe6f5166",
          "osType": "Windows"
        }
      },
      "vmId": "bbb9b451-6dc7-4117-bec5-c971eb1118c6"
    },
    "resourceGroup": "MyResourceGroup",
    "sku": {},
    "subscriptionId": "<subscriptionId>",
    "tags": {},
    "type": "microsoft.compute/virtualmachines"
  }
]

تقدم الخصائص معلومات إضافية حول مورد الجهاز الظاهري نفسه. تتضمن هذه الخصائص: نظام التشغيل والأقراص والعلامات ومجموعة الموارد والاشتراك الذي هو عضو فيه.

الأجهزة الظاهرية حسب الموقع

باستيعاب ما تعلمناه حول مورد الأجهزة الظاهرية، دعنا نستخدم خاصية الموقع لحساب جميع الأجهزة الظاهرية حسب الموقع. لتحديث الاستعلام، نقوم بإزالة الحد وتلخيص عدد قيم الموقع.

Resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| summarize count() by location
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by location"
(Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by location").Data | ConvertTo-Json

يتم تنظيم نتائج JSON بشكل مشابه للمثال التالي:

[
  {
    "count_": 386,
    "location": "eastus"
  },
  {
    "count_": 215,
    "location": "southcentralus"
  },
  {
    "count_": 59,
    "location": "westus"
  }
]

يمكننا الآن معرفة عدد الأجهزة الظاهرية لدينا في كل منطقة من مناطق Azure.

الأجهزة الظاهرية استنادا إلى SKU

بالعودة إلى خصائص الجهاز الظاهري الأصلي، دعنا نحاول العثور على جميع الأجهزة الظاهرية التي لديها حجم SKU من Standard_B2s. يظهر JSON الذي تم إرجاعه أنه مخزن في properties.hardwareprofile.vmsize. نبدأ بتحديث الاستعلام للعثور على جميع الأجهزة الظاهرية التي تتطابق مع هذا الحجم وإرجاع اسم الجهاز الظاهري والمنطقة فقط.

Resources
| where type =~ 'Microsoft.Compute/virtualMachines' and properties.hardwareProfile.vmSize == 'Standard_B2s'
| project name, resourceGroup
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' and properties.hardwareProfile.vmSize == 'Standard_B2s' | project name, resourceGroup"
(Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Compute/virtualMachines' and properties.hardwareProfile.vmSize == 'Standard_B2s' | project name, resourceGroup").Data | ConvertTo-Json

الأجهزة الظاهرية المتصلة بأقراص مدارة متميزة

للحصول على تفاصيل الأقراص المدارة المتميزة المرفقة بهذه الأجهزة الظاهرية Standard_B2s، نعمل على توسيع الاستعلام لإرجاع معرّف المورد لهذه الأقراص المدارة.

Resources
| where type =~ 'Microsoft.Compute/virtualmachines' and properties.hardwareProfile.vmSize == 'Standard_B2s'
| extend disk = properties.storageProfile.osDisk.managedDisk
| where disk.storageAccountType == 'Premium_LRS'
| project disk.id
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualmachines' and properties.hardwareProfile.vmSize == 'Standard_B2s' | extend disk = properties.storageProfile.osDisk.managedDisk | where disk.storageAccountType == 'Premium_LRS' | project disk.id"
(Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Compute/virtualmachines' and properties.hardwareProfile.vmSize == 'Standard_B2s' | extend disk = properties.storageProfile.osDisk.managedDisk | where disk.storageAccountType == 'Premium_LRS' | project disk.id").Data | ConvertTo-Json

والنتيجة هي قائمة بمعرّفات القرص.

اكتشاف القرص المدار

سنكتشف، باستخدام السجل الأول من الاستعلام السابق، الخصائص الموجودة في القرص المدار الذي تم إرفاقه بالجهاز الظاهري الأول. يستخدم الاستعلام المحدث معرّف القرص ويغير النوع.

مثال على الإخراج من الاستعلام السابق على سبيل المثال:

[
  {
    "disk_id": "/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/ContosoVM1_OsDisk_1_9676b7e1b3c44e2cb672338ebe6f5166"
  }
]
Resources
| where type =~ 'Microsoft.Compute/disks' and id == '/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/ContosoVM1_OsDisk_1_9676b7e1b3c44e2cb672338ebe6f5166'

قبل تشغيل الاستعلام، كيف عرفنا أن النوع يجب أن يكون الآن Microsoft.Compute/disks؟ إذا نظرت إلى المعرّف الكامل، فسترى /providers/Microsoft.Compute/disks/ كجزء من السلسلة. يمنحك ملحق السلسلة هذا تلميحا حول نوع البحث. يتمثل الأسلوب البديل في إزالة الحد استنادا إلى النوع والبحث فقط بدلا من ذلك بواسطة حقل المعرّف. سيتم، نظرا لأن المعرف فريد، إرجاع سجل واحد فقط وتقدم خاصية النوع عليه هذه التفاصيل.

إشعار

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

az graph query -q "Resources | where type =~ 'Microsoft.Compute/disks' and id == '/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/ContosoVM1_OsDisk_1_9676b7e1b3c44e2cb672338ebe6f5166'"
(Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Compute/disks' and id == '/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/ContosoVM1_OsDisk_1_9676b7e1b3c44e2cb672338ebe6f5166'").Data | ConvertTo-Json

يتم تنظيم نتائج JSON بشكل مشابه للمثال التالي:

[
  {
    "id": "/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/ContosoVM1_OsDisk_1_9676b7e1b3c44e2cb672338ebe6f5166",
    "kind": "",
    "location": "westus2",
    "managedBy": "",
    "name": "ContosoVM1_OsDisk_1_9676b7e1b3c44e2cb672338ebe6f5166",
    "plan": {},
    "properties": {
      "creationData": {
        "createOption": "Empty"
      },
      "diskSizeGB": 127,
      "diskState": "ActiveSAS",
      "provisioningState": "Succeeded",
      "timeCreated": "2018-09-14T12:17:32.2570000Z"
    },
    "resourceGroup": "MyResourceGroup",
    "sku": {
      "name": "Premium_LRS",
      "tier": "Premium"
    },
    "subscriptionId": "<subscriptionId>",
    "tags": {
      "environment": "prod"
    },
    "type": "microsoft.compute/disks"
  }
]

استكشاف الأجهزة الظاهرية للحصول على عناوين IP العامة

تعمل مجموعة الاستعلامات هذه على الحصول أولا على جميع موارد واجهات الشبكة (NIC) المتصلة بالأجهزة الظاهرية وتخزينها. وتستخدم الاستعلامات قائمة واجهات الشبكة NICs للعثور على كل مورد عنوان IP يمثل عنوان IP عام وتخزين هذه القيم. تقدم الاستعلامات أخيرا قائمة بعناوين IP العامة.

# Use Resource Graph to get all NICs and store in the 'nics.txt' file
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | project nic = tostring(properties['networkProfile']['networkInterfaces'][0]['id']) | where isnotempty(nic) | distinct nic | limit 20" --output table | tail -n +3 > nics.txt

# Review the output of the query stored in 'nics.txt'
cat nics.txt
# Use Resource Graph to get all NICs and store in the $nics variable
$nics = (Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | project nic = tostring(properties['networkProfile']['networkInterfaces'][0]['id']) | where isnotempty(nic) | distinct nic | limit 20").Data

# Review the output of the query stored in the variable
$nics.nic

استخدم الملف (Azure CLI) أو المتغير (Azure PowerShell) في الاستعلام التالي للحصول على تفاصيل موارد واجهة الشبكة المرتبطة حيث يوجد عنوان IP عام مرفق ب NIC.

# Use Resource Graph with the 'nics.txt' file to get all related public IP addresses and store in 'publicIp.txt' file
az graph query -q="Resources | where type =~ 'Microsoft.Network/networkInterfaces' | where id in ('$(awk -vORS="','" '{print $0}' nics.txt | sed 's/,$//')') | project publicIp = tostring(properties['ipConfigurations'][0]['properties']['publicIPAddress']['id']) | where isnotempty(publicIp) | distinct publicIp" --output table | tail -n +3 > ips.txt

# Review the output of the query stored in 'ips.txt'
cat ips.txt
# Use Resource Graph  with the $nics variable to get all related public IP addresses and store in $ips variable
$ips = (Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Network/networkInterfaces' | where id in ('$($nics.nic -join "','")') | project publicIp = tostring(properties['ipConfigurations'][0]['properties']['publicIPAddress']['id']) | where isnotempty(publicIp) | distinct publicIp").Data

# Review the output of the query stored in the variable
$ips.publicIp

أخيرا، استخدم قائمة بموارد عناوين IP العامة المخزنة في الملف (Azure CLI) أو المتغير (Azure PowerShell) للحصول على عنوان IP العام الفعلي من الكائن والعرض ذي الصلة.

# Use Resource Graph with the 'ips.txt' file to get the IP address of the public IP address resources
az graph query -q="Resources | where type =~ 'Microsoft.Network/publicIPAddresses' | where id in ('$(awk -vORS="','" '{print $0}' ips.txt | sed 's/,$//')') | project ip = tostring(properties['ipAddress']) | where isnotempty(ip) | distinct ip" --output table
# Use Resource Graph with the $ips variable to get the IP address of the public IP address resources
(Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Network/publicIPAddresses' | where id in ('$($ips.publicIp -join "','")') | project ip = tostring(properties['ipAddress']) | where isnotempty(ip) | distinct ip").Data | ConvertTo-Json

لمعرفة كيفية إنجاز هذه الخطوات في استعلام واحد باستخدام عامل التشغيل join، راجع قائمة الأجهزة الظاهرية مع واجهة الشبكة ونموذج IP العام.

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