A Starter Resource Graph lekérdezési mintáiStarter Resource Graph query samples
Az Azure Resource Graph-fal végzett lekérdezések megértéséhez először a lekérdezés nyelvét kell alapszinten megismernie.The first step to understanding queries with Azure Resource Graph is a basic understanding of the Query Language. Ha még nem ismeri a Kusto lekérdezési nyelvét (KQL), javasoljuk, hogy tekintse át a KQL vonatkozó oktatóanyagot , és ismerkedjen meg a keresett erőforrásokra vonatkozó kérelmek összeállításával.If you aren't already familiar with Kusto Query Language (KQL), it's recommended to review the tutorial for KQL to understand how to compose requests for the resources you're looking for.
A következő alapszintű lekérdezéseken vezetjük végig:We'll walk through the following starter queries:
- Az Azure-erőforrások számaCount Azure resources
- Key Vault-erőforrások számaCount key vault resources
- Erőforrások listázása név szerint rendezveList resources sorted by name
- A név szerint rendezett összes virtuális gép megjelenítése csökkenő sorrendbenShow all virtual machines ordered by name in descending order
- Első öt virtuális gép megjelenítése név és operációsrendszer-típus szerintShow first five virtual machines by name and their OS type
- A virtuális gépek száma az operációs rendszer típusa szerintCount virtual machines by OS type
- Tárolót tartalmazó erőforrások megjelenítéseShow resources that contain storage
- Az összes nyilvános IP-cím listázásaList all public IP addresses
- Az előfizetés által konfigurált IP-címmel rendelkező erőforrások számaCount resources that have IP addresses configured by subscription
- Adott címke értékkel rendelkező erőforrások listázásaList resources with a specific tag value
- Az összes olyan Storage-fiók listázása, amely adott címke értékkel rendelkezikList all storage accounts with specific tag value
- Az összes címke és azok értékeinek listázásaList all tags and their values
- Nem társított hálózati biztonsági csoportok megjelenítéseShow unassociated network security groups
- Költségmegtakarítás összegzése Azure AdvisorGet cost savings summary from Azure Advisor
- A vendég-konfigurációs házirendek hatókörében lévő gépek számaCount machines in scope of Guest Configuration policies
Ha még nincs Azure-előfizetése, kezdés előtt hozzon létre egy ingyenes fiókot.If you don't have an Azure subscription, create a free account before you begin.
Nyelvi támogatásLanguage support
Az Azure Resource Graph-ot az Azure CLI (bővítményen keresztül) és az Azure PowerShell (modulon keresztül) támogatja.Azure CLI (through an extension) and Azure PowerShell (through a module) support Azure Resource Graph. Mielőtt a következő lekérdezések bármelyikét végrehajtaná, ellenőrizze, hogy a környezet készen áll-e.Before running any of the following queries, check that your environment is ready. A kiválasztott parancshéj környezet telepítéséhez és ellenőrzéséhez lásd: Azure CLI és Azure PowerShell.See Azure CLI and Azure PowerShell for steps to install and validate your shell environment of choice.
Az Azure-erőforrások számaCount Azure resources
Ez a lekérdezés az Ön által elérhető előfizetésekben lévő Azure-erőforrások számát adja vissza.This query returns number of Azure resources that exist in the subscriptions that you have access to. Ez a lekérdezés emellett annak ellenőrzésére is jól használható, hogy a kiválasztott parancshéj rendelkezik-e a megfelelő, telepített és működőképes Azure Resource Graph-összetevőkkel.It's also a good query to validate your shell of choice has the appropriate Azure Resource Graph components installed and in working order.
Resources
| summarize count()
az graph query -q "Resources | summarize count()"
Key Vault-erőforrások számaCount key vault resources
Ez a lekérdezés a count
summarize
visszaadott rekordok számának megszámlálása helyett használja.This query uses count
instead of summarize
to count the number of records returned. Csak a Key vaultok szerepelnek a darabszámban.Only key vaults are included in the count.
Resources
| where type =~ 'microsoft.keyvault/vaults'
| count
az graph query -q "Resources | where type =~ 'microsoft.keyvault/vaults' | count"
Az erőforrások listája név szerint rendezveList resources sorted by name
Ez a lekérdezés bármilyen típusú erőforrást vissza tud adni, de csak a név, típus és hely tulajdonságokkal.This query returns any type of resource, but only the name, type, and location properties. Az order by
paranccsal rendezi a tulajdonságokat a név tulajdonság alapján növekvő (asc
) sorrendben.It uses order by
to sort the properties by the name property in ascending (asc
) order.
Resources
| project name, type, location
| order by name asc
az graph query -q "Resources | project name, type, location | order by name asc"
Az összes virtuális gép megjelenítése név szerint rendezve, csökkenő sorrendbenShow all virtual machines ordered by name in descending order
Ha csak a (Microsoft.Compute/virtualMachines
típusú) virtuális gépeket kívánja listázni, megfeleltetheti a típus tulajdonságot az eredmények között.To list only virtual machines (which are type Microsoft.Compute/virtualMachines
), we can match the property type in the results. Az előző lekérdezéshez hasonlóan a desc
paraméter az order by
paramétert csökkenő sorrendre módosítja.Similar to the previous query, desc
changes the order by
to be descending. A típusegyezésben megadott =~
esetén a Resource Graph megkülönbözteti a kis- és a nagybetűt.The =~
in the type match tells Resource Graph to be case insensitive.
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"
Az első öt virtuális gép megjelenítése a nevük és az operációs rendszerük típusa szerintShow first five virtual machines by name and their OS type
Ez a lekérdezés a top
paramétert használja ahhoz, hogy a feltételeknek megfelelőkből csak öt rekordot kérjen le, amelyek név szerint vannak rendezve.This query will use top
to only retrieve five matching records that are ordered by name. Az Azure-erőforrás típusa Microsoft.Compute/virtualMachines
.The type of the Azure resource is Microsoft.Compute/virtualMachines
. A project
adja meg az Azure Resource Graph-nak, hogy a lekérdezés mely tulajdonságokat tartalmazza.project
tells Azure Resource Graph which properties to include.
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"
A virtuális gépek száma az operációs rendszer típusa szerintCount virtual machines by OS type
Az előző lekérdezésre épülve a listánk továbbra is a Microsoft.Compute/virtualMachines
típusú Azure-erőforrásokra korlátozódik, viszont a visszaadott rekordok száma szerint már nincs szűrve.Building on the previous query, we're still limiting by Azure resources of type Microsoft.Compute/virtualMachines
, but are no longer limiting the number of records returned.
Ehelyett a summarize
és a count()
paramétert használtuk annak meghatározásához, hogyan csoportosítsa és összesítse az értékeket tulajdonság alapján, amely ebben a példában a properties.storageProfile.osDisk.osType
.Instead, we used summarize
and count()
to define how to group and aggregate the values by property, which in this example is properties.storageProfile.osDisk.osType
. Az alábbi helyen talál példát arra, hogy hogyan néz ki ez a sztring a teljes objektumban: erőforrások felfedezése – virtuális gépek felderítése.For an example of how this string looks in the full object, see explore resources - virtual machine discovery.
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)"
Egy másik módszer ugyanazon lekérdezés írásához, ha bővít (extend
) egy tulajdonságot, és ad neki egy ideiglenes nevet a lekérdezésben való használathoz, ebben az esetben ez az os.A different way to write the same query is to extend
a property and give it a temporary name for use within the query, in this case os. Ekkor a summarize
és a count()
az os-t használja az előző példában látható módon.os is then used by summarize
and count()
as in the previous example.
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)"
Megjegyzés
Vegye figyelembe, hogy míg az =~
paraméter lehetővé teszi a kis- és nagybetűk megkülönböztetése nélküli lekérdezést, ha tulajdonságokat (például properties.storageProfile.osDisk.osType) használ a lekérdezésben, a kis- és nagybetűt helyesen kell megadni.Be aware that while =~
allows case insensitive matching, use of properties (such as properties.storageProfile.osDisk.osType) in the query require the case to be correct. Ha a tulajdonság értéke helytelen, a rendszer null vagy helytelen értéket ad vissza, és a csoportosítás vagy az összefoglalás helytelen lenne.If the property is the incorrect case, a null or incorrect value is returned and the grouping or summarization would be incorrect.
A storage szót tartalmazó erőforrások megjelenítéseShow resources that contain storage
Ahelyett, hogy explicit módon meghatározná a típust az egyező találatok lekéréséhez, ez a példaként megadott lekérdezés bármely Azure-erőforrást megtalálja, amely tartalmazza (contains
) a storage szót.Instead of explicitly defining the type to match, this example query will find any Azure resource that contains
the word storage.
Resources
| where type contains 'storage' | distinct type
az graph query -q "Resources | where type contains 'storage' | distinct type"
Az összes nyilvános IP-cím listázásaList all public IP addresses
Az előző lekérdezéshez hasonlóan minden olyan elemet megtalál, amelynek a típusa tartalmazza a publicIPAddresses sztringet.Similar to the previous query, find everything that is a type with the word publicIPAddresses.
Ez a lekérdezés csak olyan eredményeket tartalmaz, amelyekben a Properties. IP-cím csak
isnotempty
a Properties. IP-cím és limit
a fent látható eredményekre tér vissza.This query expands on that pattern to only include results where properties.ipAddress
isnotempty
, to only return the properties.ipAddress, and to limit
the results by the top
100. A kiválasztott parancshéjtól függően szükség lehet az idézőjelek escape-elésére.You may need to escape the quotes depending on your chosen 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"
A konfigurált IP-címekkel rendelkező erőforrások száma előfizetés szerint csoportosítvaCount resources that have IP addresses configured by subscription
Ha maradunk az előző lekérdezéspéldánál, és hozzáadjuk a summarize
és a count()
paramétert, megkaphatjuk a konfigurált IP-címekkel rendelkező erőforrások előfizetés szerinti listáját.Using the previous example query and adding summarize
and count()
, we can get a list by subscription of resources with configured IP addresses.
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"
Az adott címkeértékkel rendelkező erőforrások listázásaList resources with a specific tag value
Az eredményeket az Azure-erőforrás típusán kívül más tulajdonságok, pl. a címke alapján is korlátozhatjuk.We can limit the results by properties other than the Azure resource type, such as a tag. Ebben a példában azokra az Environment címkenévvel rendelkező Azure-erőforrásokra szűrünk, amelyek értéke Internal.In this example, we're filtering for Azure resources with a tag name of Environment that have a value of Internal.
Resources
| where tags.environment=~'internal'
| project name
az graph query -q "Resources | where tags.environment=~'internal' | project name"
Az erőforrás címkéinek és a hozzájuk tartozó értékek megadásához adja hozzá a tags tulajdonságot a project
kulcsszóhoz.To also provide what tags the resource has and their values, add the property tags to the project
keyword.
Resources
| where tags.environment=~'internal'
| project name, tags
az graph query -q "Resources | where tags.environment=~'internal' | project name, tags"
Az adott címkeértékkel rendelkező összes tárfiók listázásaList all storage accounts with specific tag value
Kombinálhatja az előző példa szűrőfunkcióját, és type tulajdonság alapján szűrheti az Azure-erőforrásokat.Combine the filter functionality of the previous example and filter Azure resource type by type property. Ez a lekérdezés az Azure-erőforrások adott típusainak keresését is adott címkenévre és -értékre korlátozza.This query also limits our search for specific types of Azure resources with a specific tag name and value.
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'"
Megjegyzés
Ez példa az egyező találatok kereséséhez az ==
paramétert használja az =~
feltételes helyett.This example uses ==
for matching instead of the =~
conditional. Az ==
kis- és nagybetűket megkülönböztető találatot ad.==
is a case sensitive match.
Az összes címke és azok értékeinek listázásaList all tags and their values
Ez a lekérdezés felsorolja a felügyeleti csoportokra, előfizetésekre és erőforrásokra vonatkozó címkéket, valamint azok értékeit.This query lists tags on management groups, subscriptions, and resources along with their values.
A lekérdezés először korlátozza azokat az erőforrásokat, amelyekben a címkék szerepelnek isnotempty()
, a belefoglalt mezőket csak a címkéi project
, a és a, mvexpand
valamint a extend
párosított adatoknak a tulajdonság zsákból való lekérésére korlátozza.The query first limits to resources where tags isnotempty()
, limits the included fields by only including tags in the project
, and mvexpand
and extend
to get the paired data from the property bag. Ezután a használatával union
összekapcsolja az eredményeket a ResourceContainers és az erőforrások azonos eredményei között, így széleskörű lefedettséget biztosít a címkék lekéréséhez.It then uses union
to combine the results from ResourceContainers to the same results from Resources, giving broad coverage to which tags are fetched. Végül korlátozza az eredményeket a distinct
párosított értékekre, és kizárja a rendszerrejtett címkéket.Last, it limits the results to distinct
paired data and excludes system-hidden tags.
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-""
Nem társított hálózati biztonsági csoportok megjelenítéseShow unassociated network security groups
Ez a lekérdezés olyan hálózati biztonsági csoportokat (NSG) ad vissza, amelyek nincsenek hálózati adapterhez vagy alhálózathoz társítva.This query returns Network Security Groups (NSGs) that aren't associated to a network interface or subnet.
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"
Költségmegtakarítás összegzése Azure AdvisorGet cost savings summary from Azure Advisor
Ez a lekérdezés összegzi az egyes Azure Advisor javaslatok költségmegtakarítását.This query summarizes the cost savings of each Azure Advisor recommendation.
advisorresources
| where type == 'microsoft.advisor/recommendations'
| where properties.category == 'Cost'
| extend
resources = tostring(properties.resourceMetadata.resourceId),
savings = todouble(properties.extendedProperties.savingsAmount),
solution = tostring(properties.shortDescription.solution),
currency = tostring(properties.extendedProperties.savingsCurrency)
| summarize
dcount(resources),
bin(sum(savings), 0.01)
by solution, currency
| project solution, dcount_resources, sum_savings, currency
| order by sum_savings desc
az graph query -q "advisorresources | where type == 'microsoft.advisor/recommendations' | where properties.category == 'Cost' | extend resources = tostring(properties.resourceMetadata.resourceId), savings = todouble(properties.extendedProperties.savingsAmount), solution = tostring(properties.shortDescription.solution), currency = tostring(properties.extendedProperties.savingsCurrency) | summarize dcount(resources), bin(sum(savings), 0.01) by solution, currency | project solution, dcount_resources, sum_savings, currency | order by sum_savings desc"
A vendég-konfigurációs házirendek hatókörében lévő gépek számaCount machines in scope of Guest Configuration policies
Megjeleníti az Azure-beli virtuális gépek és az ív által csatlakoztatott kiszolgálók számát a hatókörben Azure Policy vendég konfigurációs hozzárendeléseihez.Displays the count of Azure virtual machines and Arc connected servers in scope for Azure Policy Guest Configuration assignments.
GuestConfigurationResources
| extend vmid = split(properties.targetResourceId,'/')
| mvexpand properties.latestAssignmentReport.resources
| where properties_latestAssignmentReport_resources.resourceId != 'Invalid assignment package.'
| project machine = tostring(vmid[(-1)]),
type = tostring(vmid[(-3)])
| distinct machine, type
| summarize count() by type
az graph query -q "GuestConfigurationResources | extend vmid = split(properties.targetResourceId,'/') | mvexpand properties.latestAssignmentReport.resources | where properties_latestAssignmentReport_resources.resourceId != 'Invalid assignment package.' | project machine = tostring(vmid[(-1)]), type = tostring(vmid[(-3)]) | distinct machine, type | summarize count() by type"
Következő lépésekNext steps
- További információ a lekérdezési nyelvről.Learn more about the query language.
- További információ az erőforrások feltárásáról.Learn more about how to explore resources.
- Lásd a speciális lekérdezésekmintáit.See samples of Advanced queries.