Migrating Azure Virtual Machines from ASM to ARM


The Azure Preview Portal is generally available and Microsoft is recommending that all new deployments be based on the ARM model. What about your existing ASM deployments? Microsoft will in time provide an automated approach to migrate existing ASM deployments to ARM, however if you are ready to take the plunge here is a PowerShell script to get you started with your migration.

 

The scripts does two things. Firstly, it allows you to copy your source vhd blob from an existing storage account to a new ARM storage account. Secondly it will create a new ARM based Virtual Machine and attach your copied vhd blob. Ensure that you are on the latest version of the Azure PowerShell module, 1.0.1 at the time of writing this article.

 

The script assumes that you have provisioned a new ARM Resource Group with the required resources (Storage Account, Virtual Network, Subnets etc).

 

########################################################################

 

# Connect to an Azure subscription

 

Login-AzureRmAccount

 

# Select your subscription if required

 

Select-AzureRmSubscription SubscriptionId "yoursubscriptionid"

 

 

########################################################################

 

Copy a Vhd from an existing storage account to a new storage account

 

########################################################################

 

 

# Source VHD

$srcVhd = "https://sourcesa.blob.core.windows.net/vhds/sourcevhd.vhd"

 

# Destination VHD name

 

$destVhdName = "destvhd.vhd"

 

# Destination Container Name

$destContainerName = "vhds"

 

# Source Storage Account and Key

 

$srcStorageAccount = "sourcesa"

$srcStorageKey = "sourcesakey"

 

# Target Storage Account and Key

 

$destStorageAccount = "destsa"

$destStorageKey = "destsakey"

 

# Create the source storage account context (this creates the context, it does not actually create a new storage account)

 

$srcContext = New-AzureStorageContext StorageAccountName $srcStorageAccount StorageAccountKey $srcStorageKey

                                        

 

# Create the destination storage account context

 

$destContext = New-AzureStorageContext StorageAccountName $destStorageAccount StorageAccountKey $destStorageKey

 

 

# Start the copy 

 

$blob1 = Start-AzureStorageBlobCopy -Context $srcContext AbsoluteUri $srcVhd DestContainer $destContainerName DestBlob $destVhdName DestContext $destContext -Verbose

 

 

# check status of copy

 

$blob1 | Get-AzureStorageBlobCopyState

 

 

########################################################################

 

Create a Virtual Machine from an existing OS disk

 

########################################################################

 

$resourceGroupName= "dest-rg"

$location= "SouthEastAsia"

$storageAccountName= "destsa"

 

$vmName= "destvm"

$vmSize="Standard_A1"

 

$vnetName= "dest-vnet"

 

 

# Get storage account configuration for the target storage account

 

$StorageAccount = Get-AzureRmStorageAccount ResourceGroupName $resourcegroupName AccountName $storageAccountNAme

 

  

#Get Virtual Network configuration

 

$vnet = Get-AzureRmVirtualNetwork -Name $vnetName ResourceGroupName $resourceGroupName

 

 

# Create VM from an existing image

 

 

$OSDiskName="$vmName-C-01"

 

$vm = New-AzureRmVMConfig vmName $vmName vmSize $vmSize

  

$nic = New-AzureRmNetworkInterface -Name $nicName ResourceGroupName $resourceGroupName -Location $location SubnetId $vnet.Subnets[1].Id

 

$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id

  

$destOSDiskUri = "https://destsa.blob.core.windows.net/vhds/destvhd.vhd"

  

# Set the OS disk properties for the new VM. If you are migrating a Linux machine use the -Linux switch instead of -Windows

 

$vm = Set-AzureRmVMOSDisk -VM $vm -Name $OSDiskName VhdUri $destOSDiskUri -Windows –CreateOption attach

 

New-AzureRmVM ResourceGroupName $resourceGroupName -Location $location -VM $vm

 

 

########################################################################

 

 

 

Comments (13)

  1. Dennis van Doorn says:

    Hi Diego, great script. Tested your script to see how ASM to ARM conversion goes this way. I ran into a problem and noticed $NicName was not set. Adding $NicName="$vmName-nic01" below the line "# Create VM from an existing image" solved this.

  2. Lifesaver! Thanks for sharing.

  3. Hi Dennis, Apologies i did not define the $NicName variable. Thanks

    1. deep says:

      Hello Diego,

      Thanks! That was a great script.

      I’ve couple of questions for you, i hope these are relevant,

      1. Would like to understand will there be any downtime? like ours is customer facing environment and cant really afford any downtime.
      2. We have a big setup and would like to know, is there any automated process published by Microsoft to move all the ASM to ARMs? If i am not wrong down the line ASM will be retired and the platform heads towards ARM….Appreciate if you could answer these for me

      1. Hi Deep, Thanks for your email. Yes this method does require an outage as we are deleting the shell of the VM and creating a new VM in the Resource Manager Model. We have recently released a new method of migration which provides automation and zero outage. Please refer to this article : https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-windows-migration-classic-resource-manager/

  4. Harikrishna Beerangi says:

    $vnet.Subnets[1].Id needs some clarification. can you please provide this. in that ‘1’ is meant for 2nd subnet under the vnet and ‘0’ is meant for 1st subnet under the vnet , isn’t it?

    1. Hi Harikrishna,

      You are spot on. The Subnets attribute stores the subnet list like an array, so Subnets[0] would be the first subnet in the array.

  5. Pawan Dubey says:

    Hi Diego,
    after migrating the vhd and creating arm vm using the migrated vhd, the vm is visible in the new portal, however the computer name and ip is blank. is there any significance of SysPrep in this scenario?

    1. Pawan Dubey says:

      Sorry Diego..
      The reason I found, I need to add public ip to access this.
      After adding public ip everything works as expected.

  6. Karthikeyan R says:

    Hi Diego,
    I’m getting this error while copying VHD. Any idea on this.?

    412 ConditionNotMet “Copy failed when reading the source.”

    Regards,
    KR

    1. Karthikeyan R says:

      I’m facing this error, anyone faced this problem?

      C:\> $nic = New-AzureRmNetworkInterface -Name $nicName –ResourceGroupName $resourceGroupName -Location $location –Sub
      etId $vnetName.Subnets[0].Id
      annot index into a null array.
      t line:1 char:1
      $nic = New-AzureRmNetworkInterface -Name $nicName –ResourceGroupName …
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : NullArray

Skip to main content