I am trying to delete all backup items in a recovery service vault using powershell. I have found an script in a website but this script waits until a backup is deleted to begin with the following one.
I would like to modify this script to order Azure delete the item and to begin with the follow one without wait.
I tried to use the flag -AsJob but powershell doesn’t recognize it (I have commented it in the code).
I am using the version 5 of powershell.
I have to delete hundreds of backup items.
Could you help me?
## Variables
$rgBackup = Read-Host -Prompt "Resource group name"
$vaultName = Read-Host -Prompt "Recovery service vault name"
$vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
$count = 0
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## 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 #-AsJob
$count++
Write-Host "$count - Item has been deleted"
}
Write-Host ($writeEmptyLine + "# Deleted backup date for all cloud protected items in Recovery Services vault " + $vault.Name)`
2
Answers
I could solve the problem with this script. It can be improved:
After reproducing from my end, I could get the desired results using PowerShell version 7 with the help of
foreach-Object -parallel
to execute deletion jobs parallelly. Fore demonstration purposes, I have used Azure Storage files as my backup items. The following is the script that worked for me.Results:
However, if you want to make this work using PowerShell version 5 you can follow Foreach loop parallelly in PowerShell by tutorialspoint.com