More Woes with ADF Deployments
Hello!
In a previous post, I wrote about some of the issues with the PowerShell script used to delete deployments. This is packaged as part of the npm package provided by Microsoft.
We recently hit an issue with the number of resources in the ARM template generated by this npm package. Helpfully the package also creates the ARM templates as linkedtemplates.
This was all fine, however the delete deployments section of this ill-fated PowerSHell script does not account for multiple deployments, which is exactly what happens when you use linked templates.
At this point I’m done with the PowerSHell script provided, and decided to write my own. I did liberally copy from the other script as fundamentally it is sound, it’s just not fit for my purposes.
[CmdletBinding()]
param (
[Parameter()]
[String]
$ResourceGroupName,
[Parameter()]
[String]
$DeploymentName
)
Write-Host "Deleting ARM deployment ... under resource group: $ResourceGroupName"
$deployments = Get-AzResourceGroupDeployment -ResourceGroupName $ResourceGroupName
$deploymentsToDelete = $deployments | Where-Object { $_.DeploymentName -like "$DeploymentName*" -or $_.DeploymentName -like "ArmTemplate_master*" -or $_.DeploymentName -like "ArmTemplateForFactory*" } | Sort-Object -Property Timestamp -Descending
foreach ($deploymentToDelete in $deploymentsToDelete){
$deploymentName = $deploymentToDelete.DeploymentName
Write-Host "Deployment to be deleted: $deploymentName"
$deploymentOperations = Get-AzResourceGroupDeploymentOperation -DeploymentName $deploymentName -ResourceGroupName $ResourceGroupName
$deploymentsToDelete = $deploymentOperations | Where-Object { $_.properties.targetResource.id -like "*Microsoft.Resources/deployments*" }
$deploymentsToDelete | ForEach-Object {
Write-Host "Deleting inner deployment: $($_.properties.targetResource.id)"
Remove-AzResourceGroupDeployment -Id $_.properties.targetResource.id
}
Write-Host "Deleting deployment: $deploymentName"
Remove-AzResourceGroupDeployment -ResourceGroupName $ResourceGroupName -Name $deploymentName
}