Intro

Azure PowerShell is a set of modules that provide command line interface to Azure. You can accomplish almost everything using Azure PowerShell. It's obvious now that Microsoft investing lots of effort into command line tools for its products and it's great! There are two command line interfaces implementation - PowerShell and Azure CLI. Personally I don't use Azure CLI very often because I primary work on windows systems, but people who work with *nix systems are first-class citizens now in Microsoft world, so they have their own tool. The funny thing is that Microsoft released PowerShell for Linux. So developers can use both Azure PowerShell and Azure CLI regardless operation system they use.

Code

First of all install PowerShell module for Azure Resource Manager and Azure Service Manager, run powershell as Administrator and then execute following line:

PS C:\WINDOWS\system32> Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
PS C:\WINDOWS\system32> Install-Module AzureRM
PS C:\WINDOWS\system32> Install-Module Azure

It will add PSGaller as a trusted repository, otherwise you will be getting:

Untrusted repository You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?

all the time.

To get all commands related to azure profiles run the following command:

PS C:\WINDOWS\system32> Get-Command -Module AzureRM.Profile

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           Login-AzureRmAccount                               2.1.0      AzureRM.profile
Alias           Select-AzureRmSubscription                         2.1.0      AzureRM.profile
Cmdlet          Add-AzureRmAccount                                 2.1.0      AzureRM.profile
Cmdlet          Add-AzureRmEnvironment                             2.1.0      AzureRM.profile
Cmdlet          Disable-AzureRmDataCollection                      2.1.0      AzureRM.profile
Cmdlet          Enable-AzureRmDataCollection                       2.1.0      AzureRM.profile
Cmdlet          Get-AzureRmContext                                 2.1.0      AzureRM.profile
Cmdlet          Get-AzureRmEnvironment                             2.1.0      AzureRM.profile
Cmdlet          Get-AzureRmSubscription                            2.1.0      AzureRM.profile
Cmdlet          Get-AzureRmTenant                                  2.1.0      AzureRM.profile
Cmdlet          Remove-AzureRmEnvironment                          2.1.0      AzureRM.profile
Cmdlet          Save-AzureRmProfile                                2.1.0      AzureRM.profile
Cmdlet          Select-AzureRmProfile                              2.1.0      AzureRM.profile
Cmdlet          Set-AzureRmContext                                 2.1.0      AzureRM.profile
Cmdlet          Set-AzureRmEnvironment                             2.1.0      AzureRM.profile

Now login to the azure account using PowerShell, an interactive login window will be shown:

PS C:\WINDOWS\system32> Login-AzureRmAccount

There is more info about this command on MSDN, this GitHub Issue can also be useful. Also, more information about different accounts used by Microsoft can be found here

This command will set a context for current powershell session.

The Get-AzureRmSubscription cmdlet gets the subscription ID, subscription name, and home tenant for Azure subscriptions that the current account can access:

PS C:\WINDOWS\system32> Get-AzureRmSubscription

SubscriptionName : Visual Studio Enterprise
SubscriptionId   : <GUID>
TenantId         : <GUID>
State            : Enabled

To get lots of information about different endpoints and metadata of Azure services:

PS C:\WINDOWS\system32> Get-AzureRmEnvironment

Name                                              : AzureCloud
EnableAdfsAuthentication                          : False
ActiveDirectoryServiceEndpointResourceId          : https://management.core.windows.net/
AdTenant                                          :
GalleryUrl                                        : https://gallery.azure.com/
ManagementPortalUrl                               : http://go.microsoft.com/fwlink/?LinkId=254433
ServiceManagementUrl                              : https://management.core.windows.net/
PublishSettingsFileUrl                            : http://go.microsoft.com/fwlink/?LinkID=301775
ResourceManagerUrl                                : https://management.azure.com/
SqlDatabaseDnsSuffix                              : .database.windows.net
StorageEndpointSuffix                             : core.windows.net
ActiveDirectoryAuthority                          : https://login.microsoftonline.com/
GraphUrl                                          : https://graph.windows.net/
GraphEndpointResourceId                           : https://graph.windows.net/
TrafficManagerDnsSuffix                           : trafficmanager.net
AzureKeyVaultDnsSuffix                            : vault.azure.net
AzureDataLakeStoreFileSystemEndpointSuffix        : azuredatalakestore.net
AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix : azuredatalakeanalytics.net
AzureKeyVaultServiceEndpointResourceId            : https://vault.azure.net

This command show all available Azure instances, such as AzureUSGovernment, AzureChinaCloud, AzureGermanCloud. If Microsoft decides to change some endpoints or add/remove an instance of Azure, it will be reflected in the output of this command. This information is used by Resource Manager cmdlets, I belive this info can also be used in scripts.

It can be modified with Add-AzureRmEnvironment & Set-AzureRmEnvironment cmdlet.

To get a current user's tenant the following command has to be used:

PS C:\WINDOWS\system32> Get-AzureRmTenant

TenantId : <GUID>
Domain   : outlook.com

To get info about current session context the following command can be used:

PS C:\WINDOWS\system32> Get-AzureRmContext

Environment           : AzureCloud
Account               : oleksii.udovychenko@outlook.com
TenantId              : 11747c24-923f-4f49-98e1-34ae9c31817f
SubscriptionId        : 8a2fdad7-75c8-46c4-a740-c547dc545543
SubscriptionName      : Visual Studio Enterprise
CurrentStorageAccount :

this information is used in requests to the Resource Manager

If CurrentStorageAccount property is empty, some commands will not work. To set CurrentStorageAccount it is required to create a resource group and a storage account inside. It could be done using the following command:

PS C:\WINDOWS\system32> New-AzureRmResourceGroup -Name Default -Location "Australia East"

ResourceGroupName : Default
Location          : australiaeast
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/<GUID>/resourceGroups/Default

The resource group has been created in "Australia East" region. More information regarding New-AzureRmResourceGroup can be found on MSDN

Create a storage account inside the resource group:

PS C:\WINDOWS\system32> New-AzureRmStorageAccount -ResourceGroupName Default -AccountName "default748347" -Location "Australia East" -Type "Standard_GRS"

ResourceGroupName   : default
StorageAccountName  : default748347
Id                  : /subscriptions/<GUID>/resourceGroups/default/providers/Microsoft.St
                      orage/storageAccounts/default748347
Location            : australiaeast
Sku                 : Microsoft.Azure.Management.Storage.Models.Sku
Kind                : Storage
Encryption          :
AccessTier          :
CreationTime        : 9/10/2016 12:34:34 PM
CustomDomain        :
LastGeoFailoverTime :
PrimaryEndpoints    : Microsoft.Azure.Management.Storage.Models.Endpoints
PrimaryLocation     : australiaeast
ProvisioningState   : Succeeded
SecondaryEndpoints  :
SecondaryLocation   : australiasoutheast
StatusOfPrimary     : Available
StatusOfSecondary   : Available
Tags                : {}
Context             : Microsoft.WindowsAzure.Commands.Common.Storage.LazyAzureStorageContext

Now set this account as default:

PS C:\WINDOWS\system32> Set-AzureRmCurrentStorageAccount -ResourceGroupName Default -AccountName default748347
default748347
PS C:\WINDOWS\system32> Get-AzureRmContext

Environment           : AzureCloud
Account               : oleksii.udovychenko@outlook.com
TenantId              : 11747c24-923f-4f49-98e1-34ae9c31817f
SubscriptionId        : 8a2fdad7-75c8-46c4-a740-c547dc545543
SubscriptionName      : Visual Studio Enterprise
CurrentStorageAccount : default748347

Run the following command to save the current azure profile. In this way we won't need to authenticate next time:

PS C:\WINDOWS\system32> Save-AzureRmProfile -Path C:\Profile.json

Next time load azure profile using the following command:

PS C:\WINDOWS\system32> Select-AzureRmProfile -Path C:\Profile.json

Summary

Microsoft done a great job working on Azure and Azure PowerShell. It takes only few hours to get into Azure and start developing something, but even after few years of working with Azure I can't say that I trully know it. It has lots of features and Microsoft adds something new and exciting literally every month.


;