Use of a saved Azure login in PowerShell

Introduction

Using powershell to make things happen in Azure is awesome. Personally, I put together small little snippets based on one or two simple actions, like “start the development VM” or “disable the polling Logic App”. There is one downside to this though and that is the constant barrage of login windows. You might not succeed with your new script the first time and after updating it, you have to login again … this makes Mikael a dull boy.

You can work around this by saving the credentials to disc, using powershell. Here is how you do it.

Save-AzureRmProfile

There is a powershell comman that saves your key locally in a jsonfile. You can the use that file to login. Of course, you need to make sure you protect that key.

Simply execute the command and point to a path where you want to save it.

Save-AzureRmProfile
-Path
“c:\MySecretFolder\azurecredentials.json”

This creates a json-file that contains all the information you need to login to your Azure subscription. Take a look after you saved it to see that it contains a veeeery long key.

Select-AzureRmProfile

Now it is time to use the saved credential and that is very easy as well.

There is a script that makes use of a saved credential and starts a virtual machine.

Select-AzureRmProfile
-Path
“C:\MySecretFolder\azurecredentials.json “

Select-AzureRmSubscription
-SubscriptionId
“<subscription GUID>”

 

Start-AzureRmVM
-Name
“Devmachine” -ResourceGroupName “Devmachine”

Looking thru the script, the first line does the actual logging in. Then a subscription is selected, if you only have one subscription you can skip this step. Then the machine is started.

The magic is really in the first two rows, and these are the two rows I reuse in all my short action-focused scripts.