Performing management tasks within an Azure tenant, using PowerShell, requires a handful of prerequisites to be met first. For example, the AzureRM PowerShell module needs to be installed so that Azure PowerShell commands become available. However, since the preferred method of installing the AzureRM module is to do so via the PowerShell Gallery, the latest version of PowerShellGet needs to be installed first.
Once the latest version of PowerShellGet and AzureRM are installed, the AzureRM module can be loaded and used to connect to the Azure tenant. The next few exercises will walk you through the installation of PowerShellGet, how to use it to download and install AzureRM, and then how to use AzureRM to connect to an Azure tenant.
Before using PowerShell to deploy and manage virtual machines in Azure, you will need to install the Azure PowerShell module (AzureRM) on your workstation. The best way to install Azure PowerShell is to do it from the PowerShell Gallery, which is what you will learn to do in this section, starting with the installation of PowerShellGet.
EDITOR'S NOTE: This tutorial was excerpted from the complete online video course, "Creating and Managing Azure Virtual Machines with PowerShell", which can be found on the Udemy training platform.
The installation of PowerShellGet is necessary because PowerShellGet is the tool that is used to pull down PowerShell modules from the gallery – so, before doing anything, you need to ensure that you have the latest version of the PowerShellGet module installed on your workstation. To check what version of PowerShellGet you have, you need to run the Get-Module command, which is shown below:
Get-Module -Name PowerShellGet -ListAvailable | Select-Object -Property Name,Version,Path
The Get-Module command above will list what versions of PowerShellGet are currently available on your workstation. By piping the results of the Get-Module command (using the “|” character) to the Select-Object command, you can view just the info you are looking for. In this case, you can see the Name, Version, and Path of each instance of PowerShellGet that currently resides on your machine.
To perform the exercises in this tutorial, you are going to need PowerShellGet version 184.108.40.206 or later installed. If you don’t have PowerShellGet version 220.127.116.11 or later installed, update by running the Install-Module command below:
Install-Module PowerShellGet -Force
What the Install-Module command, above, will do, is update your PowerShellGet module to the latest version. Specifying the -Force switch simply suppresses any “are you sure” confirmation-type messages.
Once the latest version of PowerShellGet is installed, you can install Azure PowerShell.
Installing Azure PowerShell Module
Before using PowerShellGet to download and install Azure PowerShell, you must first configure your PowerShell environment with an execution policy that allows scripts to run. Since the default execution policy for PowerShell is “Restricted”, you must change it by running the Set-ExecutionPolicy command (shown below).
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Running the Set-ExecutionPolicy command, above, to set the execution policy to “RemoteSigned”, is sufficient for installing Azure PowerShell from the PowerShell Gallery. You could also set your policy to “Unrestricted” but that would further open you up to unsigned commands that could be malicious.
Once your execution policy is set to “RemoteSigned”, you can install the AzureRM PowerShell module by running the Install-Module command as shown below:
Install-Module -Name AzureRM -AllowClobber
Running the command above launches the process of connecting to the PowerShell Gallery and downloading the Azure PowerShell module. The AllowClobber switch ensures that you get the full and complete installation of AzureRM.
The PowerShell Gallery is not configured as a Trusted repository for PowerShellGet (you’d think it would be trusted by default). As such, you are likely to see a warning about the repository not being trusted and you will be prompted if you are sure you want to download the module. It’s safe to answer “Yes” or “Yes to all” when you see this prompt.
Selecting Yes” allows the installation of Azure PowerShell to continue.
The installation of Azure PowerShell is generally painless. The AzureRM module that gets installed is a rollup module for the Azure Resource Manager cmdlets. Installing the AzureRM module also pulls down, from the PowerShell Gallery, any Azure PowerShell module not previously installed.
Loading the AzureRM Module
Once the Azure PowerShell module (AzureRM) has been installed, you can load the module into your PowerShell session, using the Import-Module command. Per Microsoft’s recommendations, you should do this in a non-elevated PowerShell session so if you haven’t done so already, open a new “non-elevated” PowerShell session and from within this session, run the command shown below:
Import-Module -Name AzureRM
Importing the Azure PowerShell module is rather uneventful. However, rest assured, that, unless an error message is displayed, you now have Azure PowerShell loaded – and now that you have Azure PowerShell installed and loaded, you can now actually connect to Azure using PowerShell.
Connecting to Azure via PowerShell
Although Azure PowerShell supports multiple login methods, the easiest way to get logged in is to do so interactively at the command line, using the Connect-AzureRMAccount command. It’s a simple command to run (as shown below):
After hitting enter, you are prompted with a dialog box asking for your Azure admin credentials. Go ahead and supply your Azure admin credentials. Once you’ve supplied those credentials, you will be connected to your tenant.
To confirm that you are connected to your Azure tenant, you can run the Get-AzureRmSubscription command (shown below) from PowerShell to confirm that it returns your subscription information.
If the command above returns your subscription info, you have successfully installed the AzureRM PowerShell module.
More Learning Resources
If you’d like to learn more about topics like this one, visit me at my website or at my training site. You can also find me on LinkedIn and on Facebook. If you are after free video tutorials, visit my YouTube channel. Some of my courses, like this one, can also be found on Udemy.com, where you can use coupon code THOMASMITCHELL at checkout to get any one of my complete online courses for just $12.99.