共用方式為


使用 Resource Graph 探索您的 Azure 資源

Azure Resource Graph 可協助您快速且大規模地探索及尋找 Azure 資源。 專為快速回應而設計,很適合用來了解您的環境以及您 Azure 資源中的屬性。

注意

根據 Resource Graph 資料表,屬性會與 Azure 入口網站中顯示的大小寫相符,或採用小寫。 例如,查詢 resourceContainers 資料表時的資源群組名稱會與入口網站相符,但資料表 resources 中所含資源的 resourceGroup 屬性將是小寫。 這可能會導致非預期的結果,因應之道是在查詢中使用不區分大小寫的比較運算子 (例如 =~ 而非 ==),以及在與 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 所傳回的內容相同,該 ConvertTo-Json Cmdlet 會用於 屬性上 Data 。 的預設值 Depth2。 若將其設定為 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/contosovm2222",
            "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_11111111111111111111111111111111",
            "resourceGroup": "MyResourceGroup",
            "storageAccountType": "Premium_LRS"
          },
          "name": "ContosoVM1_OsDisk_1_11111111111111111111111111111111",
          "osType": "Windows"
        }
      },
      "vmId": "11111111-1111-1111-1111-111111111111"
    },
    "resourceGroup": "MyResourceGroup",
    "sku": {},
    "subscriptionId": "<subscriptionId>",
    "tags": {},
    "type": "microsoft.compute/virtualmachines"
  }
]

我們能透過這些屬性了解與虛擬機器資源本身相關的額外資訊。 這些屬性包括:作業系統、磁碟、標記,以及其所屬的資源群組和訂閱。

依位置劃分的虛擬機器

我們將瞭解虛擬機資源的相關信息,讓我們使用 location 屬性依位置計算所有虛擬機。 若要更新查詢,請移除限制並摘要說明位置值的計數。

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。 我們會更新查詢,以尋找符合此大小的所有虛擬機(VM),並只傳回 VM 和區域的名稱。

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_11111111111111111111111111111111"
  }
]
Resources
| where type =~ 'Microsoft.Compute/disks' and id == '/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/ContosoVM1_OsDisk_1_11111111111111111111111111111111'

執行查詢之前,我們該如何知道 type 現在應該是 Microsoft.Compute/disks? 如果您查看完整識別碼,您會注意到 /providers/Microsoft.Compute/disks/ 字串的一部分。 此字串片段會為您提供要搜尋之類型的提示。 替代方法是依類型移除限制,而只依 [識別碼] 欄位搜尋。 由於標識碼是唯一的,只會傳回一筆記錄,而且 type 其上的 屬性會提供該詳細數據。

注意

若要使此範例正常運作,必須使用您自己的環境中的結果取代 [識別碼] 欄位。

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

JSON 結果的結構類似於下列範例:

[
  {
    "id": "/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/ContosoVM1_OsDisk_1_11111111111111111111111111111111",
    "kind": "",
    "location": "westus2",
    "managedBy": "",
    "name": "ContosoVM1_OsDisk_1_11111111111111111111111111111111",
    "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) 資源。 然後查詢會使用 NIC 清單來尋找每個屬於公用 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),以取得連結至 NIC 之公用 IP 位址的相關網路適配器資源詳細數據。

# 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

最後,使用檔案 (Azure CLI) 或變數 (Azure PowerShell) 中儲存的公用 IP 位址資源清單,從相關的物件中取得實際的公用 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 範例的虛擬機。

下一步