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.