Set your PowerShell session to automatically log into Azure

[Edit 18 July 2017] - A recent update in the PowerShell command line tools for Azure has broken my previous scripts.  I've now updated the post with the updated commands.  Note you should update your version of the Azure Command Line Tools before configuring this on your own machine.

 

I’ve been working in Azure for a while and find it quite frustrating that every time I launched up PowerShell ISE I needed to first log into Azure to get things started.  I knew back in the classic days you could download your Azure profile and load that into PowerShell to save you having to log in ever time.  I’ve never tried this with the new Azure Resource Manager though.

This post will show you the simple process of downloading your Azure profile and configuring PowerShell to automatically load it on start up.

Open up PowerShell ISE and execute the following commands:

[powershell]

Login-AzureRmAccount

Save-AzureRmContext -Path “c:\AzureProfile\azureprofile.json”

[/powershell]

This will download your Azure login details into a JSON file.

https://msdn.microsoft.com/en-us/library/mt619249.aspx

 

The next step is to configure your PowerShell profile to automatically load this up when it starts.

In the PowerShell ISE run:

[powershell]

notepad $profile

[/powershell]

This will load up your PowerShell profile in notepad (if you've never done this before it will probably be empty).  Now enter the scripts to load your Azure profile details.

[powershell]

Import-AzureRmContext -Path “c:\AzureProfile\azureprofile.json” | Out-Null
Write-Host "Successfully logged in using saved profile file" -ForegroundColor Green
       

Get-AzureRmSubscription –SubscriptionName "your subscription name" | Select-AzureRmSubscription  | Out-Null
Write-Host "Set Azure Subscription for session complete"  -ForegroundColor Green

[/powershell]

Save and close the notepad file.

Now, whenever you load up the PowerShell ISE you will automatically be logged into Azure!  Awesome! Smile

 

image

Comments

  • Anonymous
    November 22, 2016
    thanks ! works well
  • Anonymous
    November 23, 2016
    Great tip, thanks! Found this most useful once adding selection menuWrite-Host ""Write-Host "1. Environment Contoso" -ForegroundColor GreenWrite-Host "2. Environment Contoso Development" -ForegroundColor GreenWrite-Host ""Do {Write-Host -NoNewline "Where to login? Type a number from 1 - 2: " -ForegroundColor Magenta; [int]$choice=read-host} while ((1..2) -notcontains $choice) Switch ($choice) { 1 {Select-AzureRmProfile -Path “c:\AzureProfile\azureprofile1.json” | Out-Null; Write-Host "Successfully logged in using saved profile file" -ForegroundColor Green; Get-AzureRmSubscription –SubscriptionName "your subscription name" | Select-AzureRmSubscription; Write-Host "Set Azure Subscription for session complete" -ForegroundColor Green} 2 {Select-AzureRmProfile -Path “c:\AzureProfile\azureprofile2.json” | Out-Null; Write-Host "Successfully logged in using saved profile file" -ForegroundColor Green; Get-AzureRmSubscription –SubscriptionName "your second subscription name" | Select-AzureRmSubscription; Write-Host "Set Azure Subscription for session complete" -ForegroundColor Green} }
    • Anonymous
      November 23, 2016
      That's a nice little addition right there - thanks for sharing!
  • Anonymous
    March 08, 2017
    Awesome!!!One Question - This json permanent or expired after some time?
  • Anonymous
    June 21, 2017
    Great, but the credential saved in JSON expires after some time.And next logon gives a really ugly error (The Subscription ID cannot be found, or something similar).Is there any documentation on this ??