Book Image

Azure PowerShell Quick Start Guide

By : Thomas Mitchell
Book Image

Azure PowerShell Quick Start Guide

By: Thomas Mitchell

Overview of this book

As an IT professional, it is important to keep up with cloud technologies and learn to manage those technologies. PowerShell is a critical tool that must be learned in order to effectively and more easily manage many Azure resources. This book is designed to teach you to leverage PowerShell to enable you to perform many day-to-day tasks in Microsoft Azure. Taking you through the basic tasks of installing Azure PowerShell and connecting to Azure, you will learn to properly connect to an Azure tenant with PowerShell. Next, you will dive into tasks such as deploying virtual machines with PowerShell, resizing them, and managing their power states with PowerShell. Then, you will learn how to complete more complex Azure tasks with PowerShell, such as deploying virtual machines from custom images, creating images from existing virtual machines, and creating and managing of data disks. Later, you will learn how to snapshot virtual machines, how to encrypt virtual machines, and how to leverage load balancers to ensure high availability with PowerShell. By the end of this book, you will have developed dozens of PowerShell skills that are invaluable in the deployment and management of Azure virtual machines.
Table of Contents (7 chapters)

Installing Azure PowerShell

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

Installing PowerShellGet

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.

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 (https://www.powershellgallery.com/) installed on your workstation. To check what version of PowerShellGet you have, run the Get-Module command, which is shown as follows:

Get-Module `
-Name PowerShellGet `
-ListAvailable | Select-Object -Property Name,Version,Path

The Get-Module command 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 book, you are going to need PowerShellGet version 1.1.2.0 or later installed. If you don't have PowerShellGet version 1.1.2.0 or later installed, update it by running the Install-Module command, shown as follows:

Install-Module PowerShellGet -Force

The Install-Module command will update your PowerShellGet module to the latest version. Specifying the -Force switch simply suppresses any Are you sure? confirmation type messages. The following screenshot shows the installation of PowerShellGet:

Once the latest version of PowerShellGet is installed, you can install Azure PowerShell.

Installing the 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:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Running the Set-ExecutionPolicy command, 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. As you can see in the following screenshot, PowerShell will prompt you to confirm that you wish to change the execution policy:

Once your execution policy is set to RemoteSigned, you can install the AzureRM PowerShell module by running the Install-Module command:

Install-Module -Name AzureRM -AllowClobber

Running the preceding command 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.

As you can see in the following screenshot, 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 whether 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. As 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 following command:

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 interactively at the command line, using the Connect-AzureRMAccount command. It's a simple command to run:

Connect-AzureRmAccount

After hitting Enter, a dialog box prompts you 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 from PowerShell to confirm that it returns your subscription information:

Get-AzureRmSubscription

If the preceding command returns your subscription info, you can move on to the next steps.