Persistant Azure PowerShell Logins

While doing azure automation from some time, I have found that I have to login each time I run a new PowerShell session. This is irritating while writing and executing scripts. You can do this manually by configuring the management certificate and subscription details with the Set-AzureSubscription and Select-AzureSubscription cmdlets or automatically by downloading the PublishSettings file from Windows Azure and importing it. In this blog post, we’ll see how to automate the login process for Microsoft Azure so that our script can run without any manual intervention. 

For Azure Resource Manager Model

First, you need to start a new PowerShell session and login using Login-AzureRmAccount. This will initiate a pop-up box in which you can enter your credentials. Now we need to run below command:

 Save-AzureRmProfile –Path C:\your-path-here\AzureRmProfile.json

This will save the information of your profile. If you cannot find the json file at the path mentioned, it will be created under under C:\Users\your-user-name\ directory. It took me some time to figure that out.

Now, you can modify your PowerShell profile to auto import this json file:

Select-AzureRmProfile –Path C:\your-path-here\AzureRmProfile.json | out-null

So everytime you start a new PowerShell session, it will import the json file for you and you’ll no more require authentication prompts.

For Azure Classic Model

First, we’ll need to authenticate ourselves using Add-AzureAccount cmdlet. After this, run below command:

Get-AzurePublishSettingsFile

This will import the publishsettings file for you. Now, you need to modify your PowerShell profile to auto import this file:

Import-AzurePublishSettingsFile <mysettings>.publishsettings

where you need to replace<mysettings> with the file name of the publishsettings file. So, you’ll need no more authentication prompts while trying to work with classic resources.

2 thoughts on “Persistant Azure PowerShell Logins

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s