PowerShell working with Azure Resource Manager RM – Step By Step Changing RM Subscriptions


There are two different types resource modes in Azure.  One is the Service Manager (classic) and the other is the new Resource Manager.   Resource Manager is the new model and much, much better for a bunch of reasons.  Those reasons are beyond the scope of this post but the way to connect to the Resource Manager is different in PowerShell that the Service Manager.  For that reason, I wanted to outline how to connect to Azure Resource Manager in Powershell.

 

First, in order to connect to Azure you have to have the latest version of the PowerShell Azure client which can be downloaded from the Azure download page https://azure.microsoft.com/en-us/downloads/.  Scroll down to Command-Line tools and under PowerShell  click Windows install.  I direct link is https://go.microsoft.com/?linkid=9828653&clcid=0x409  Login to your Azure account. After providing your credentials, you will be given the option to install components.  Follow the prompts.  You may require a restart when finished.

  1. Add-AzureRmAccount
    A summary of your account is returned.

    Environment : AzureCloud
    Account : someone@example.com

    The Add-AzureRMAccount adds cached credentials to your Pwwershell session.  If you have already connected once, you can useLogin-AzureRmAccount
  2. If you have multiple subscriptions, provide the subscription id you wish to use for deployment with the Set-AzureRmContext command To determine what subscriptions you have, you can use Get-AzureRmSubscription.
    Get-AzureRmSubscriptionReturns list of available subscriptions
    SubscriptionName : Internal Consumption
    SubscriptionId : 1942a221-…
    TenantId : 72f988bf-…
    State : EnabledSubscriptionName : Microsoft Azure Internal Consumption
    SubscriptionId : 357e6482-…
    TenantId : 72f988bf-…
    State : Enabled
  3. You could make it a bit easier to read with a format command
    Get-AzureRmSubscription | Format-table SubscriptionName,isDefault,isCurrent,DefaultAccount,subscriptionid
  4. To Select a subscription, use Set-AzureRmContext command.Set-AzureRmContext -SubscriptionID <YourSubscriptionId>
  5. If you do not have an existing resource group, create a new resource group with the New-AzureRmResourceGroup command. Provide the name of the resource group and location that you need for your solutionNew-AzureRmResourceGroup -Name ExampleResourceGroup -Location “West US”A summary of the new resource group is returned.ResourceGroupName : ExampleResourceGroup
    Location : westus
    ProvisioningState : Succeeded
    Tags :
    Permissions :
    Actions NotActions
    ======= ==========
    *
    ResourceId : /subscriptions/######/resourceGroups/ExampleResourceGroup
  6. Notice: now when you run commands use the resource manager version of the commands. As an example instead of using Get-AzureVM you would use Get-AzureRmVM