Partager via


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

  • Anonymous
    December 12, 2015
    Thanks
  • Anonymous
    January 21, 2016
    The comment has been removed
  • Anonymous
    January 27, 2016
    Lifesaver! Thanks for sharing.
  • Anonymous
    February 08, 2016
    Hi Dennis, Apologies i did not define the $NicName variable. Thanks
  • Anonymous
    March 31, 2016
    Thanks !
  • Anonymous
    June 15, 2016
    $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?
    • Anonymous
      July 24, 2016
      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.
  • Anonymous
    September 14, 2016
    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?
    • Anonymous
      September 14, 2016
      Sorry Diego..The reason I found, I need to add public ip to access this.After adding public ip everything works as expected.
  • Anonymous
    April 11, 2017
    Hi Diego,I'm getting this error while copying VHD. Any idea on this.?412 ConditionNotMet "Copy failed when reading the source."Regards,KR
    • Anonymous
      April 12, 2017
      The comment has been removed