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)`
I could solve the problem with this script. It can be improved:
# Powershell version >= 7.0
# Si al eliminar da error de formato de fecha, probar a lanzarlo desde una VM ubicada en North Europe en inglés ya que en algunas máquinas no da este error.
# Autenticarse en Azure
Connect-AzAccount -Tenant "[TENANT ID]"
Set-AzContext -SubscriptionId "[SUBSCRIPTION ID]"
# VARIABLES
[String]$rgBackup = "[RG]"
[String]$vaultName = "[RECOVERY SERVICE VAULT NAME]"
# $vaultId = "[RECOVERY SERVICE VAULT ID]"
$vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
$containerBackup = Get-AzRecoveryServicesBackupItem -BackupManagementType AzureVM -WorkloadType AzureVM -VaultId $vault.ID | Where-Object {$_.DeleteState -eq "NotDeleted"}
$containerbackup| ForEach-Object -ThrottleLimit 10 -Parallel {write-host -fore green $_.name; Disable-AzRecoveryServicesBackupProtection -Item $_ -VaultId "[RECOVERY SERVICE VAULT ID WITHOUT VARIABLES]" -RemoveRecoveryPoints -Force -Verbose}
Write-Host "********************************* FIN *********************************************************"