New Delhi , Moti Nagar
+91-9007641046
subhendu@subhendumct.com

Restore VM from Recovery Service Vault – PowerShell

Live in Future - Live in Cloud

Restore VM from Recovery Service Vault – PowerShell

# Mention your VM here
$VMName = "SubhenduTestVM"
# Mention your Stage Storage here
$stagestorageaccountname = "teststorage"

########################Body####################################################################
$VMRGname = (Get-AzureRmResource -Name $VMName -ResourceType "Microsoft.Compute/virtualMachines").ResourceGroupName
$VMBackupStatus = (Get-AzureRmRecoveryServicesBackupStatus -Name $VMName -ResourceGroupName $VMRGName -Type "AzureVM").BackedUp
$VMRSVVaultID = (Get-AzureRmRecoveryServicesBackupStatus -Name $VMName -ResourceGroupName $VMRGName -Type "AzureVM").VaultId

$VMRSVName = (Get-AzureRmResource -ResourceId $VMRSVVaultID).Name
$RSVRG = (Get-AzureRmResource -Name $VMRSVName).ResourceGroupName
$vault = Get-AzureRMRecoveryServicesVault -ResourceGroupName $RSVRG -Name $VMRSVName
$Container = Get-AzureRMRecoveryServicesBackupContainer -ContainerType "AzureVM" -Status "Registered" -FriendlyName $VMName -VaultId $vault.ID
$BackupItem = Get-AzureRmRecoveryServicesBackupItem -Container $Container -WorkloadType "AzureVM" -VaultId $vault.ID 
$StartDate = (Get-Date).AddDays(-10) 
$EndDate = Get-Date
Get-AzureRMRecoveryServicesVault –Name $VMRSVName | Set-AzureRMRecoveryServicesVaultContext
$RecoveryPointTimeDetails = Get-AzureRmRecoveryServicesBackupRecoveryPoint -Item $BackupItem -StartDate $StartDate.ToUniversalTime() -EndDate $EndDate.ToUniversalTime() -VaultId $VMRSVVaultID
$RPID = $RecoveryPointTimeDetails[0].RecoveryPointId
$RP = Get-AzureRmRecoveryServicesBackupRecoveryPoint -Item $BackupItem -RecoveryPointId $RPID
$stagestorageaccountnameRG = (Get-AzureRmResource -Name $stagestorageaccountname -ResourceType "Microsoft.Storage/storageAccounts").ResourceGroupName
Restore-AzureRmRecoveryServicesBackupItem -RecoveryPoint $RP[0] -StorageAccountName $stagestorageaccountname -StorageAccountResourceGroupName $stagestorageaccountnameRG
################################################################################################

Leave a Reply

Your email address will not be published. Required fields are marked *