Forum Discussion
Azure PowerShell Script to create Virtual machine from Azure Recovery Service vault restore
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
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.OsTypeCould 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. https://resources.microsoft.com/subscriptions/987a.... . or just /subscriptions/987a.... .)
Something in that object is using a relative URI where as the New-AzVM command is expecting an absolute URI.
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.Idto
Add-AzVMNetworkInterface -VM $VMCreateName -Id $VMNIC.IdThanks
Omar
- nirmalmcse02Copper Contributoromarmciver
I have replaced the lines as you have shared. Still this powershell is getting failed due to same error.
Please suggest.
Below is error.
Name : TestVMRESTORE
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.NewAzureVMCommandHey 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.OsTypeCould 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. https://resources.microsoft.com/subscriptions/987a.... . or just /subscriptions/987a.... .)
Something in that object is using a relative URI where as the New-AzVM command is expecting an absolute URI.