The ramblings of an old IT Pro travelling the digital byways.

Tuesday, June 21, 2016

Move Azure Resources

Sometimes it becomes necessary to move an Azure resource from one resource group to another within a subscription.  While this is technically not difficult, it can be a bit of a pain.  Let's let the computers do what they do best.

Following is a quick PowerShell script.  It assumes you've already authenticated to your subscription (Login-AzureRmAccount) and selected the appropriate context (Select-AzureRMSubscription).


<#
.NOTES
===================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.122
Created on:   5/19/2016 3:11 PM
Created by:   TowerBE
Organization: XXXXXXX
Filename:             move-resource.ps1
===================================================================
.SYNOPSIS
Moves an Azure Resource.

.DESCRIPTION
A quick and dirty script to move an Azure resource from one Resource
Group to another

.PARAMETER resName
The name of the Azure Resource to be moved.

.PARAMETER oldRg
The name of the Azure Resource Group where the resource currently exists.

.PARAMETER newRg
The name of the Azure Resource Group where you're moving the resource.

.EXAMPLE
move-resource.ps1 -resName "myservicebus" -oldRg "busted" -newRg "hotness"
This will move the resource myservicebus from the busted resource group
to the hotness resource group.
#>

[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[string]$resName,
[Parameter(Mandatory = $true)]
[string]$oldRg,
[Parameter(Mandatory = $true)]
[string]$newRg
)

$resource = Get-AzureRmResource -ResourceName "$resName" -ResourceGroupName "$oldRg"
Move-AzureRmResource -DestinationResourceGroupName "$newRg" -ResourceId $resource.ResourceId

Please note that not all resources can be moved, and not all resources support moving fully.  I've used this script for moving storage accounts, service bus namespaces and other resources types, but don't make any guarantees. If you want to read more about moving resources, go to https://azure.microsoft.com/en-us/documentation/articles/resource-group-move-resources/.

No comments:

About Me

My photo
A living, breathing contradiction.