Phidiax Tech Blog

Adventures in custom software and technology implementation.

Azure VM Migration from ASM to ARM using AzCopy and PowerShell

During recent attempts to manage migration to Azure Resource Management (ARM), more here Azure Resource Manager Overview, from Azure Service Management (ASM), we were able to implement an alternative to using the ASM2ARM Tool, by utilizing AzCopy and PowerShell.

Here is the previous Phidiax ASM2ARM Tool blog: Azure VM Migration from ASM to ARM Using the ASM2ARM Tool - Multiple VMs and Virtual Network

This approach to migrating resources from ASM model to the new ARM model allows for potential automation of this service. For the majority of these migration use cases the ASM2ARM Tool may be a better option, however I recently found this approach to be easier to execute.

The idea here is to migrate a VM into a preconfigured Azure Resource Group that may contain other resources and VMs. This approach may also be a good platform to upgrade the VM running Standard Storage to a Premium Storage in ARM.

AzCopy is the preferred and supported way to copy VMs between Azure and on-premise. Download the latest version of AzCopy.

For this exercise we will use AzCopy to copy from Azure SM (Classic) to Azure RM. Now, AzCopy can be run from PowerShell or CMD shell. For best results, consider running these scripts in a VM that resides within the Azure region that you will be copying across. This will reduce the time of the migration and is less prone to error.

Run AzCopy

#Run AzCopy using a vm in Azure in the regional locale
start cmd /k AzCopy /Source:https://<sourcedomain>.blob.core.windows.net/<VHDFolder>`
/SourceKey:<sourcekey> /Dest:https://<destdomain>.blob.core.windows.net/<VHDFolder>`
/DestKey:<destkey> /Pattern:<VHDName>.vhd /z:"%cd%\tmp" /nc:512 /SyncCopy /V

New VM resource Allocation

After the VHD files are copied to the new storage container in the desired ARM storage account, the next step is to begin the build of the VM resources that the VHD will inhabit.

The first step is to personalize the configuration for your new VM. Edit the variables below to reflect the desired resource names and also all existing resources like the Resource Group and Storage Account names, etc.

#Resource allocation in Azure RM
$pipName = "<pubipName>"
$rgName = "<YourResourceGroupName>"
$location = "<ex. East US 2>"
$pip = New-AzureRmPublicIpAddress -Name $pipName -ResourceGroupName $rgName -Location $location -AllocationMethod Dynamic
$subnet1Name = "<subnetName>"
$nicname = "<nicName>"
$vnetName = "<vnetName>"
$vnetAddressPrefix = "<ex. 192.168.0.0/23>"
$vnetSubnetAddressPrefix = "<ex. 192.168.1.0/24>"

Build Virtual Networking

These lines can be used to link to an existing networking configuration or build a new virtual network for your VM resources.

#Use for building new vnet, subnet and nic
$nic = New-AzureRmNetworkInterface -Name $nicname -ResourceGroupName $rgName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName -Location $location -AddressPrefix $vnetAddressPrefix -Subnet $subnetconfig
$subnetconfig = New-AzureRmVirtualNetworkSubnetConfig -Name $subnet1Name -AddressPrefix $vnetSubnetAddressPrefix

#!!Use below if using existing vnet, subnet and nic!!
#$nic = Get-AzureRmNetworkInterface -Name $nicname -ResourceGroupName $rgName
#$vnet = Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName
#$subnetconfig = Get-AzureRmVirtualNetworkSubnetConfig -Name $subnet1Name -VirtualNetwork $vnet

Commit

This next set is where the actions are committed. Edit the first section to reflect the desired VM name and the existing Storage account name that the migrated VHD resides within.

#VM creation
$vmName = "<vmName>"
$storageAccName = "<StorageAcctName>"
$computerName = "<computerName>" 

#Enter a new username and password in the pop-up for the following
$cred = Get-Credential

#Get the storage account where the uploaded image is stored
$storageAcc = Get-AzureRmStorageAccount -ResourceGroupName $rgName -AccountName $storageAccName

#Set the VM name and size
#Use "Get-Help New-AzureRmVMConfig" to know the available options for -VMsize
$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize "<ex. Standard_A1>"

#Set the Windows operating system configuration and add the NIC
$vmNeti = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $nic.Id

#Create the OS disk
$osDiskName = "<osDiskName>"

#Create the OS disk URI
$osDiskUri = "<https://<domainName>.blob.core.windows.net/<Folder>/<vhdFileName>.vhd>" 

#Configure the OS disk to be created from image (-CreateOption fromImage) and give the URL of the uploaded image VHD for the -SourceImageUri parameter

#You can find this URL in the result of the Add-AzureRmVhd cmdlet above
$vmOsDisk = Set-AzureRmVMOSDisk -VM $vmNeti -Name $osDiskName -VhdUri $osDiskUri -CreateOption Attach -Windows

#Create the new VM
New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vmOsDisk

I hope that you find this approach useful! Happy Clouding…

Loading

Privacy Policy  |  Contact  |  Careers

2009-2017 Phidiax, LLC - All Rights Reserved