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”.