Home > Enterprise >  How can i query Azure for resources using Powershell with multiple query options?
How can i query Azure for resources using Powershell with multiple query options?

Time:10-18

I am query for resources in a subscription as follows:

(az resource list --subscription <subscription-id> --query "[? 
type=='Microsoft.Web/sites'] "| ConvertFrom-Json)

But i want to put a second query parameter like the following

(az resource list --subscription <subscription-id> --query "[? 
type=='Microsoft.Web/sites'&& resourcegroup== $resourcegroup.name ]"| 
 ConvertFrom-Json)

How can i do this ?

CodePudding user response:

For querying multiple parameters you can use az graph query in azure cli as below and I followed Microsoft-Document:

Firstly, Set account subscription as below:

az account set --name "XX"    

XX- Subscription name.

Now query to get resources:

az graph query -q "Resources | where type=~ 'microsoft.web/sites' | project resourceGroup, name"| ConvertFrom-Json

enter image description here

CodePudding user response:

Please try by using the Pipe expression (|) to separate each condition. Your command would be something like:

(az resource list --subscription <subscription-id> --query "[? 
type=='Microsoft.Web/sites'] | [?resourceGroup=='$resourcegroup.name']" | 
 ConvertFrom-Json)

Please ensure that you are using proper casing for property names (e.g. resourceGroup instead of resourcegroup) and string values are inside single quotes.

Here's the command I used to get storage accounts in a resource group in my Azure Subscription:

az resource list --query "[? type=='Microsoft.Storage/storageAccounts'] | [?resourceGroup=='my-resource-group-name']"
  • Related