我正在尝试使用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"
}
]
答案 0 :(得分:1)
尝试一下:
az resource list --query "[].{Type:type}[?Type == 'Microsoft.Web/serverFarms']"
哪些输出:
[
{
"type": "Microsoft.Web/serverFarms"
}
]
由于使用多重选择哈希{}
来生成JSON对象列表,因此需要使用[?Type == 'Microsoft.Web/serverFarms']
来过滤所需的类型。在学习如何编写更复杂的JMESPath查询时,JMESPath Tutorial页在这里可能会有所帮助。
另一种选择是使用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"}