Azure PowerShell Script to create Virtual machine from Azure Recovery Service vault restore

Occasional Contributor

I am using below PowerShell script to create VM from Recovery Service Vault restore however this is not working. Could you please help me to debug?


PowerShell Error

New-AzVM : This operation is not supported for a relative URI.


Below is my PowerShell Script

$ConfigBlobName = "resotevmconfig.json"
$ContainerName = "restorevmcontainername"
$TemplateName = "azuredeploy.json"
$JSONpath = "exportjsonpath"
$StorageAccountRestore = "storageaccountnamewhererestorewashappened"
$StorageAccountRestoreRGName = "Resourcegroupnameofstorage"
$VMRestoreName = "TestVMRESTORE"
$VMVNETName = "Virtualnetworkname"
$VMVNETRGName = "virtualnetworkRG"
$VMNICLocation = "location"
$VNETSubnetIndex= 1
$VMRestoreRGName = "RestorevmRGname"
# Step 8 - Prepare the VM restore
Set-AzCurrentStorageAccount -Name $StorageAccountRestore -ResourceGroupName $StorageAccountRestoreRGName
New-AzStorageBlobSASToken -Container $ContainerName -Blob $TemplateName -Permission r -FullUri
Get-AzStorageBlobContent -Container $ContainerName -Blob $ConfigBlobName -Destination $JSONpath
$VMObject = ((Get-Content -Path $JSONpath -Raw -Encoding Unicode)).TrimEnd([char]0x00) | ConvertFrom-Json
$VMCreateName = New-AzVMConfig -VMSize $VMObject.'properties.hardwareProfile'.vmSize -VMName "$VMRestoreName"
Set-AzVMOSDisk -VM $VMCreateName -Name "$VMRestoreName" -VhdUri $VMObject.'properties.StorageProfile'.OsDisk -CreateOption "Attach"
Set-AzVMOSDisk -VM $VMCreateName -Name "test1-restored-OsDisk" -VhdUri $VMObject.'properties.storageProfile'.osDisk.vhd -CreateOption "Attach" -Windows
$VMCreateName.StorageProfile.OsDisk.OsType = $VMObject.'properties.StorageProfile'.OsDisk.OsType
$VNETName = Get-AzVirtualNetwork -Name "$VMVNETName" -ResourceGroupName "$VMVNETRGName"
$VMNIC = New-AzNetworkInterface -Name $VMNICName -ResourceGroupName "$VMRestoreRGName" -Location "$VMNICLocation" -SubnetId $VNETName.Subnets[$VNETSubnetIndex].Id 
$VMCreateName=Add-AzVMNetworkInterface -VM $VMCreateName -Id $VMNIC.Id
# Step 9 - Start the VM restore
New-AzVM -ResourceGroupName "$VMRestoreRGName" -Location "$VMNICLocation" -VM $VMCreateName -Verbose
3 Replies

Hey @nirmalmcse02 


Have a look at the $VMCreateName parameter.


On line 18 you use this to store a VM object (not just a name), and then on line 25 you use this object to Add the network interface to the VM. At the same time you assign the return of that Add-AzVMNetworkInterface to the same variable.


Try changing line 25 from

$VMCreateName=Add-AzVMNetworkInterface -VM $VMCreateName -Id $VMNIC.Id


Add-AzVMNetworkInterface -VM $VMCreateName -Id $VMNIC.Id





I have replaced the lines as you have shared. Still this powershell is getting failed due to same error.
Please suggest.
Below is error.
HardwareProfile : {VmSize}
NetworkProfile : {NetworkInterfaces}
StorageProfile : {OsDisk}

PS C:\> # Step 9 - Start the VM restore
PS C:\> New-AzVM -ResourceGroupName "$VMRestoreRGName" -Location "$VMNICLocation" -VM $VMCreateName -Verbose
New-AzVM : This operation is not supported for a relative URI.
At line:1 char:1
+ New-AzVM -ResourceGroupName "$VMRestoreRGName" -Location "$VMNICLocat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzVM], InvalidOperationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.NewAzureVMCommand
best response confirmed by nirmalmcse02 (Occasional Contributor)

Hey @nirmalmcse02,


You also seem to be setting the VM OS Disk twice.

Set-AzVMOSDisk -VM $VMCreateName -Name "$VMRestoreName" -VhdUri $VMObject.'properties.StorageProfile'.OsDisk -CreateOption "Attach"

Set-AzVMOSDisk -VM $VMCreateName -Name "test1-restored-OsDisk" -VhdUri $VMObject.'properties.storageProfile'.osDisk.vhd -CreateOption "Attach" -Windows
$VMCreateName.StorageProfile.OsDisk.OsType = $VMObject.'properties.StorageProfile'.OsDisk.OsType


Could you add:

Write-Output $VMCreateName

 ...before the New-AzVM command.


If you need to redact any of the content, please be sure to identity for where you had relative URIs vs absolute (i.e. . or just /subscriptions/987a.... .)


Something in that object is using a relative URI where as the New-AzVM command is expecting an absolute URI.