Script PowerShell : Delete VM Item backup and the Recovery Service Vault

%3CLINGO-SUB%20id%3D%22lingo-sub-2936260%22%20slang%3D%22en-US%22%3EScript%20PowerShell%20%3A%20Delete%20VM%20Item%20backup%20and%20the%20Recovery%20Service%20Vault%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2936260%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3E***Steps%20%3A%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E-Disable%26nbsp%3Bsoft%26nbsp%3Bdelete%26nbsp%3Bfor%26nbsp%3Bthe%26nbsp%3BAzure%26nbsp%3BBackup%26nbsp%3BRecovery%26nbsp%3BServices%26nbsp%3Bvault%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E-Check%26nbsp%3Bif%26nbsp%3Bthere%26nbsp%3Bare%26nbsp%3Bbackup%26nbsp%3Bitems%26nbsp%3Bin%26nbsp%3Ba%26nbsp%3Bsoft-deleted%26nbsp%3Bstate%26nbsp%3Band%26nbsp%3Breverse%26nbsp%3Bthe%26nbsp%3Bdelete%26nbsp%3Boperation%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E-Stop%26nbsp%3Bprotection%26nbsp%3Band%26nbsp%3Bdelete%26nbsp%3Bdata%26nbsp%3Bfor%26nbsp%3Ball%26nbsp%3Bbackup-protected%26nbsp%3Bitems%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E-Delete%26nbsp%3Bthe%26nbsp%3BRecovery%26nbsp%3BServices%26nbsp%3Bvault%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E*******************************************************************************************%3C%2FP%3E%3CP%3E%23%23%26nbsp%3BVariables%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24rgBackup%26nbsp%3B%3D%26nbsp%3B%22RG_Name%22%26nbsp%3B%3C%2FP%3E%3CP%3E%24rgBackupInstanRecovery%26nbsp%3B%3D%26nbsp%3B%22RG_Name%22%26nbsp%3B%3C%2FP%3E%3CP%3E%24vaultName%26nbsp%3B%3D%26nbsp%3B%22vault_name%22%26nbsp%3B%3C%2FP%3E%3CP%3E%24vault%26nbsp%3B%3D%26nbsp%3BGet-AzRecoveryServicesVault%26nbsp%3B-ResourceGroupName%26nbsp%3B%24rgBackup%26nbsp%3B-Name%26nbsp%3B%24vaultName%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%23%26nbsp%3B----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------%3C%2FP%3E%3CP%3E%26nbsp%3B%23%23%26nbsp%3BDisable%26nbsp%3Bsoft%26nbsp%3Bdelete%26nbsp%3Bfor%26nbsp%3Bthe%26nbsp%3BAzure%26nbsp%3BBackup%26nbsp%3BRecovery%26nbsp%3BServices%26nbsp%3Bvault%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESet-AzRecoveryServicesVaultProperty%26nbsp%3B-Vault%26nbsp%3B%24vault.ID%26nbsp%3B-SoftDeleteFeatureState%26nbsp%3BDisable%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWrite-Host%26nbsp%3B(%24writeEmptyLine%26nbsp%3B%2B%26nbsp%3B%22%26nbsp%3B%23%26nbsp%3BSoft%26nbsp%3Bdelete%26nbsp%3Bdisabled%26nbsp%3Bfor%26nbsp%3BRecovery%26nbsp%3BService%26nbsp%3Bvault%26nbsp%3B%22%26nbsp%3B%2B%26nbsp%3B%24vault.Name)%60%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%23%26nbsp%3B----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%23%26nbsp%3BCheck%26nbsp%3Bif%26nbsp%3Bthere%26nbsp%3Bare%26nbsp%3Bbackup%26nbsp%3Bitems%26nbsp%3Bin%26nbsp%3Ba%26nbsp%3Bsoft-deleted%26nbsp%3Bstate%26nbsp%3Band%26nbsp%3Breverse%26nbsp%3Bthe%26nbsp%3Bdelete%26nbsp%3Boperation%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24containerSoftDelete%26nbsp%3B%3D%26nbsp%3BGet-AzRecoveryServicesBackupItem%26nbsp%3B-BackupManagementType%26nbsp%3BAzureWorkload%26nbsp%3B-WorkloadType%26nbsp%3BMSSQL%26nbsp%3B%26nbsp%3B-VaultId%26nbsp%3B%24vault.ID%26nbsp%3B%7C%26nbsp%3BWhere-Object%26nbsp%3B%7B%24_.DeleteState%26nbsp%3B-eq%26nbsp%3B%22ToBeDeleted%22%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eforeach%26nbsp%3B(%24item%26nbsp%3Bin%26nbsp%3B%24containerSoftDelete)%26nbsp%3B%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BUndo-AzRecoveryServicesBackupItemDeletion%26nbsp%3B-Item%26nbsp%3B%24item%26nbsp%3B-VaultId%26nbsp%3B%24vault.ID%26nbsp%3B-Force%26nbsp%3B-Verbose%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWrite-Host%26nbsp%3B(%24writeEmptyLine%26nbsp%3B%2B%26nbsp%3B%22%23%26nbsp%3BUndeleted%26nbsp%3Ball%26nbsp%3Bbackup%26nbsp%3Bitems%26nbsp%3Bin%26nbsp%3Ba%26nbsp%3Bsoft%26nbsp%3Bdeleted%26nbsp%3Bstate%26nbsp%3Bin%26nbsp%3BRecovery%26nbsp%3BServices%26nbsp%3Bvault%26nbsp%3B%22%26nbsp%3B%2B%26nbsp%3B%24vault.Name)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%23%26nbsp%3B----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%23%26nbsp%3BStop%26nbsp%3Bprotection%26nbsp%3Band%26nbsp%3Bdelete%26nbsp%3Bdata%26nbsp%3Bfor%26nbsp%3Ball%26nbsp%3Bbackup-protected%26nbsp%3Bitems%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24containerBackup%26nbsp%3B%3D%26nbsp%3BGet-AzRecoveryServicesBackupItem%26nbsp%3B-BackupManagementType%26nbsp%3BAzureVM%26nbsp%3B-WorkloadType%26nbsp%3BAzureVM%26nbsp%3B-VaultId%26nbsp%3B%24vault.ID%26nbsp%3B%7C%26nbsp%3BWhere-Object%26nbsp%3B%7B%24_.DeleteState%26nbsp%3B-eq%26nbsp%3B%22NotDeleted%22%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eforeach%26nbsp%3B(%24item%26nbsp%3Bin%26nbsp%3B%24containerBackup)%26nbsp%3B%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BDisable-AzRecoveryServicesBackupProtection%26nbsp%3B-Item%26nbsp%3B%24item%26nbsp%3B-VaultId%26nbsp%3B%24vault.ID%26nbsp%3B-RemoveRecoveryPoints%26nbsp%3B-Force%26nbsp%3B-Verbose%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWrite-Host%26nbsp%3B(%24writeEmptyLine%26nbsp%3B%2B%26nbsp%3B%22%23%26nbsp%3BDeleted%26nbsp%3Bbackup%26nbsp%3Bdate%26nbsp%3Bfor%26nbsp%3Ball%26nbsp%3Bcloud%26nbsp%3Bprotected%26nbsp%3Bitems%26nbsp%3Bin%26nbsp%3BRecovery%26nbsp%3BServices%26nbsp%3Bvault%26nbsp%3B%22%26nbsp%3B%2B%26nbsp%3B%24vault.Name)%60%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%23%26nbsp%3B----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%23%26nbsp%3BDelete%26nbsp%3Bthe%26nbsp%3BRecovery%26nbsp%3BServices%26nbsp%3Bvault%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERemove-AzRecoveryServicesVault%26nbsp%3B-Vault%26nbsp%3B%24vault%26nbsp%3B-Verbose%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWrite-Host%26nbsp%3B(%24writeEmptyLine%26nbsp%3B%2B%26nbsp%3B%22%23%26nbsp%3BRecovery%26nbsp%3BServices%26nbsp%3Bvault%26nbsp%3B%22%26nbsp%3B%2B%26nbsp%3B%24vault.Name%26nbsp%3B%2B%26nbsp%3B%22%26nbsp%3Bdeleted%22%26nbsp%3B%2B%26nbsp%3B%24writeSeperatorSpaces%26nbsp%3B%2B%26nbsp%3B%24currentTime)%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

***Steps :

-Disable soft delete for the Azure Backup Recovery Services vault

-Check if there are backup items in a soft-deleted state and reverse the delete operation

-Stop protection and delete data for all backup-protected items

-Delete the Recovery Services vault

 

*******************************************************************************************

## Variables

 

$rgBackup = "RG_Name" 

$rgBackupInstanRecovery = "RG_Name" 

$vaultName = "vault_name" 

$vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName

 

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 ## Disable soft delete for the Azure Backup Recovery Services vault

 

Set-AzRecoveryServicesVaultProperty -Vault $vault.ID -SoftDeleteFeatureState Disable

 

Write-Host ($writeEmptyLine + " # Soft delete disabled for Recovery Service vault " + $vault.Name)`

 

 

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

## Check if there are backup items in a soft-deleted state and reverse the delete operation

 

$containerSoftDelete = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureWorkload -WorkloadType MSSQL  -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "ToBeDeleted"}

 

foreach ($item in $containerSoftDelete) {

    Undo-AzRecoveryServicesBackupItemDeletion -Item $item -VaultId $vault.ID -Force -Verbose

}

 

Write-Host ($writeEmptyLine + "# Undeleted all backup items in a soft deleted state in Recovery Services vault " + $vault.Name)

 

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

## Stop protection and delete data for all backup-protected items

 

$containerBackup = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureVM -WorkloadType AzureVM -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "NotDeleted"}

 

foreach ($item in $containerBackup) {

    Disable-AzRecoveryServicesBackupProtection -Item $item -VaultId $vault.ID -RemoveRecoveryPoints -Force -Verbose

}

 

Write-Host ($writeEmptyLine + "# Deleted backup date for all cloud protected items in Recovery Services vault " + $vault.Name)`

 

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

## Delete the Recovery Services vault

 

Remove-AzRecoveryServicesVault -Vault $vault -Verbose

 

Write-Host ($writeEmptyLine + "# Recovery Services vault " + $vault.Name + " deleted" + $writeSeperatorSpaces + $currentTime)

0 Replies