<img alt="" src="https://secure.leadforensics.com/150446.png " style="display:none;">
Go to top icon

PowerShell Script for migrating Azure Classic to Azure Resource Manager

Surbhi Agarwal Feb 01, 2019

Powershell Azure Powershell Azure Resource Manager

First, install the latest version of Azure PowerShell. Ensure that you are an administrator for the subscription in Azure portal: To perform this migration, you must be added as a co-administrator for the subscription in the Azure portal.

Perform the below steps to migrate:

  • Sign into the Azure portal.
  • On the Hub menu, select Subscription. If you don't see it, select All Services.
  • Find the appropriate subscription entry, then look at My Role field. For a co-administrator, the value should be Account admin.
  • If you are not able to add a co-administrator, then contact a service administrator or co-administrator for the subscription to get yourself added.


Below are the main steps to migrate from Azure Classic to Azure Resource Manager:

Set your subscription and sign up for migration:
  • First, start a PowerShell prompt. For migration, you need to set up your environment for both classic and Resource Manager.
  • Sign in to your account for the Resource Manager model.

    Connect-AzureRmAccount

  • Get the available subscriptions by using the following command:

    Get-AzureRMSubscription | Select SubscriptionName

  • Set your Azure subscription for the current session. This example sets the default subscription name to My Azure Subscription. Replace the example subscription name with your own.

    Select-AzureRmSubscription –SubscriptionName "My Azure Subscription"

Register with the migration resource provider by using the following command:

Register-AzureRmResourceProvider -ProviderNamespace Microsoft.ClassicInfrastructureMigrate

Please wait for few minutes for the registration to finish. You can check the status of the approval by using the following command:

Get-AzureRmResourceProvider -ProviderNamespace Microsoft.ClassicInfrastructureMigrate

Now sign in to your account for the classic model.

Add-AzureAccount

  • Get the available subscriptions by using the following command:

    Get-AzureSubscription | Select SubscriptionName

  • Set your Azure subscription for the current session. This example sets the default subscription to My Azure Subscription. Replace the example subscription name with your own.

    Select-AzureSubscription –SubscriptionName "My Azure Subscription"

Make sure you have enough Azure Resource Manager Virtual Machine vCPUs in the Azure region of your current deployment or VNET: You can use the following PowerShell command to check the current number of vCPUs you have in Azure Resource Manager.

To learn more about vCPU quotas, see Limits and the Azure Resource Manager.

  • This example checks the availability in the West US region. Replace the example region name with your own.

    Get-AzureRmVMUsage -Location "West US"

Run commands to migrate your IaaS resources:
  • Migrate VM’s in a cloud service (not in a virtual network)
  • Migrate VM’s in a virtual network
  • Migrate Account
    Please note that you need to migrate virtual network first as the Virtual Machines associated to them will move together with them. Then next move the Storage accounts after migrating the virtual networks.

    To migrate virtual machines in a virtual network, you migrate the virtual network. The virtual machines automatically migrate with the virtual network. Pick the virtual network that you want to migrate.

  • This example sets the virtual network name to myVnet. Replace the example virtual network name with your own.

    $vnetName = "myVnet"

  • First, validate if you can migrate the virtual network by using the following command:

    Move-AzureVirtualNetwork -Validate -VirtualNetworkName $vnetName

  • The following command displays any warnings and errors that block migration. If validation is successful, then you can proceed with the following Prepare step:

    Move-AzureVirtualNetwork -Prepare -VirtualNetworkName $vnetName

  • Check the configuration for the prepared virtual machines by using either Azure PowerShell or the Azure portal. If you are not ready for migration and you want to go back to the old state, use the following command:

    Move-AzureVirtualNetwork -Abort -VirtualNetworkName $vnetName

  • If the prepared configuration looks good, you can move forward and commit the resources by using the following command:

    Move-AzureVirtualNetwork -Commit -VirtualNetworkName $vnetName

Once you're done migrating the virtual machines, we recommend you perform the following prerequisite checks before you migrate the storage accounts.

Migrate classic virtual machines whose disks are stored in the storage account

$storageAccountName = 'yourStorageAccountName'

Get-AzureDisk | where-Object {$_.MediaLink.Host.Contains($storageAccountName)} | Select-Object -ExpandProperty AttachedTo -Property DiskName | Format-List -Property RoleName, DiskName

The above command returns RoleName and DiskName properties of all the classic VM disks in the storage account.

RoleName is the name of the virtual machine to which a disk is attached. If this command returns disks, then ensure that virtual machines to which these disks are attached are migrated before migrating the storage account.

  • Delete unattached classic VM disks stored in the storage account
  • Find unattached classic VM disks in the storage account using following command:

    $storageAccountName = 'yourStorageAccountName'

    Get-AzureDisk | where-Object {$_.MediaLink.Host.Contains($storageAccountName)} | Where-Object -Property AttachedTo -EQ $null | Format-List -Property DiskName

  • If above command returns disks, then delete these disks using the following command:

    Remove-AzureDisk -DiskName 'yourDiskName'

  • Delete VM images stored in the storage account
  • The following command returns all the VM images with OS disk stored in the storage account.

    Get-AzureVmImage | Where-Object { $_.OSDiskConfiguration.MediaLink -ne $null -and $_.OSDiskConfiguration.MediaLink.Host.Contains($storageAccountName)` } | Select-Object -Property ImageName, ImageLabel

  • The following command returns all the VM images with data disks stored in the storage account.
    Get-AzureVmImage | Where-Object {$_.DataDiskConfigurations -ne $null -and ($_.DataDiskConfigurations | Where-Object {$_.MediaLink -ne $null -and $_.MediaLink.Host.Contains($storageAccountName)}).Count -gt 0 } | Select-Object -Property ImageName, ImageLabel

  • Delete all the VM images returned by above commands using this command:

    Remove-AzureVMImage -ImageName 'yourImageName'

  • Validate Storage Account and Start Migration
  • Validate each storage account for migration by using the following command. In this example, the storage account name is myStorageAccount. Replace the example name with the name of your own storage account.

    $storageAccountName = "myStorageAccount"

    Move-AzureStorageAccount -Validate -StorageAccountName $storageAccountName

  • Next step is to prepare the storage account for migration:

    $storageAccountName = "myStorageAccount"
    Move-AzureStorageAccount -Prepare -StorageAccountName $storageAccountName

  • Check the configuration for the prepared storage account by using either Azure PowerShell or the Azure portal. If you are not ready for migration and you want to go back to the old state, use the following command:

    Move-AzureStorageAccount -Abort -StorageAccountName $storageAccountName

  • If the prepared configuration looks good, you can move forward and commit the resources by using the following command:

    Move-AzureStorageAccount -Commit -StorageAccountName $storageAccountName
By now you would have successfully migrated from Azure classic to Azure Resource Manager. Hope this blog was helpful. Leave your thoughts in comments.

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.