json - 使用 AZ CLI 提取应用服务计划 Azure 资源列表

标签 json azure command-line-interface jmespath

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

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

  [
   {
"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 resources list --query [].{type:type}

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

查询的输出是

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

最佳答案

试试这个:

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

哪些输出:

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

由于您使用多选哈希 {} 来生成 JSON 对象列表,因此您需要使用 [?Type == 'Microsoft.Web/serverFarms'] 过滤你想要的类型。 JMESPath Tutorial当学习如何编写更复杂的 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"}

关于json - 使用 AZ CLI 提取应用服务计划 Azure 资源列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60713619/

相关文章:

php - AJAX 请求返回 unicode 字符作为问号

json - 如何根据 Mongoose 中的子文档值检索父文档?

azure - 如何允许应用程序注册仅由特定用户或组访问?

azure - 在 Azure 应用程序网关上部署 ASP.NET 代码时出现 502 错误

bash - envsubst : command not found on Mac OS X 10. 8

windows - 如何使用命令行选项在 7zip 文件管理器中打开存档?

javascript - 如何用javascript迭代json?

json - 如何在Windows中导入mongodb转储文件(bson+json)?

azure - Web API 身份验证最佳实践

angular - 在远程服务器中 Dockerize Angular cli 应用程序