温馨提示:本文翻译自stackoverflow.com,查看原文请点击:json - Extract App Service Plan Azure Resource list with AZ CLI
azure command-line-interface jmespath json

json - 使用AZ CLI提取App Service Plan Azure资源列表

发布于 2020-05-31 11:53:55

我正在尝试使用az cli提取Azure订阅中的所有APP服务计划。

命令是z资源列表,输出如下

  [
   {
"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"
 }
]

现在,我只想提取包含JMESPATH语言的“ Microsoft.Web / serverFarms”类型。我正在使用Azure命令行az资源列表--query []。{type:type}

但是我有多种类型。我如何只获取包含“ Microsoft.Web / serverFarms”的类型?

查询的输出是

    [
    {
  "type": "Microsoft.Web/serverFarms"
   },
  {
 "type": "Microsoft.OperationalInsights/workspaces"
  },
  {
  "type": "Microsoft.OperationsManagement/solutions"
  }
 ]

查看更多

提问者
Cyber.Drunk
被浏览
7
RoadRunner - MSFT 2020-03-17 11:41

尝试这个:

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

哪些输出:

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

由于使用多选哈希{}表生成JSON对象列表,因此需要使用[?Type == 'Microsoft.Web/serverFarms']来过滤所需的类型。在学习如何编写更复杂的JMESPath查询时,JMESPath教程”页面在这里可能会有所帮助。

另一个选择是ConvertFrom-Json使用PowerShell 使用反序列化将JSON数组输出反序列化为System.Management.Automation.PSCustomObject的数组,并用于Where-Object过滤出相等的类型"Microsoft.Web/serverFarms"

$json = az resource list | ConvertFrom-Json

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