$VaultName = “app–rs” # Vault Name for Recovery Points (Backups)
$VmName = “app-a” # VM Name of the backup to be used
$StAcName = “appd2kjflekhn67u” # Storage Account Name to be used for recovery
$StAcRGName = “appst” # Storage Account Resource Group Name
$Region = “UK South”
$RestoredVmName = “db-a” # Name for Restored VM (Can be same name as source if souce has been deleted)
$destination_path = “C:tempvmconfig.json” # Path to download config file
#### Set context
Get-AzureRmRecoveryServicesVault -Name $VaultName | Set-AzureRmRecoveryServicesVaultContext
#### Select required VM
$backupitem = Get-AzureRmRecoveryServicesBackupItem -BackupManagementType AzureVM -WorkloadType AzureVM -Name $VmName
#### List available recovery points for up to 7 days old or alter value (-7)
$startDate = (Get-Date).AddDays(-7)
$endDate = Get-Date
$rp = Get-AzureRmRecoveryServicesBackupRecoveryPoint -Item $backupitem -StartDate $startdate.ToUniversalTime() -EndDate $enddate.ToUniversalTime()
$rp | ft
#Select the restore point – 0 = Most recent RP, 1 = 2nd newest RP etc…
Write-Host “Select the restore point – latest is 0, next is 1 etc…” -ForegroundColor Red
$rpselected = ($input = Read-Host)
$rps = $rp[$rpselected]
#### Define Storage Account to be used
$restorejob = Restore-AzureRMRecoveryServicesBackupItem -RecoveryPoint $rps -StorageAccountName $StAcName -StorageAccountResourceGroupName $StAcRGName
#### Start Restore Job
$restorejob
#### Monitor job status
Do {
Start-Sleep -Seconds 180
Get-Date -Format T
$JobStatus = Get-AzureRmRecoveryServicesBackupJobDetails -job $restorejob
$JobStatus
$JobStatusCheck = $JobStatus.Status
} While ($JobStatusCheck -eq “InProgress”)
If ($JobStatusCheck -ne “Completed”) {
Write-Host “Seems Job has not completed successfully!”
exit
}
Write-Host “Creating VM from restored disks” -ForegroundColor Yellow
### Get details for restored disks
Write-Host “Getting details for restored disks” -ForegroundColor Yellow
$details = Get-AzureRmRecoveryServicesBackupJobDetails -job $restorejob
#### used for the restore process. Do not alter
$properties = $details.properties
$storageAccountName = $properties[“Target Storage Account Name”]
$containerName = $properties[“Config Blob Container Name”]
$blobName = $properties[“Config Blob Name”]
#### Set the Azure storage context and restore the JSON configuration file
Write-Host “Setting Azure storage context and restore the JSON configuration file” -ForegroundColor Yellow
Set-AzureRmCurrentStorageAccount -Name $storageaccountname -ResourceGroupName $StAcRGName
Get-AzureStorageBlobContent -Container $containerName -Blob $blobName -Destination $destination_path
$obj = ((Get-Content -Path $destination_path -Encoding Unicode)).TrimEnd([char]0x00) | ConvertFrom-Json
#### Use the JSON configuration file to create the VM configuration
Write-Host “Creating VM configuration from JSON config” -ForegroundColor Yellow
$vm = New-AzureRmVMConfig -VMSize $obj.HardwareProfile.VirtualMachineSize -VMName $RestoredVmName
#### Attach the OS disk and data disks
Write-Host “Attaching OS and data disks” -ForegroundColor Yellow
Set-AzureRmVMOSDisk -VM $vm -Name “OsDisk12” -VhdUri $obj.StorageProfile.OSDisk.VirtualHardDisk.Uri -CreateOption “Attach”
$vm.StorageProfile.OsDisk.OsType = $obj.StorageProfile.OSDisk.OperatingSystemType
foreach($dd in $obj.StorageProfile.DataDisks)
{
$vm = Add-AzureRmVMDataDisk -VM $vm -Name “datadisk1” -VhdUri $dd.VirtualHardDisk.Uri -DiskSizeInGB $null -Lun $dd.Lun -CreateOption Attach
}
#### Network settings
Write-Host “Configuring Network settings” -ForegroundColor Yellow
## Get Nic URI
$PrimaryNIC = $obj.NetworkProfile | select -ExpandProperty NetworkInterfaces | Where-Object {$_.Primary -eq “True”} | select ReferenceURI | Format-Table -HideTableHeaders
## Get Nic Name from URI
$NicURIstring = $PrimaryNIC | Out-String
$PNicNameURI = $NicURIstring.Split(“/”)[-1]
$PNicName = $PNicNameURI.trim()
$GetPrimaryNic = Get-AzureRmNetworkInterface -Name $PNicName -ResourceGroupName $StAcRGName
$VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $GetPrimaryNic.Id -Primary
$OtherNICs = ($obj.NetworkProfile | select -ExpandProperty NetworkInterfaces | Where-Object {$_.Primary -ne “True”} | select ReferenceURI | Format-Table -HideTableHeaders | Out-String).Trim()
$OtherNICs.Split(“`r”) | % {
$NicURIstring = $_
$ONicNameURI = $NicURIstring.Split(“/”)[-1]
$ONicName = $ONicNameURI.trim()
$GetOtherNic = Get-AzureRmNetworkInterface -Name $ONicName -ResourceGroupName $StAcRGName
$VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $GetOtherNic.Id}
#### Create VM
Write-Host “Creating VM $RestoredVmName” -ForegroundColor Yellow
$vm.StorageProfile.OsDisk.OsType = $obj.StorageProfile.OSDisk.OperatingSystemType
New-AzureRmVM -ResourceGroupName $StAcRGName -Location $Region -VM $vm
### END SCRIPT ###