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.
Install the right module
This logic is located in a PowerShell module called AzureRM.Profile. In order to run these scripts you may need to install that module. Use the following line:
If the script is complaining about not recognizing a cmdlet.
There is a powershell command that saves your key locally in a jsonfile. You can use that file to login. Of course, you need to make sure you protect that key. Simply execute these commands and point to a path where you want to save it.
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.
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.
“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.