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

