Skip to content

Latest commit

 

History

History
253 lines (161 loc) · 7.26 KB

File metadata and controls

253 lines (161 loc) · 7.26 KB
ms.topic include
ms.custom devx-track-azurecli

This section contains examples of JMESPath queries for different Azure resources.

Query examples for Azure accounts

This section shows example queries for storage accounts.

  • This example returns the tenant ID and subscription ID of the Azure account and subscription you're using.
az account show --query "{tenantId:tenantId,subscriptionid:id}"
az account show --query "{tenantId:tenantId,subscriptionid:id}"
az account show --query "{tenantId:tenantId,subscriptionid:id}"

Query examples for Microsoft Entra service principals

This section shows example queries for Microsoft Entra service principals.

  • The following query returns the first Microsoft Graph application service principal who has read permissions.
az ad sp list --display-name "Microsoft Graph" --query "[0].appRoles[?value=='User.Read.All' && contains(allowedMemberTypes, 'Application')].id" --output tsv
az ad sp list --display-name "Microsoft Graph" --query "[0].appRoles[?value=='User.Read.All' && contains(allowedMemberTypes, 'Application')].id" --output tsv
az ad sp list --display-name "Microsoft Graph" --query "[0].appRoles[?value=='User.Read.All' && contains(allowedMemberTypes, 'Application')].id" --output tsv

Query examples for storage accounts

This section shows example queries for storage accounts.

  • This example returns the primary endpoints for all tables a storage account.
az storage account show --resource-group QueryDemo --name mystorageaccount --query "primaryEndpoints.table"
az storage account show --resource-group QueryDemo --name mystorageaccount --query "primaryEndpoints.table"
az storage account show --resource-group QueryDemo --name mystorageaccount --query "primaryEndpoints.table"

Query examples for Virtual Machines

This section shows example queries for Virtual Machines (VMs).

  • This example returns the names of VMs whose disk size is larger than 50 GB.
az vm list --resource-group QueryDemo --query "[?storageProfile.osDisk.diskSizeGb >=\`50\`].{Name:name,  admin:osProfile.adminUsername, DiskSize:storageProfile.osDisk.diskSizeGb }" --output table
az vm list --resource-group QueryDemo --query "[?storageProfile.osDisk.diskSizeGb >=``50``].{Name:name,  admin:osProfile.adminUsername, DiskSize:storageProfile.osDisk.diskSizeGb }" --output table

Notice the extra escape characters (`) surrounding the 50 in the command previous. The extra escape characters are present because Azure CLI commands are considered Command Prompt scripts. Take into consideration the built-in parsing of both PowerShell and of a Command Prompt. Azure CLI will only receive a symbol if it still exists after 2 rounds of parsing. For more information about other possible quoting issues in PowerShell, see Considerations for running the Azure CLI in a PowerShell scripting language.

az vm list --resource-group QueryDemo --query "[?storageProfile.osDisk.diskSizeGb >=`50`].{Name:name, admin:osProfile.adminUsername, DiskSize:storageProfile.osDisk.diskSizeGb }" --output table

  • The following query demonstrates how to list the names and storage account types of VMs who use SSD storage.
az vm list --resource-group QueryDemo --query "[].{Name:name, Storage:storageProfile.osDisk.managedDisk.storageAccountType} | [? contains(Storage,'SSD')]"
az vm list --resource-group QueryDemo --query "[].{Name:name, Storage:storageProfile.osDisk.managedDisk.storageAccountType} | [? contains(Storage,'SSD')]"
az vm list --resource-group QueryDemo --query "[].{Name:name, Storage:storageProfile.osDisk.managedDisk.storageAccountType} | [? contains(Storage,'SSD')]"

Query examples for cognitive services

This section shows example queries for cognitive services.

  • The following query demonstrates how to list endpoints of a cognitive service.
az cognitiveservices account show --resource-group QueryDemo --name DemoAccount --query "properties.endpoint"

az cognitiveservices account show --resource-group QueryDemo --name DemoAccount --query "properties.endpoint"
az cognitiveservices account show --resource-group QueryDemo --name DemoAccount --query "properties.endpoint"


Query examples for virtual networks

This section shows example queries for virtual networks (VNet).

  • The following query lists the IDs of IP addresses that contain the substring in the variable IP.
IP="20.127"
az network public-ip list --query "[?ipAddress!=null]|[?contains(ipAddress, '$IP')].[id]" --output tsv
$IP="20.127"
az network public-ip list --query "[?ipAddress!=null]|[?contains(ipAddress, '$IP')].[id]" --output tsv
Set IP="20.127"
az network public-ip list --query "[?ipAddress!=null]|[?contains(ipAddress, '%IP%')].[id]" --output tsv

Query examples for web apps

This section shows example queries for web apps.

  • The following query lists all web apps that are currently running.
az webapp list --resource-group DemoGroup --query "[?state=='Running']"
az webapp list --resource-group DemoGroup --query "[?state=='Running']"
az webapp list --resource-group DemoGroup --query "[?state=='Running']"

  • The following query returns the profile name and url of all web apps whose profile name ends with FTP.
az webapp deployment list-publishing-profiles --resource-group DemoGroup --name DemoApp --query "[?ends_with(profileName, 'FTP')].{profileName: profileName, publishUrl: publishUrl}"
az webapp deployment list-publishing-profiles --resource-group DemoGroup --name DemoApp --query "[?ends_with(profileName, 'FTP')].{profileName: profileName, publishUrl: publishUrl}"
az webapp deployment list-publishing-profiles --resource-group DemoGroup --name DemoApp --query "[?ends_with(profileName, 'FTP')].{profileName: profileName, publishUrl: publishUrl}"