skip to Main Content

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


  1. Chosen as BEST ANSWER

    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 *********************************************************"
    
    
    

  2. 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.

    ## 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 AzureStorage -WorkloadType AzureFiles -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 AzureStorage -WorkloadType AzureFiles -VaultId $vault.ID
    
    $vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgBackup -Name $vaultName
    
    Set-AzRecoveryServicesVaultContext -Vault $vault
    
    $containerBackup | foreach-Object -parallel {
    
        Disable-AzRecoveryServicesBackupProtection -Item $_ -RemoveRecoveryPoints -Force -Verbose
    
    }
    Write-Host ($writeEmptyLine + "# Deleted backup date for all cloud protected items in Recovery Services vault " + $vault.Name)
    

    Results:

    enter image description here

    However, if you want to make this work using PowerShell version 5 you can follow Foreach loop parallelly in PowerShell by tutorialspoint.com

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search