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

Showing posts with label Azure. Show all posts
Showing posts with label Azure. Show all posts

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/.

Saturday, June 11, 2016

Persisting Azure RM Credentials: Profile switch in PowerShell

I manage a number of Azure subscriptions for my employer. One of the things that used to drive me nuts is having to constantly go through the Login process, especially since we use Multi Factor Authentication, and then having to switch back and forth between subscriptions.

I recently stumbled across an blog about the Save-AzureRmProfile command. Did I mention I'm relatively new to Azure? While blog provided useful information and simplified things for me, I took it one step further (have to give my buddy Rick credit for sparking the idea).

 I saved a profile file for each one of my subscriptions,

    Save-AzureRmProfile -Path C:\users\Brian\Documents\Azure\Profiles\AzureRmDevProfile.json 

then edited my PowerShell profile to include a function for each profile (hint: if you don't have a PowerShell profile, New-Item –type file –force $profile).

    function goDev { 
        Select-AzureRmProfile -Path C:\users\Brian\Documents\Azure\Profiles\AzureRmDevProfile.json 
    }

Now, if I want to switch to my development subscription while in PowerShell, I simply type "goDev" and I'm there. I have a separate go function for each of the subscriptions I manage.

Saving my Azure profiles and modifying my PowerShell profile: 20 minutes.
Eliminating yet another pesky annoyance: Priceless!

About Me

My photo
A living, breathing contradiction.