Skip to content

Commit 9b5303e

Browse files
authored
Merge pull request #198356 from chaya-na/patch-2
Update delete-recovery-services-vault.md
2 parents c6def75 + 711e315 commit 9b5303e

File tree

1 file changed

+52
-10
lines changed

1 file changed

+52
-10
lines changed

articles/backup/scripts/delete-recovery-services-vault.md

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ This script helps you to delete a Recovery Services vault.
2929
```
3030

3131
1. Launch PowerShell 7 as Administrator.
32-
1. Before you run the script for vault deletion, run the following command to upgrade the _Az module_ to the latest version:
33-
34-
```azurepowershell-interactive
35-
Uninstall-Module -Name Az.RecoveryServices
36-
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
37-
Install-Module -Name Az.RecoveryServices -Repository PSGallery -Force -AllowClobber
38-
```
3932

4033
1. In the PowerShell window, change the path to the location the file is present, and then run the file using **./NameOfFile.ps1**.
4134
1. Provide authentication via browser by signing into your Azure account.
@@ -45,6 +38,26 @@ The script will continue to delete all the backup items and ultimately the entir
4538
## Script
4639

4740
```azurepowershell-interactive
41+
Write-Host "WARNING: Please ensure that you have at least PowerShell 7 before running this script. Visit https://go.microsoft.com/fwlink/?linkid=2181071 for the procedure." -ForegroundColor Yellow
42+
$RSmodule = Get-Module -Name Az.RecoveryServices -ListAvailable
43+
$NWmodule = Get-Module -Name Az.Network -ListAvailable
44+
$RSversion = $RSmodule.Version.ToString()
45+
$NWversion = $NWmodule.Version.ToString()
46+
47+
if($RSversion -lt "5.3.0")
48+
{
49+
Uninstall-Module -Name Az.RecoveryServices
50+
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
51+
Install-Module -Name Az.RecoveryServices -Repository PSGallery -Force -AllowClobber
52+
}
53+
54+
if($NWversion -lt "4.15.0")
55+
{
56+
Uninstall-Module -Name Az.Network
57+
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
58+
Install-Module -Name Az.Network -Repository PSGallery -Force -AllowClobber
59+
}
60+
4861
Connect-AzAccount
4962
5063
$VaultName = "Vault name" #enter vault name
@@ -237,9 +250,38 @@ $backupServersMARSFin = Get-AzRecoveryServicesBackupContainer -ContainerType "Wi
237250
$backupServersMABSFin = Get-AzRecoveryServicesBackupManagementServer -VaultId $VaultToDelete.ID| Where-Object { $_.BackupManagementType -eq "AzureBackupServer" }
238251
$backupServersDPMFin = Get-AzRecoveryServicesBackupManagementServer -VaultId $VaultToDelete.ID | Where-Object { $_.BackupManagementType-eq "SCDPM" }
239252
$pvtendpointsFin = Get-AzPrivateEndpointConnection -PrivateLinkResourceId $VaultToDelete.ID
240-
Write-Host "Number of backup items left in the vault and which need to be deleted:" $backupItemsVMFin.count "Azure VMs" $backupItemsSQLFin.count "SQL Server Backup Items" $backupContainersSQLFin.count "SQL Server Backup Containers" $protectableItemsSQLFin.count "SQL Server Instances" $backupItemsSAPFin.count "SAP HANA backup items" $backupContainersSAPFin.count "SAP HANA Backup Containers" $backupItemsAFSFin.count "Azure File Shares" $StorageAccountsFin.count "Storage Accounts" $backupServersMARSFin.count "MARS Servers" $backupServersMABSFin.count "MAB Servers" $backupServersDPMFin.count "DPM Servers" $pvtendpointsFin.count "Private endpoints"
241-
Write-Host "Number of ASR items left in the vault and which need to be deleted:" $ASRProtectedItems "ASR protected items" $ASRPolicyMappings "ASR policy mappings" $fabricCount "ASR Fabrics" $pvtendpointsFin.count "Private endpoints. Warning: This script will only remove the replication configuration from Azure Site Recovery and not from the source. Please cleanup the source manually. Visit https://go.microsoft.com/fwlink/?linkid=2182781 to learn more"
242-
Remove-AzRecoveryServicesVault -Vault $VaultToDelete
253+
254+
#Display items which are still present in the vault and might be preventing vault deletion.
255+
256+
if($backupItemsVMFin.count -ne 0) {Write-Host $backupItemsVMFin.count "Azure VM backups are still present in the vault. Remove the same for successful vault deletion." -ForegroundColor Red}
257+
if($backupItemsSQLFin.count -ne 0) {Write-Host $backupItemsSQLFin.count "SQL Server Backup Items are still present in the vault. Remove the same for successful vault deletion." -ForegroundColor Red}
258+
if($backupContainersSQLFin.count -ne 0) {Write-Host $backupContainersSQLFin.count "SQL Server Backup Containers are still registered to the vault. Remove the same for successful vault deletion." -ForegroundColor Red}
259+
if($protectableItemsSQLFin.count -ne 0) {Write-Host $protectableItemsSQLFin.count "SQL Server Instances are still present in the vault. Remove the same for successful vault deletion." -ForegroundColor Red}
260+
if($backupItemsSAPFin.count -ne 0) {Write-Host $backupItemsSAPFin.count "SAP HANA Backup Items are still present in the vault. Remove the same for successful vault deletion." -ForegroundColor Red}
261+
if($backupContainersSAPFin.count -ne 0) {Write-Host $backupContainersSAPFin.count "SAP HANA Backup Containers are still registered to the vault. Remove the same for successful vault deletion." -ForegroundColor Red}
262+
if($backupItemsAFSFin.count -ne 0) {Write-Host $backupItemsAFSFin.count "Azure File Shares are still present in the vault. Remove the same for successful vault deletion." -ForegroundColor Red}
263+
if($StorageAccountsFin.count -ne 0) {Write-Host $StorageAccountsFin.count "Storage Accounts are still registered to the vault. Remove the same for successful vault deletion." -ForegroundColor Red}
264+
if($backupServersMARSFin.count -ne 0) {Write-Host $backupServersMARSFin.count "MARS Servers are still registered to the vault. Remove the same for successful vault deletion." -ForegroundColor Red}
265+
if($backupServersMABSFin.count -ne 0) {Write-Host $backupServersMABSFin.count "MAB Servers are still registered to the vault. Remove the same for successful vault deletion." -ForegroundColor Red}
266+
if($backupServersDPMFin.count -ne 0) {Write-Host $backupServersDPMFin.count "DPM Servers are still registered to the vault. Remove the same for successful vault deletion." -ForegroundColor Red}
267+
if($ASRProtectedItems -ne 0) {Write-Host $ASRProtectedItems "ASR protected items are still present in the vault. Remove the same for successful vault deletion." -ForegroundColor Red}
268+
if($ASRPolicyMappings -ne 0) {Write-Host $ASRPolicyMappings "ASR policy mappings are still present in the vault. Remove the same for successful vault deletion." -ForegroundColor Red}
269+
if($fabricCount -ne 0) {Write-Host $fabricCount "ASR Fabrics are still present in the vault. Remove the same for successful vault deletion." -ForegroundColor Red}
270+
if($pvtendpointsFin.count -ne 0) {Write-Host $pvtendpointsFin.count "Private endpoints are still linked to the vault. Remove the same for successful vault deletion." -ForegroundColor Red}
271+
272+
$accesstoken = Get-AzAccessToken
273+
$token = $accesstoken.Token
274+
$authHeader = @{
275+
'Content-Type'='application/json'
276+
'Authorization'='Bearer ' + $token
277+
}
278+
$restUri = "https://management.azure.com/subscriptions/"+$SubscriptionId+'/resourcegroups/'+$ResourceGroup+'/providers/Microsoft.RecoveryServices/vaults/'+$VaultName+'?api-version=2021-06-01&operation=DeleteVaultUsingPS'
279+
$response = Invoke-RestMethod -Uri $restUri -Headers $authHeader -Method DELETE
280+
281+
$VaultDeleted = Get-AzRecoveryServicesVault -Name $VaultName -ResourceGroupName $ResourceGroup -erroraction 'silentlycontinue'
282+
if ($VaultDeleted -eq $null){
283+
Write-Host "Recovery Services Vault" $VaultName "successfully deleted"
284+
}
243285
#Finish
244286
245287
```

0 commit comments

Comments
 (0)