Introduction to PowerShell with Windows Azure

by [Published on 17 April 2014 / Last Updated on 17 April 2014]

In this month’s article I’ll provide a high level overview of PowerShell management for Windows Azure Infrastructure-as-a-Service.

Introduction

PowerShell for Windows was first released back in November of 2006, and has since evolved in to a powerful command-line configuration environment that administrators are becoming increasingly reliant upon to perform their systems management tasks. With each release of the Microsoft Windows operating system, more and more capabilities are being added and more management tasks are being exposed to PowerShell. Today, using PowerShell, it is possible to accomplish nearly everything from the command line that is possible in the Graphical User Interface (GUI). In fact, many tasks can only be performed using PowerShell, and some tasks can be managed more granularly using PowerShell.

PowerShell for Windows Azure

Windows Azure Infrastructure-as-a-Service allows for the configuration and deployment of virtual machines hosted in Microsoft datacenters. Deploying workloads to the public clouds has numerous benefits in terms of scale, performance, and operating expense. To provide the same support for remote command-line management, PowerShell is also available to configure and manage many aspects of Windows Azure, including virtual machine and storage provisioning, and network configuration.

Installing PowerShell of Windows Azure

PowerShell for Windows Azure can be downloaded from GitHub here. There are two ways to download PowerShell; the Web Platform Installer and the Standalone version. The Web Platform Installer will give you PowerShell along with additional features such as the Windows Azure SDK, Windows Azure Authoring Tools, Windows Azure Emulator, IIS 8.0 Express, Windows Azure Libraries for .NET, and Windows Azure Storage Tools. If you’re a developer, this is the installation method you’ll likely choose. If you’re a systems administrator, you’ll likely want to avoid all of that extraneous software and simply download the Standalone PowerShell tools for Windows. For the purposes of this article I’ve chosen to download and install the Standalone version.

Image
Figure 1

Create a Windows Azure Virtual Machine using PowerShell

Once PowerShell for Windows Azure is installed, the first thing that must be done is to associate PowerShell with the Windows Azure account and subscription to be managed. Begin by opening an elevated PowerShell session and adding your Windows Azure account using the following command:

Add-AzureAccount

You’ll be prompted to enter the e-mail address of the account you wish to sign in with. After you’ve authenticated successfully, download the Azure Publish Settings file using the following PowerShell command:

Get-AzurePublishSettingsFile

Next, import the Azure Publish Settings file you just downloaded using the following PowerShell command:

Import-AzurePublishSettingsFile <path_to_file>

Typically there are multiple subscriptions assigned to an account, so use the following PowerShell command to list all associated subscriptions:

Get-AzureSubscription | Select SubscriptionName

Image
Figure 2

Choose a subscription and use the following PowerShell to select it:

Select-AzureSubscription –SubscriptionName "<subscription_name>"

If you do not already have a Windows Azure storage account, you’ll need to create one before proceeding. To display a list of locations, execute the following PowerShell command:

Get-AzureLocation | Select DisplayName

Image
Figure 3

Once you’ve decided on a location to create the storage account, execute the following PowerShell command:

New-AzureStorageAccount -StorageAccountName "<name>" -Label "<name>" -Location "<region>"

Note:
The account name and label can only include lowercase numbers and letters.

Image
Figure 4

Now associate the new storage account with the current subscription using the following PowerShell command:

Set-AzureSubscription -SubscriptionName "<subscription>" -CurrentStorageAccountName "<storage_account>"

Before deploying a virtual machine we must first choose a region to deploy the VM. To display a list of available regions, execute the following PowerShell command:

Get-AzureLocation | Select DisplayName

Image
Figure 5

Now let’s look at the list of available virtual machine images available to us. Use the following PowerShell command:

Get-AzureVMImage | Select ImageName

Image
Figure 6

This will return a long list of virtual machine images to choose from. Once you’ve decided on a location and image, you can create the VM. Here I’ve chose to create a VM using a Windows Server 2012 R2 Standard Edition image. To create the VM, execute the following PowerShell command:

New-AzureQuickVM -Windows -ServiceName "<service_name>" -Name "<vm_hostname>" -ImageName "<image_name>" -Location "<location>" -InstanceSize "<size>"  -AdminUsername "<admin_user>" -Password "<password>"

Note:
If the -InstanceSize parameter is omitted, a Small size VM will be created. For a list of all VM instance sizes, click here.

Image
Figure 7

Once complete, you can view the status of the virtual machine using the following PowerShell command:

Get-AzureVM

Image
Figure 8

With the virtual machine provisioned and running, you can even use PowerShell to retrieve the Remote Desktop configuration file and launch an RDP session to the virtual machine.

Get-AzureRemoteDesktopFile -ServiceName "<service_name>" -Name "<vm_hostname>" -LocalPath "C:\temp\app1.rdp"

Image
Figure 9

Now run the Remote Desktop client using the following command:

mstsc.exe <path_to_rdp_file>

Delete a Windows Azure Virtual Machine using PowerShell

Using PowerShell we can also stop and delete the virtual machine and all related disks and services. We’ll begin by stopping the VM we just created using the following PowerShell command:

Stop-AzureVM -ServiceName "<service_name> " -Name "<vm_hostname>"

Image
Figure 10

Now remove the VM by using the following PowerShell command:

Stop-AzureVM -ServiceName "<service_name> " -Name "<vm_hostname>"

Image
Figure 11

Next, remote and delete the virtual disks associated with the VM. To find the name of the virtual disk, use the following PowerShell command:

Get-AzureDisk | Select DiskName

Image
Figure 12

Once you have the name of the virtual disk you can delete it using the following PowerShell command:

Remove-AzureDisk -DiskName <diskname> -DeleteVHD

Image
Figure 13

Finally, delete the service using the following PowerShell command:

Image
Figure 14

Summary

PowerShell is a powerful platform that can be leveraged by systems administrators for configuration and automation, not only for on-premises Windows workloads, but for the virtual machine infrastructure hosted in Windows Azure. You can quickly and easily deploy virtual machines in the cloud, and manage them using the same familiar PowerShell environment you use today. Although this article focused solely on deploying a virtual machine to Windows Azure Infrastructure-as-a-Service, PowerShell is by no means limited only to that service. You can also use PowerShell to manage applications and web sites hosted by Windows Azure Platform-as-a-Service. There are 279 PowerShell cmdlets in the latest release of PowerShell for Windows Azure, and that is certain to continue to grow over time. Here we used the most simple method to deploy a virtual machine, but as you explore and learn new PowerShell cmdlets for Windows Azure you’ll find that the power and flexibility it provides can enable you to perform much more advanced administration for your Windows Azure hosted machines and services.

See Also


The Author — Richard Hicks

Richard Hicks avatar

Richard M. Hicks (MCP, MCSE, MCTS, MCITP:EA, MCSA, MVP) is a network and information security expert specializing in Microsoft technologies. He is the founder and principal consultant of Richard M. Hicks Consulting and is focused on helping organizations large and small implement DirectAccess, VPN, and cloud networking solutions on Microsoft Platforms.

Advertisement

Featured Links