gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:41:49 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com