Search code examples
powershellazure-rm-templatepowershell-7.0

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": {}
}

Hi all, I am trying something like

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

But after that the brackets [] are removed ("resources": [])

I need to do this at the level of the ARM template before deploying it, it is not necessary to do with PowerShell, but I don’t know any other options

I also cannot delete them via PSobject as it is generated via [parameters ('Connection2Name')]


Solution

  • I found a way to solve this problem with the script below

    $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'
    

    This site was helpful to me in this question https://www.cloudsma.com/2019/05/building-json-payload-in-powershell/