PowerShell is the best way to manage Azure, because scripting allows you to automate many tasks. For instance, you can push virtual machines to the cloud and turn them on, turn them off, or even schedule them to be turned off (and back on during business hours) to save money.
With the Azure cmdlets for PowerShell, you can do all those things within the Azure Service Manager platform. However, Azure is moving toward the Azure Resource Management (ARM) platform, and a new set of tools and methods is available for working with the fabric. Not all the tools and resources are over there yet, but more are coming all the time. For more info, comparing the old and new tools, please visit this page. In this post, we focus on the new ARM platform, and I assume that you have the AzureRM cmdlets installed.
Log in to Azure with Login-AzureRmAccount ^
To use any of the cmdlets in the AzureRM module, you need to log in. This requires that you have an Azure subscription. Of course, the main reason you need to sign in isn’t because Microsoft just wants to know who uses its cloud, but you are also supposed to pay for the resources you use.
To see whether you are logged in or not, you can run the Get-AzureRmResourceGroup cmdlet, which allows you see the number of resource groups with their names in your subscription. If you didn’t sign in, you will receive the error message Run Login-AzureRmAccount to login.
When I run Login-AzureRmAccount, I see a dialog like the one below, asking for credentials:
This will log me in to the specified Azure account and display information about my subscription as shown below:
Now that I’m logged in, I can use Get-AzureRmresourceGroup to view my Azure resources with PowerShell:
Get-AzureRmresourceGroup | Select ResourceGroupName
Saving credentials with Save-AzureRmProfile ^
A downside of the method described above is that you always have to enter your credentials before you start working with Azure. A more convenient way is to store your credentials in a file:
Save-AzureRmProfile -Path “c:\folder\azureprofile.json”
This will create the file azureprofile.json, which contains all the login information for your Azure account.
Important Note: This file is a plain-text JSON file. If an unauthorized person gains access to this file, it would compromise your Azure account, and this person could use Azure resources on your costs. Thus, you should treat the file like cash.
Obviously, we have a clear case where you have to weigh security against convenience. However, the advantage of storing your credentials in a JSON file is not just that you can quickly sign in; it also allows you to authenticate easily from within your PowerShell scripts.
Logging in with Select-AzureRmProfile ^
Logging in with the profile is as easy as calling the Select-AzureRmProfile cmdlet, passing the JSON file you saved previously:
Select-AzureRmProfile -Path “c:\folder\azureprofile.json”
After you log in, Azure will display some information about your subscription:
One thing to note: You can work with only one profile at a time, because it is tied to the subscription. If you want to work with multiple subscriptions simultaneously, you will need to build and select another profile.
For each subscription you work with, follow these steps:
- Log in to the Azure subscription with Login-AzureRmAccount.
- Save a profile for that subscription as a JSON file with Save-AzureRmProfile -path “c:\folder\contoso-profile.json”.
- Import the profile for the subscription you are accessing with Select-AzureRmProfile -path “c:\folder\contoso-profile.json”.
Once you create and store the profiles, logging in is as easy as importing the JSON file for the subscription you need.
To keep customer data segregated from other customers (because subscriptions are separated by tenants), only one subscription may be active at a time in a single session; however, you can log in to multiple subscriptions by using multiple PowerShell sessions.
That’s all there is to it. Now you can use all the PowerShell cmdlets in the AzureRM module to work with your subscription(s).