Powershell
What follows are some powershell commands that can add resource locks to all your databases and storage accounts, they took a while to build, but are very effective, enjoy. Write-Host -ForegroundColor Cyan "Adding a CanNotDelete lock to all databases"
Get-AzureRmResource `
| Where-Object {$_.ResourceGroupName -eq myresourcegroupname -and `
$_.ResourceType -eq "Microsoft.Sql/servers/databases"} `
| Select-Object `
ResourceName,ResourceType, `
@{name="name"; `
Expression={$_.name.replace("myazuresqlservername/","")}}, `
@{name="lockname"; `
Expression={"lock-databases-"+$_.name.replace("myazuresqlservername/","")}} `
| %{New-AzureRmResourceLock -ResourceGroupName myresourcegroupname`
-LockLevel CanNotDelete `
-LockNotes "Prevent accidental deletion" `
-LockName $_.lockname `
-ResourceName $_.ResourceName `
-ResourceType $_.ResourceType `
-Verbose -Force -ErrorAction Stop}
Write-Host -ForegroundColor Cyan "Adding a CanNotDelete lock to all storage accounts"
Get-AzureRmResource `
| Where-Object {$_.ResourceGroupName -eq myresourcegroupname -and `
$_.ResourceType -eq "Microsoft.Storage/storageAccounts"} `
| Select-Object ResourceName,ResourceType,Name, `
@{name="lockname"; `
Expression={"lock-storageAccounts-"+$_.name}} `
| %{New-AzureRmResourceLock -ResourceGroupName myresourcegroupname`
-LockLevel CanNotDelete `
-LockNotes "Prevent accidental deletion" `
-LockName $_.lockname `
-ResourceName $_.ResourceName `
-ResourceType $_.ResourceType `
-Verbose -Force -ErrorAction Stop}
You can customise a bit further and replace the strings "myazuresqlservername" and "myresourcegroupname" with powershell variables and stick this straight in a powershell console or in a script.
Lock removal
As an aside, if you do subsequently want to delete the DB or storage account you first need to remove the lock like this:Remove-AzureRmResourceLock -ResourceId /subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/myresourcegroupname/providers/Microsoft.Sql/servers/myazuresqlservername/databases/mydatabasename -LockName lock-databases-mydatabasename
No comments:
Post a Comment