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.
[…] one of the previous blog post we discussed how to create persistant Azure PowerShell logins. There we described commands to do […]
LikeLike
[…] discussed in one of the previous blog posts, we can use PowerShell to help create persistent logins. Now consider scenario, where you have […]
LikeLike