Tuesday, 20 September 2016

Convert Classic Azure VM to Azure Resource Manager with Powershell

Problem


I have a classic VM which I want to move into an Azure Resource Manager machine. The only way this is currently possible is to recreate a new machine using an existing VHD. Unfortunately this doesn’t work if you just try to create an RM machine using the existing classic storage VHD.

You can get the VHD location for the powershell code from Azure Portal, click on the stroage account and in through blobs, vhds etc.

Powershell Code

Login-AzureRMAccount

#New VM Information
$rgName="ServersUS"
$netrgName="ServersUS"
$vnetName="NetworkUS"
$subnetIndex=0

$locName="Central US"
$vmName="newvm"
$vmSize="Basic_A2"

#Old VM Information
$osDiskUri="https://classicalstorageaccount.blob.core.windows.net/vhds/classic.vhd"
$diskName=$osDiskUri.Split("/")[-1] #This should just be the filename of the vhd

$vnet=Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $netrgname
$nicName="$($vmName)-nic"
$nic=New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $netrgName `
    -Location $locName -SubnetId $vnet.Subnets[$subnetIndex].Id
$vm=New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$vm=Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$osDiskCaching = 'ReadWrite'
$vm=Set-AzureRmVMOSDisk -VM $vm -VhdUri $osDiskUri -name $DiskName `
    -CreateOption attach -Windows -Caching $osDiskCaching

New-AzureRmVM -ResourceGroupName $rgName -Location $locName -VM $vm

Error while using classic storage

WARNING: Error occurred when getting storage account, classicalstorageaccount, for boot diagnostics: Sequence contains no matching element
WARNING: Since the VM is created using premium storage, existing standard storage account, rmstorageaccount, is used for boot diagnostics.
New-AzureRmVM : Long running operation failed with status 'Failed'.
StartTime: 20/09/2016 14:48:55
EndTime: 20/09/2016 14:48:56
OperationID: f49d21d3-b3e5-44c0-85d7-72bba687b371
Status: Failed
ErrorCode: AcquireDiskLeaseFailed
ErrorMessage: Failed to acquire lease while creating disk 'classic.vhd' using blob with URI https://classicalstorageaccount.blob.core.windows.net/vhds/classic.vhd. Blob is already in use.
At line:29 char:1
+ New-AzureRmVM -ResourceGroupName $rgName -Location $locName -VM $vm
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzureRmVM], ComputeCloudExcep
   tion
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.NewAzureVMCommand

Resolution

Copy the vhd to a new storage account using the Azure Storage Explorer or Powershell. You will need to change the access policy on the vhd to Blob or Container in order to copy using the Storage Explorer. There is a powershell method (linked above) which allows you to copy the file using authentication.


Changing the access policy for the vhd file in Azure Portal

File copy took about 20 mins going from central US classic to Central US Resource Manager. Once completed the powershell code above can be used to create a new virtual machine with an existing VHD

1 comment:

  1. Thanks for your documentation Dave, help us alot.

    ReplyDelete

Please be nice! :)

Nutanix CE 2.0 on ESXi AOS Upgrade Hangs

AOS Upgrade on ESXi from 6.5.2 to 6.5.3.6 hangs. Issue I have tried to upgrade my Nutanix CE 2.0 based on ESXi to a newer AOS version for ...