Compartir a través de


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

 

 

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