I am looking to make an Azure DevOps Pipeline to move resources at the resource group level, to another subscription with an empty resource group. However, I’m running into an issue with Move-AzResource
where it can’t move all resources since some resources like storage accounts and VMs have a dependency on other resources. I know that the easy way to this is to just use the Azure Portal, but I was wondering if there was a way to programmable move resources instead. Any advice would help!
PowerShell Script:
# Connect to your Azure account
Connect-AzAccount -Tenant 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX' -Subscription 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
# Define resource group names
$sourceRG = "move-this"
$targetRG = "move-to-this"
# Specify the target subscription ID
$destinationSubscription = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" # Replace with your actual target subscription ID
# Get resources from the source resource group
$resources = Get-AzResource -ResourceGroupName $sourceRG
# Move each resource to the target resource group
foreach ($resource in $resources) {
Move-AzResource -DestinationResourceGroupName $targetRG -DestinationSubscriptionId $destinationSubscription -ResourceId $resource.ResourceId -Force
}
Error:
Line |
9 | Move-AzResource -DestinationResourceGroupName $targetRG -Destinat …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| ResourceMoveValidationFailed : The resource batch move request has '1' validation errors. Diagnostic information: timestamp '20240511T202422Z', tracking
| Id '791c0f0a-aa19-4929-86b0-fab99adb6ec9', request correlation Id '4502f145-7358-4076-b6ac-33987628efcf'. CorrelationId:
| 6693eb66-f052-48da-bbb1-ab6676610a26
Tried to use Move-AzResource
to move all of the resources in a resource group to a different resource group in another subscription. However, I’m getting validation errors due to the nature of the resources in that resource group.
2
Answers
I found that just moving each individual resource rather than the entire resource group was the answer. Using this script, I can list the resources I want to move in a parameter and move each resource individually through a for loop.
move.ps1:
move-reosurces.yml:
This script will do the job.
I tested to use the same script as yours to move a simple Microsoft.Storage (without any dependent resources) across resource groups in different subscriptions and it succeeded. So what seemed to block you at the moment was how to know and move the dependent resources.
According to the document,
By the way, you may also test with the script/command to validate move scenario via Azure PowerShell to see why the validation didn’t pass.