如何使用 PowerShell 从 ARM 模板中删除资源?

How to remove recourses from ARM template using PowerShell?

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "Connection1Name": {
            "type": "string",
            "defaultValue": "ApiConnection1"
        },
        "Connection2Name": {
            "type": "string",
            "defaultValue": "ApiConnection2"
        }
    },
    "functions": [],
    "variables": {},
    "resources": [


        {
            "name": "logicApp1",
            "type": "Microsoft.Logic/workflows",
            "apiVersion": "2017-07-01",
            "location": "[resourceGroup().location]",
            "properties": {
                "definition": {
                    "$schema": "https://schema.management.azure.com/schemas/2016-06-01/Microsoft.Logic.json",
                    "contentVersion": "1.0.0.0",
                    "parameters": {
                    },
                    "triggers": {
                    },
                    "actions": {
                    },
                    "outputs": {
                    }
                },
                "parameters": {
                }
            }
        },


        {
            "name": "[parameters('Connection1Name')]",
            "type": "Microsoft.Web/connections",
            "apiVersion": "2016-06-01",
            "location": "[resourceGroup().location]",
            "tags": {
            },
            "properties": {
                "displayName": "[parameters('Connection1Name')]",
                "parameterValues": {
                },
                "customParameterValues": {
                },
                "nonSecretParameterValues": {
                },
                "api": {
                    "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', resourceGroup().location, '/managedApis/logicAppConnectorApi1')]"
                }
            }
        },


        {
            "name": "[parameters('Connection2Name')]",
            "type": "Microsoft.Web/connections",
            "apiVersion": "2016-06-01",
            "location": "[resourceGroup().location]",
            "tags": {
            },
            "properties": {
                "displayName": "[parameters('Connection2Name')]",
                "parameterValues": {
                },
                "customParameterValues": {
                },
                "nonSecretParameterValues": {
                },
                "api": {
                    "id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', resourceGroup().location, '/managedApis/logicAppConnectorApi1')]"
                }
            }
        }

    ],
    "outputs": {}
}

大家好,我正在尝试

$jsonContentObject = Get-Content 'logicApp.json' -Raw | ConvertFrom-Json
$jsonContentObject.resources = $jsonContentObject.resources | Where-Object {$_.type -notmatch 'Microsoft.Web/connections' }

但之后括号 [] 被删除 ("resources": [])

我需要在部署之前在 ARM 模板级别执行此操作,不需要使用 PowerShell,但我不知道任何其他选项

我也无法通过 PSobject 删除它们,因为它是通过 [parameters ('Connection2Name')]

生成的

我找到了使用以下脚本解决此问题的方法

$jsonContentObject = Get-Content 'logicApp.json' -Raw | ConvertFrom-Json
$resourceSquare = @(
    $jsonContentObject.resources | Where-Object { $_.type -notmatch 'Microsoft.Web/connections' }
)

$jsonContentObject.resources = $resourceSquare
$jsonContentObject | ConvertTo-Json -Depth 100 | Set-Content 'logicApp.json'

这个网站对我这个问题很有帮助https://www.cloudsma.com/2019/05/building-json-payload-in-powershell/