Moving all Azure resources from one resource group to another, across subscriptions

So I had to move a lot of resources from one of my subscriptions to another and thought, how hard can it be. Well I started to look around in the Azure portal and realized I couldn't move between subscriptions at all. So I did some googling and found this article from Microsoft on how to move resources. I can recommend reading it to find the limitations of which resources can and can't be moved.

Unfortunately the article only showed how to move single resources instead of entire subscriptions so I wrote a little PowerShell script to move all resources inside a resource group.

One prerequisite is that the destination resource group must already be created.

$sourceSubId = "11111111-1111-1111-1111-111111111111" # Subscription you want to move resources from
$destSubId = "22222222-2222-2222-2222-222222222222" #     Subscription you want to move resources to

$sourceResourceGroup = "ResourceGroupOld" # Name of the resource group your moving resources from
$destResourceGroup = "ResourceGroupNew" # Name of the resource group your moving resources to

$nonMovableTypes = @('microsoft.insights/components','Microsoft.ClassicStorage/storageAccounts','Microsoft.Compute/virtualMachines/extensions','Microsoft.Sql/servers/databases') # These are the types that cannot be moved
$typeToMoveSeparate = @() # These are the types that must be moved separately

try
{
    Select-AzureRmSubscription -SubscriptionId $sourceSubId

    $groupResources = Find-AzureRmResource -ResourceGroupNameContains $sourceResourceGroup
    $resourceIds = @()
    $moveSeparateResources = @()

    foreach ($r in $groupResources)
    {
        if ($nonMovableTypes.Contains($r.ResourceType)) {
            continue
        }

        if ($typeToMoveSeparate.Contains($r.ResourceType)) {
            $moveSeparateResources = $moveSeparateResources + $r;
        } else {
            $resourceIds = $resourceIds + $r.ResourceId;
        }
    }

    if ($resourceIds.Count.Equals(0) -and $moveSeparateResources.Count.Equals(0))
    {
        "No resources to move"
    }
    else
    {
        if (!$resourceIds.Count.Equals(0)) {
            "Started moving " + $resourceIds.Count + " resources";
            Move-AzureRmResource -DestinationResourceGroupName $destResourceGroup -ResourceId $resourceIds -DestinationSubscriptionId $destSubId -Verbose
        }

        foreach ($r in $moveSeparateResources) {
            "Moving type " + $r.ResourceType + ""
            Move-AzureRmResource -DestinationResourceGroupName $destResourceGroup -ResourceId $r.ResourceId -DestinationSubscriptionId $destSubId -Verbose
        }
 
        "Move done!"
    }
}
catch
{
    Write-Error($_)
}

If you've just created your new subscription you might get an error like this one

MissingRegistrationsForTypes : The subscription 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' is not registered for resource types 'Microsoft.Web/sites.'

To solve it you need to register the resource type this can be done with the following commands

Select-AzureRmSubscription -SubscriptionId "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx" Register-AzureRmResourceProvider -ProviderNamespace Microsoft.Web

Share post
About Johan Boström

I'm a system architect, developer and solution expert, with experience in web-, application-, server- and windows service development with main focus on .NET / C#. Special skills with many kinds of CMS-tools, like EPiServer, Umbraco and Litium Studio.

Comments