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

Extract App Service Plan Azure Resource list with AZ CLI

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

命令为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 resource 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"
  }
]

由于您使用 multi-select 散列 {} 来生成 JSON 对象的列表,因此您需要使用 [?Type == 'Microsoft.Web/serverFarms'] 来过滤您想要的类型.在学习如何编写更复杂的 JMESPath 查询时,JMESPath Tutorial 页面在这里很有帮助。

另一种选择是使用 ConvertFrom-Json using PowerShell to deserialize the JSON array output to an array of System.Management.Automation.PSCustomObject, and use Where-Object 过滤掉等于 "Microsoft.Web/serverFarms":

的类型
$json = az resource list | ConvertFrom-Json

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