Bagikan melalui


Kueri sampel Azure Resource Graph untuk Azure Virtual Machines

Halaman ini berisi kumpulan kueri sampel Azure Resource Graph untuk Azure Virtual Machines.

Contoh kueri

Jumlah penginstalan pembaruan OS yang diselesaikan

Mengembalikan daftar status eksekusi penginstalan pembaruan OS yang dilakukan untuk komputer Anda dalam tujuh hari terakhir.

PatchAssessmentResources
| where type !has 'softwarepatches'
| extend machineName = tostring(split(id, '/', 8)), resourceType = tostring(split(type, '/', 0)), tostring(rgName = split(id, '/', 4))
| extend prop = parse_json(properties)
| extend lTime = todatetime(prop.lastModifiedDateTime), OS = tostring(prop.osType), installedPatchCount = tostring(prop.installedPatchCount), failedPatchCount = tostring(prop.failedPatchCount), pendingPatchCount = tostring(prop.pendingPatchCount), excludedPatchCount = tostring(prop.excludedPatchCount), notSelectedPatchCount = tostring(prop.notSelectedPatchCount)
| where lTime > ago(7d)
| project lTime, RunID=name,machineName, rgName, resourceType, OS, installedPatchCount, failedPatchCount, pendingPatchCount, excludedPatchCount, notSelectedPatchCount
az graph query -q "PatchAssessmentResources | where type !has 'softwarepatches' | extend machineName = tostring(split(id, '/', 8)), resourceType = tostring(split(type, '/', 0)), tostring(rgName = split(id, '/', 4)) | extend prop = parse_json(properties) | extend lTime = todatetime(prop.lastModifiedDateTime), OS = tostring(prop.osType), installedPatchCount = tostring(prop.installedPatchCount), failedPatchCount = tostring(prop.failedPatchCount), pendingPatchCount = tostring(prop.pendingPatchCount), excludedPatchCount = tostring(prop.excludedPatchCount), notSelectedPatchCount = tostring(prop.notSelectedPatchCount) | where lTime > ago(7d) | project lTime, RunID=name,machineName, rgName, resourceType, OS, installedPatchCount, failedPatchCount, pendingPatchCount, excludedPatchCount, notSelectedPatchCount"

Jumlah komputer virtual berdasarkan status ketersediaan dan ID Langganan

Mengembalikan jumlah mesin virtual (jenis Microsoft.Compute/virtualMachines) yang diagregasi oleh status ketersediaan di setiap langganan Anda.

HealthResources
| where type =~ 'microsoft.resourcehealth/availabilitystatuses'
| summarize count() by subscriptionId, AvailabilityState = tostring(properties.availabilityState)
az graph query -q "HealthResources | where type =~ 'microsoft.resourcehealth/availabilitystatuses' | summarize count() by subscriptionId, AvailabilityState = tostring(properties.availabilityState)"

Jumlah mesin virtual berdasarkan status daya

Menampilkan jumlah mesin virtual (jenis Microsoft.Compute/virtualMachines) yang dikategorikan sesuai dengan status dayanya. Untuk informasi selengkapnya tentang status daya, lihat Gambaran umum status daya.

Resources
| where type == 'microsoft.compute/virtualmachines'
| summarize count() by PowerState = tostring(properties.extended.instanceView.powerState.code)
az graph query -q "Resources | where type == 'microsoft.compute/virtualmachines' | summarize count() by PowerState = tostring(properties.extended.instanceView.powerState.code)"

Menghitung komputer virtual berdasarkan jenis OS

Membangun kueri sebelumnya, kami masih membatasi sumber daya jenis Azure Microsoft.Compute/virtualMachines, tetapi tidak lagi membatasi jumlah rekaman yang dikembalikan. Sebaliknya, kami menggunakan summarize dan count() untuk menentukan cara mengelompokkan dan menggabungkan nilai menurut properti, yang dalam contoh ini adalah properties.storageProfile.osDisk.osType. Untuk contoh tampilan string ini di objek lengkap, lihat menjelajahi sumber daya - penemuan komputer virtual.

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)"

Menghitung komputer virtual berdasarkan jenis OS

Cara lain untuk menulis kueri ‘Menghitung mesin virtual berdasarkan jenis OS’ adalah ke extend properti dan memberinya nama sementara untuk digunakan dalam kueri, dalam hal inios. Laluos digunakan oleh summarize dan count() seperti dalam contoh yang dirujuk.

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)"

Dapatkan semua peringatan baru dari 30 hari terakhir

Kueri ini menyediakan daftar semua peringatan baru pengguna, dari 30 hari terakhir.

iotsecurityresources
| where type == 'microsoft.iotsecurity/locations/devicegroups/alerts'
| where todatetime(properties.startTimeUtc) > ago(30d) and properties.status == 'New'
az graph query -q "iotsecurityresources | where type == 'microsoft.iotsecurity/locations/devicegroups/alerts' | where todatetime(properties.startTimeUtc) > ago(30d) and properties.status == 'New'"

Mendapatkan kapasitas dan ukuran set skala komputer virtual

Kueri ini mencari sumber daya set skala komputer virtual dan mendapatkan berbagai detail termasuk ukuran komputer virtual dan kapasitas set skala. Kueri menggunakan fungsi toint() untuk mentransmisikan kapasitas ke angka sehingga dapat diurutkan. Akhirnya, kolom diubah namanya menjadi properti dengan nama kustom.

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"

Mencantumkan semua ekstensi yang dipasang di komputer virtual

Pertama, kueri ini menggunakan extend pada jenis sumber daya komputer virtual untuk mendapatkan ID dalam huruf besar (toupper()) ID, mendapatkan nama dan jenis sistem operasi, dan mendapatkan ukuran komputer virtual. Mengubah ID sumber daya ke huruf besar merupakan cara yang baik sebagai persiapan untuk bergabung ke properti lain. Kemudian, kueri menggunakan join untuk mendapatkan ekstensi komputer virtual dengan kind leftouter mencocokkan huruf besar substring ID ekstensi. Bagian DARI ID sebelumnya /extensions/\<ExtensionName\> adalah format yang sama dengan ID komputer virtual, jadi kami menggunakan properti ini untuk join. summarize kemudian digunakan dengan make_list pada nama ekstensi komputer virtual untuk menggabungkan nama setiap ekstensi di mana ID, OSName, OSType, dan VMSize sama ke dalam satu properti array. Terakhir, kita order by huruf kecil OSName dengan asc. Secara default, order by turun.

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"

Daftarkan pembaruan OS yang tersedia untuk semua komputer Anda yang dikelompokkan berdasarkan kategori pembaruan

Menghasilkan daftar OS tertunda untuk mesin Anda.

PatchAssessmentResources
| where type !has 'softwarepatches'
| extend prop = parse_json(properties)
| extend lastTime = properties.lastModifiedDateTime
| extend updateRollupCount = prop.availablePatchCountByClassification.updateRollup, featurePackCount = prop.availablePatchCountByClassification.featurePack, servicePackCount = prop.availablePatchCountByClassification.servicePack, definitionCount = prop.availablePatchCountByClassification.definition, securityCount = prop.availablePatchCountByClassification.security, criticalCount = prop.availablePatchCountByClassification.critical, updatesCount = prop.availablePatchCountByClassification.updates, toolsCount = prop.availablePatchCountByClassification.tools, otherCount = prop.availablePatchCountByClassification.other, OS = prop.osType
| project lastTime, id, OS, updateRollupCount, featurePackCount, servicePackCount, definitionCount, securityCount, criticalCount, updatesCount, toolsCount, otherCount
az graph query -q "PatchAssessmentResources | where type !has 'softwarepatches' | extend prop = parse_json(properties) | extend lastTime = properties.lastModifiedDateTime | extend updateRollupCount = prop.availablePatchCountByClassification.updateRollup, featurePackCount = prop.availablePatchCountByClassification.featurePack, servicePackCount = prop.availablePatchCountByClassification.servicePack, definitionCount = prop.availablePatchCountByClassification.definition, securityCount = prop.availablePatchCountByClassification.security, criticalCount = prop.availablePatchCountByClassification.critical, updatesCount = prop.availablePatchCountByClassification.updates, toolsCount = prop.availablePatchCountByClassification.tools, otherCount = prop.availablePatchCountByClassification.other, OS = prop.osType | project lastTime, id, OS, updateRollupCount, featurePackCount, servicePackCount, definitionCount, securityCount, criticalCount, updatesCount, toolsCount, otherCount"

Daftar penginstalan pembaruan OS Linux yang diselesaikan

Mengembalikan daftar status Linux Server - eksekusi penginstalan pembaruan OS yang dilakukan untuk komputer Anda dalam tujuh hari terakhir.

PatchAssessmentResources
| where type has 'softwarepatches' and properties has 'version'
| extend machineName = tostring(split(id, '/', 8)), resourceType = tostring(split(type, '/', 0)), tostring(rgName = split(id, '/', 4)), tostring(RunID = split(id, '/', 10))
| extend prop = parse_json(properties)
| extend lTime = todatetime(prop.lastModifiedDateTime), patchName = tostring(prop.patchName), version = tostring(prop.version), installationState = tostring(prop.installationState), classifications = tostring(prop.classifications)
| where lTime > ago(7d)
| project lTime, RunID, machineName, rgName, resourceType, patchName, version, classifications, installationState
| sort by RunID
az graph query -q "PatchAssessmentResources | where type has 'softwarepatches' and properties has 'version' | extend machineName = tostring(split(id, '/', 8)), resourceType = tostring(split(type, '/', 0)), tostring(rgName = split(id, '/', 4)), tostring(RunID = split(id, '/', 10)) | extend prop = parse_json(properties) | extend lTime = todatetime(prop.lastModifiedDateTime), patchName = tostring(prop.patchName), version = tostring(prop.version), installationState = tostring(prop.installationState), classifications = tostring(prop.classifications) | where lTime > ago(7d) | project lTime, RunID, machineName, rgName, resourceType, patchName, version, classifications, installationState | sort by RunID"

Daftar komputer virtual dan status ketersediaan terkait menurut ID sumber daya

Mengembalikan daftar terbaru mesin virtual (jenis Microsoft.Compute/virtualMachines) yang diagregasi oleh status ketersediaan. Kueri juga menyediakan ID sumber daya terkait berdasarkan properties.targetResourceId, untuk penelusuran kesalahan dan mitigasi yang mudah. Status ketersediaan bisa menjadi salah satu dari empat nilai: Tersedia, Tidak Tersedia, Terdegradasi, dan Tidak Diketahui. Untuk informasi selengkapnya tentang arti setiap status ketersediaan, lihat Gambaran umum Azure Resource Health.

HealthResources
| where type =~ 'microsoft.resourcehealth/availabilitystatuses'
| summarize by ResourceId = tolower(tostring(properties.targetResourceId)), AvailabilityState = tostring(properties.availabilityState)
az graph query -q "HealthResources | where type =~ 'microsoft.resourcehealth/availabilitystatuses' | summarize by ResourceId = tolower(tostring(properties.targetResourceId)), AvailabilityState = tostring(properties.availabilityState)"

Daftar komputer virtual berdasarkan status ketersediaan dan status daya dengan ID Sumber Daya dan Grup sumber daya

Mengembalikan daftar mesin virtual (jenis Microsoft.Compute/virtualMachines) yang diagregasi pada status daya dan status ketersediaan mereka untuk memberikan status kesehatan yang kohesif untuk mesin virtual Anda. Kueri juga menyediakan detail tentang grup sumber daya dan ID sumber daya yang terkait dengan setiap entri untuk visibilitas terperinci ke dalam sumber daya Anda.

Resources
| where type =~ 'microsoft.compute/virtualmachines'
| project resourceGroup, Id = tolower(id), PowerState = tostring( properties.extended.instanceView.powerState.code)
| join kind=leftouter (
  HealthResources
  | where type =~ 'microsoft.resourcehealth/availabilitystatuses'
  | where tostring(properties.targetResourceType) =~ 'microsoft.compute/virtualmachines'
  | project targetResourceId = tolower(tostring(properties.targetResourceId)), AvailabilityState = tostring(properties.availabilityState))
  on $left.Id == $right.targetResourceId
| project-away targetResourceId
| where PowerState != 'PowerState/deallocated'
az graph query -q "Resources | where type =~ 'microsoft.compute/virtualmachines' | project resourceGroup, Id = tolower(id), PowerState = tostring( properties.extended.instanceView.powerState.code) | join kind=leftouter ( HealthResources | where type =~ 'microsoft.resourcehealth/availabilitystatuses' | where tostring(properties.targetResourceType) =~ 'microsoft.compute/virtualmachines' | project targetResourceId = tolower(tostring(properties.targetResourceId)), AvailabilityState = tostring(properties.availabilityState)) on \$left.Id == \$right.targetResourceId | project-away targetResourceId | where PowerState != 'PowerState/deallocated'"

Daftar komputer virtual yang tidak Tersedia oleh Id Sumber Daya

Mengembalikan daftar terbaru mesin virtual (jenis Microsoft.Compute/virtualMachines) yang diagregasi oleh status ketersediaannya. Daftar yang diisi hanya menyoroti komputer virtual yang status ketersediaannya tidak Tersedia untuk memastikan Anda mengetahui semua status terkait tempat komputer virtual Anda berada. Ketika semua mesin virtual Anda Tersedia, Anda dapat berharap untuk tidak mendapatkan hasil.

HealthResources
| where type =~ 'microsoft.resourcehealth/availabilitystatuses'
| where tostring(properties.availabilityState) != 'Available'
| summarize by ResourceId = tolower(tostring(properties.targetResourceId)), AvailabilityState = tostring(properties.availabilityState)
az graph query -q "HealthResources | where type =~ 'microsoft.resourcehealth/availabilitystatuses' | where tostring(properties.availabilityState) != 'Available' | summarize by ResourceId = tolower(tostring(properties.targetResourceId)), AvailabilityState = tostring(properties.availabilityState)"

Daftar penginstalan pembaruan OS Windows Server yang diselesaikan

Mengembalikan daftar status Windows Server - penginstalan pembaruan OS yang dilakukan untuk komputer Anda dalam tujuh hari terakhir.

PatchAssessmentResources
| where type has 'softwarepatches' and properties !has 'version'
| extend machineName = tostring(split(id, '/', 8)), resourceType = tostring(split(type, '/', 0)), tostring(rgName = split(id, '/', 4)), tostring(RunID = split(id, '/', 10))
| extend prop = parse_json(properties)
| extend lTime = todatetime(prop.lastModifiedDateTime), patchName = tostring(prop.patchName), kbId = tostring(prop.kbId), installationState = tostring(prop.installationState), classifications = tostring(prop.classifications)
| where lTime > ago(7d)
| project lTime, RunID, machineName, rgName, resourceType, patchName, kbId, classifications, installationState
| sort by RunID
az graph query -q "PatchAssessmentResources | where type has 'softwarepatches' and properties !has 'version' | extend machineName = tostring(split(id, '/', 8)), resourceType = tostring(split(type, '/', 0)), tostring(rgName = split(id, '/', 4)), tostring(RunID = split(id, '/', 10)) | extend prop = parse_json(properties) | extend lTime = todatetime(prop.lastModifiedDateTime), patchName = tostring(prop.patchName), kbId = tostring(prop.kbId), installationState = tostring(prop.installationState), classifications = tostring(prop.classifications) | where lTime > ago(7d) | project lTime, RunID, machineName, rgName, resourceType, patchName, kbId, classifications, installationState | sort by RunID"

Mencantumkan komputer virtual dengan antarmuka jaringan dan IP publiknya

Kueri ini menggunakan dua leftouter join perintah untuk menggabungkan komputer virtual yang dibuat dengan model penyebaran Resource Manager, antarmuka jaringan terkait, dan alamat IP publik apa pun yang terkait dengan antarmuka jaringan tersebut.

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"

Menampilkan semua komputer virtual yang diurutkan berdasarkan urutan menurun

Untuk mencantumkan hanya komputer virtual (yang berjenis Microsoft.Compute/virtualMachines), kita dapat mencocokkan jenis properti dalam hasil. Mirip dengan kueri sebelumnya desc, mengubah order by menjadi menurun. =~ dalam jenis ini memberitahukan Resource Graph agar peka huruf besar/kecil.

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"

Tampilkan lima komputer virtual pertama berdasarkan nama dan jenis OS-nya

Kueri ini akan menggunakantop hanya untuk mengambil lima catatan yang cocok dan diurutkan berdasarkan nama. Jenis sumber daya Azure adalah Microsoft.Compute/virtualMachines. project memberi tahu Azure Resource Graph properti mana yang akan disertakan.

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"

Meringkas komputer virtual menurut properti diperluas status daya

Kueri ini menggunakan properti yang diperluas pada komputer virtual untuk diringkas berdasarkan status daya.

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)"

Komputer virtual dicocokkan dengan regex

Kueri ini mencari komputer virtual yang cocok dengan ekspresi reguler (dikenal sebagai regex). mencocokkan regex @memungkinkan kami menentukan regex yang akan dicocokkan, yaitu ^Contoso(.*)[0-9]+$. Definisi regex tersebut dijelaskan sebagai:

  • ^ - Pencocokan harus dimulai dari awal string.
  • Contoso - String peka huruf besar/kecil.
  • (.*) - Kecocokan subekspresi:
    • . - Mencocokkan satu karakter mana pun (kecuali baris baru).
    • * - Mencocokkan elemen sebelumnya nol kali atau lebih.
  • [0-9] - Pencocokan grup karakter untuk angka 0 hingga 9.
  • + - Mencocokkan elemen sebelumnya satu kali atau lebih.
  • $ - Kecocokan elemen sebelumnya harus terjadi di akhir string.

Setelah mencocokkan berdasarkan nama, kueri memproyeksikan nama dan pesanan berdasarkan nama naik.

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"

Langkah berikutnya