Azure / Azure/azure-powershell
Move-ResourceGroup cmdlet
- Dominant language
- C#
- Stars
- 4.8k
- Forks
- 4.3k
- Avg merge
- 3d 14h
- Merged PRs (30d)
- 54
Description
## Description of the new feature
```
Move-AzResourceGroup
[-Name ]
[-DestinationSubscriptionId ]
[-DestinationName ]
[-WhatIf]
```
The portal can do this already, but everything the portal can do powershell should be able to do better right? ;)
Reference: [Move resources to a new resource group or subscription](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-move-resources).
## Proposed implementation details (optional)
1. Check all resources in RG can are movable^.
2. Register any missing resource providers
3. [Maybe] Create destination RG. Use the same location as the source.
4. Execute move
5. [Maybe] Remove source RG
^ This information doesn't currently seem programmatically available. Maybe it's possible by repeatedly calling [validate move](https://docs.microsoft.com/en-us/rest/api/resources/resources/validatemoveresources), but it's hard to distinguish between resources that genuinely aren't movable and those that are just associated with some other resource that is.
Dumb script that does 2-4:
```
function Move-AzResourceGroup {
param ($Name, $DestinationSubscriptionId, $DestinationName)
if (-not $DestinationName) { $DestinationName = $Name}
$fromRg = Get-AzResourceGroup -Name $Name
# HACK: should check if all resources are movable
$fromRs = Get-AzResource -ResourceGroupName $fromRg.ResourceGroupName
# this check won't filter out some unmovable alert resources, for example
$fromRsTopLevel = $fromRs | Where { -not $_.ParentResource }
# see #10271
$fromRpNames = $fromRs | % { $_.ResourceType.Split('/')[0] } | select -Unique
$fromSub = (Get-AzContext).Subscription
$toSub = Get-AzSubscription -SubscriptionId $DestinationSubscriptionId
Set-AzContext -SubscriptionObject $toSub
$fromRpNames | % { Register-AzResourceProvider -ProviderNamespace $_ -Verbose }
$toRg = New-AzResourceGroup -Name $DestinationName -Verbose -Location $fromRg.Location
Set-AzContext -SubscriptionObject $fromSub
Move-AzResource -DestinationResourceGroupName $DestinationName -DestinationSubscriptionId $toSub.SubscriptionId -ResourceId $fromRsTopLevel.ResourceId
}
```
Contributor guide
Assessment
This issue has not been assessed yet.