如何使用 az 命令为 jmespath 查询添加 & 条件

How to add & condition for jmespath query with az command

我想过滤掉查询表达式中的两种类型

Json 文件:

[
   {
    "name": "name0",
    "tags": {
      "env": "dev"
    },
    "type": "Microsoft.OperationsManagement/solutions"
  },
  {
    "name": "name1",
    "tags": {
      "env": "dev"
    },
    "type": "Microsoft.Web/sites"
  },
  {
    "name": "name2",
    "tags": {
      "env": "dev"
    },
    "type": "Microsoft.Web/serverFarms"
  },
  {
    "name": "name4",
    "tags": null,
    "type": "Microsoft.Network/privateDnsZones/virtualNetworkLinks"
  }
]

表达式

az resource list  --resource-group MYRG --query '[? type != `"Microsoft.OperationsManagement/solutions"` && `"Microsoft.Network/privateDnsZones/virtualNetworkLinks"`]'

但结果中包含第二种类型

[
  {
    "name": "name1",
    "tags": {
      "env": "dev"
    },
    "type": "Microsoft.Web/sites"
  },
  {
    "name": "name2",
    "tags": {
      "env": "dev"
    },
    "type": "Microsoft.Web/serverFarms"
  },
  {
    "name": "name4",
    "tags": null,
    "type": "Microsoft.Network/privateDnsZones/virtualNetworkLinks"
  }
]

我试过其他表达方式,但没有成功。谁能告诉我我到底错过了什么。

您可以尝试在 &&:

之后添加另一个 type !=
az resource list  --resource-group MYRG --query '[? type != `"Microsoft.OperationsManagement/solutions"` && type != `"Microsoft.Network/privateDnsZones/virtualNetworkLinks"`]'

您还可以使用 slice 进行过滤以排除“Microsoft.OperationsManagement/solutions”和“Microsoft.Network/privateDnsZones/virtualNetworkLinks”:

az resource list --resource-group MYRG --query [1:3].type

可以参考How to query Azure resources using the Azure CLI and JMESPATH queries