1
votes

I'm trying to extract all APP service plan in a Azure subscription with az cli.

Command is az resource list, output below

  [
   {
"id": "/subscriptions/123453343434-83-342-3434-34-3/resourceGroups/KC-EMEA-RSGP-PROJECTS-PRD-01/providers/Microsoft.Web/serverFarms/EMEA-ASPLAN-PROJECTS-PRD-01",
"identity": null,
"kind": "app",
"location": "westeurope",
"managedBy": null,
"name": "EMEA-ASPLAN-PROJECTS-PRD-01",
"plan": null,
"properties": null,
"resourceGroup": "EMEA-RSGP-PROJECTS-PRD-01",
"sku": {
  "capacity": 1,
  "family": "Pv2",
  "model": null,
  "name": "P3v2",
  "size": "P3v2",
  "tier": "PremiumV2"
},
"tags": {
  "CUSTOMER": "Customer",
  "Creator": "matteo",
  "SCOPE": "PRODUCTION"
},
"type": "Microsoft.Web/serverFarms"
  },
    {
"id": "/subscriptions/123453343434-83-342-3434-34-3/resourceGroups/DefaultResourceGroup-WEU/providers/Microsoft.OperationalInsights/workspaces/DefaultWorkspace-123453343434-83-342-3434-34-3",
"identity": null,
"kind": null,
"location": "westeurope",
"managedBy": null,
"name": "DefaultWorkspace-123453343434-83-342-3434-34-3",
"plan": null,
"properties": null,
"resourceGroup": "DefaultResourceGroup-WEU",
"sku": null,
"tags": null,
"type": "Microsoft.OperationalInsights/workspaces"
   },
  {
"id": "/subscriptions/123453343434-83-342-3434-34-3/resourceGroups/defaultresourcegroup-weu/providers/Microsoft.OperationsManagement/solutions/Security(DefaultWorkspace-123453343434-83-342-3434-34-3)",
"identity": null,
"kind": null,
"location": "westeurope",
"managedBy": null,
"name": "Security(DefaultWorkspace-123453343434-83-342-3434-34-3-WEU)",
"plan": {
  "name": "Security(DefaultWorkspace-123453343434-83-342-3434-34-3-WEU)",
  "product": "OMSGallery/Security",
  "promotionCode": "",
  "publisher": "Microsoft",
  "version": null
},
"properties": null,
"resourceGroup": "defaultresourcegroup-weu",
"sku": null,
"tags": null,
"type": "Microsoft.OperationsManagement/solutions"
 }
]

Now I'd like to extract only the type contains 'Microsoft.Web/serverFarms' with JMESPATH language. I'm using Azure Command line az resource list --query [].{type:type}

But I've multiple type. How can I get only type contains 'Microsoft.Web/serverFarms' ?

output of the query is

    [
    {
  "type": "Microsoft.Web/serverFarms"
   },
  {
 "type": "Microsoft.OperationalInsights/workspaces"
  },
  {
  "type": "Microsoft.OperationsManagement/solutions"
  }
 ]
1
What are you really looking for?LMG

1 Answers

1
votes

Try this:

az resource list --query "[].{Type:type}[?Type == 'Microsoft.Web/serverFarms']"

Which Outputs:

[
  {
    "type": "Microsoft.Web/serverFarms"
  }
]

Since you use the multi-select hash {} to product a list of JSON objects, you need to then use [?Type == 'Microsoft.Web/serverFarms'] to filter the type you want. The JMESPath Tutorial page can be helpful here when learning how to write more complex JMESPath queries.

Another option is to use ConvertFrom-Json using PowerShell to deserialize the JSON array output to an array of System.Management.Automation.PSCustomObject, and use Where-Object to filter out the types that equal "Microsoft.Web/serverFarms":

$json = az resource list | ConvertFrom-Json

$result = $json | Where-Object {$_.type -eq "Microsoft.Web/serverFarms"}