0

I am having a list of Resource Groups in Subscription A. e.g:

$list = ("RG1","RG2","RG3","RG4","RG5")

I am picking Resource Group one by one and moving them to Subscription B.

The script is working fine almost all the time but sometimes in any Resource Group if any Resource is not able to move for any reason then the script got failed. As I picked Resource group one by one so if an error occurred in RG2 then the remaining 3 Resource groups (RG3, RG4, RG5) will not be processed.

To handle this I just simply add -ErrorAction SilentlyContinue in my script so that script will continue to run. e.g:

Move-AzResource -ResourceId $Resource.ResourceId -DestinationSubscriptionId $destinationSubscriptionID -DestinationResourceGroupName $RG -Force -ErrorAction SilentlyContinue

Below is the basic structure:

$list = ("RG1","RG2","RG3","RG4","RG5")
try
{
    foreach($RG in $list)
    {
        Move-AzResource -ResourceId $Resource.ResourceId -DestinationSubscriptionId $destinationSubscriptionID -DestinationResourceGroupName $RG -Force -ErrorAction SilentlyContinue
    }
}
catch
{
    Write-Output "Catch Block !!!"
    $_.Exception.Message
}

Error Message:

Catch Block !!!
ResourceMoveFailed : Resources '/subscriptions/b9aafef7-2451-4170-bdab-322f757f545d/resourceGroups/syncintegration/providers/Microsoft.Insights/components/syncintegration' move actions failed. The correlation Id is 'e3f823c0-a2ca-4c27-a005-13d189614a0c'
CorrelationId: c3d224fc-a1b3-4e32-840b-6c980c4af90f

Expected: If any error comes then it should ignore the error pick the next resource group. It would be good if it logs the error and then continues to pick other resource groups.

rAJ
  • 1,295
  • 5
  • 31
  • 66
  • 1
    To put it in PowerShell error-handling terms: the `-ErrorAction` parameter only affects _non-terminating_ errors, whereas `Move-AzResource` apparently emits - seemingly unexpectedly - a (statement-) _terminating_ error (as evidenced by the fact that it can be caught with `try / catch`). See [this answer](https://stackoverflow.com/a/60549569/45375) for more information. – mklement0 Sep 24 '21 at 15:53

1 Answers1

3

This is still a known issue with some other azure-powershell commands. If Move-AzResource has the same issue, you may be able to work around it by adding -Verbose as well:

https://github.com/Azure/azure-powershell/issues/11384

If not, then make sure you have the module updated. You might want to try raising a new issue on the link above.

Another example workaround is to catch the error within the Foreach loop to continue after an error:

foreach($RG in $list) {
  Try { Move-AzResource -ResourceId $Resource.ResourceId -DestinationSubscriptionId $destinationSubscriptionID -DestinationResourceGroupName $RG -Force
  Catch { $_.Exception.Message; Continue }
}
Cpt.Whale
  • 4,784
  • 1
  • 10
  • 16