Microsoft Azure (formerly Windows Azure) is a cloud computing service provided by Microsoft. Azure provides the platform and infrastructure to deploy and manage applications and services through a global network of datacenters. These services include websites, databases, virtual machines, message queuing, identity management, content delivery, and so on.
Tip
Microsoft is continually adding new services and features to services provided in Microsoft Azure. To keep yourself updated on the latest Azure offerings, subscribe to the Microsoft Azure blog at http://azure.microsoft.com/blog/. In addition, the Microsoft Azure website provides detailed information on all current Azure offerings at http://azure.microsoft.com.
With Microsoft Azure geared to supplement and integrate into the infrastructure of large enterprise organizations, many organizations apply their data center operation policies and methodologies to their Azure implementations. For many of these organizations, scripting deployment, configuration, and management tasks is a must.
Much like other Microsoft server products (Exchange Server and SharePoint Server for example), Microsoft Azure services and products can be instantiated, configured, and managed using Windows PowerShell. Using PowerShell, we can automate and script many of the deployment, configuration, and management tasks that are common to data center operations.
From connecting to Microsoft Azure to managing Active Directory instances on Azure, this book will cover how to automate and script common tasks to manage Microsoft Azure. In this chapter, we will get acquainted with Windows PowerShell and get connected to Microsoft Azure. Lastly, we will create an Azure website to introduce how we can manage Microsoft Azure using Windows PowerShell.
Windows PowerShell is an automation and configuration framework provided by Microsoft. It comprises a command-line shell and a scripting language built on the Microsoft .NET Framework. The commands used in PowerShell are referred to as
cmdlets and, typically, have a verb prefix followed by a topical command name. For instance, the cmdlet to add a computer to an Active Directory domain is Add-Computer
.
Tip
Nearly all Microsoft-provided cmdlets for PowerShell include help content that can be accessed using the Get-Help
cmdlet. To retrieve help content for the Add-Computer
cmdlet, for instance, you would simply enter Get-Help Add-Computer
.
Modules and snap-ins that have been imported into the current PowerShell session provide the cmdlets in PowerShell. The core modules bundled with PowerShell provide the cmdlets that are available out of the box with PowerShell. Other Microsoft server products, such as Microsoft SharePoint Server (the 2010 version and higher) and Microsoft Exchange Server (the 2007 version and higher), provide modules and snap-ins that make cmdlets specific to these products available in a PowerShell session.
The following screenshot shows what the PowerShell Command Prompt looks like. The default color scheme for PowerShell windows is gray text on a dark blue background; for clarity, however, when printed or viewed in black and white, the screenshots used in this book use black text on a white background.

With PowerShell built on the Microsoft .NET Framework, .NET classes and methods can be used directly from the Command Prompt. For instance, while the Get-Date
cmdlet will return a System.DateTime
object, we can also accomplish the same task by entering [System.DateTime]::Now
, as shown in the following screenshot. This is particularly useful when creating complex scripts that require using .NET classes and methods not already exposed as cmdlets.

When accessing a static method or property of a .NET class, the full class name (with its namespace) is placed in square brackets ([
and ]
). The method name or property name is then placed after two semicolons (::
). For instance, accessing the Now
property of System.DateTime
is written as [System.DateTime]::Now
, and accessing the IsLeapYear
method of System.DateTime
is written as [System.DateTime]::IsLeapYear(2020)
. It's not required to know how to interact directly with .NET classes and methods when using PowerShell. However, this is useful when functionality needs to be extended beyond what the available cmdlets can do.
In addition to the Windows PowerShell Command Prompt, Microsoft has provided the Windows PowerShell Integrated Scripting Environment (ISE). PowerShell ISE is an application specifically designed to write PowerShell scripts. It includes IntelliSense, the Command Prompt, and a list of available cmdlets. The examples throughout this book will use the PowerShell Command Prompt, as shown in the preceding screenshot. However, you can use the PowerShell ISE to complete any of the examples provided:

Windows PowerShell is included in Windows, starting with Windows 7 and Windows Server 2008 R2. The following table indicates which version of PowerShell is included with each version of Windows:
PowerShell version |
Windows versions |
---|---|
PowerShell 2.0 |
Windows 7, Windows Server 2008 R2 |
PowerShell 3.0 |
Windows 8, Windows Server 2012 |
PowerShell 4.0 |
Windows 8.1, Windows Server 2012 R2 |
Using PowerShell to manage Microsoft Azure requires PowerShell 3.0 or higher. Windows 7 and Windows Server 2008 R2 do not include a supported version of PowerShell. In order to manage Microsoft Azure on Windows 7 or Windows Server 2008 R2, the Windows Management Framework will need to be updated to version 3.0 or higher. To download the Windows Management Framework 3.0 (which includes PowerShell 3.0), visit http://www.microsoft.com/en-us/download/details.aspx?id=34595, and to download the Windows Management Framework 4.0 (which includes PowerShell 4.0), visit http://www.microsoft.com/en-us/download/details.aspx?id=40855.
Tip
While PowerShell 4.0 and higher are not required to manage Microsoft Azure, each subsequent version of PowerShell after 3.0 has enhanced the overall capabilities of the framework. To upgrade to a newer version of PowerShell, download the latest version of the Windows Management Framework.
For more information about Windows PowerShell and Microsoft Azure, see the following resources:
Scripting with Windows PowerShell (http://technet.microsoft.com/en-us/library/bb978526.aspx)
Windows PowerShell (http://en.wikipedia.org/wiki/Windows_PowerShell)
Microsoft Azure website (http://azure.microsoft.com)
Windows Management Framework 3.0 download (http://www.microsoft.com/en-us/download/details.aspx?id=34595)
Windows Management Framework 4.0 download (http://www.microsoft.com/en-us/download/details.aspx?id=40855)
Before using PowerShell cmdlets for Microsoft Azure, we must first have an active Microsoft Azure subscription, install the necessary prerequisites, and connect to the Microsoft Azure subscription. In addition, as Microsoft Azure is a cloud service, we must have an Internet connection in order to manage it.
Microsoft Azure is a subscription-based service, typically billed monthly. Microsoft Developer Network (MSDN) subscribers receive a free monthly credit for development purposes. In addition, Microsoft offers a free trial of Microsoft Azure for one month (up to $200 worth of services). To sign up for the free trial, visit http://azure.microsoft.com/en-us/pricing/free-trial.
For the purposes of the examples provided in this book, it does not matter what type of Microsoft Azure subscription is used. It simply must be active and you need global administrator rights to the subscription.
To manage Microsoft Azure with Windows PowerShell requires the following prerequisites:
Windows client operating system (Windows 7 or newer) or Windows server operating system (Windows Server 2008 R2 or newer)
Windows PowerShell 3.0 or newer (bundled with the Windows Management Framework)
Microsoft Azure PowerShell
While managing the operating system is outside the scope of this book, it is a good idea to ensure that the Windows operating system is up-to-date with the latest security updates and service packs.
The Microsoft Azure PowerShell management tools require at least PowerShell 3.0. If the computer is running Windows 8 (or newer) or Windows Server 2012 (or newer), no additional configuration is required for PowerShell. Since Windows 7 and Windows Server 2008 R2 are bundled with PowerShell 2.0, an updated version of the Windows Management Framework will need to be installed.
If the computer is running Windows 7 or Windows Server 2008 R2, follow these steps to verify that PowerShell 3.0 or newer is installed:
Open Windows PowerShell from the Start menu.
Enter
$PSVersionTable
in Command Prompt and press Enter. ThePSVersion
property will display the installed version of PowerShell, as shown here:If the PowerShell version, as mentioned in step 2, is not 3.0 or greater, download and install a newer version of the Windows Management Framework. To download the Windows Management Framework 3.0 (which includes PowerShell 3.0), visit http://www.microsoft.com/en-us/download/details.aspx?id=34595. To download the Windows Management Framework 4.0 (which includes PowerShell 4.0), visit http://www.microsoft.com/en-us/download/details.aspx?id=40855.
Microsoft Azure PowerShell installs a PowerShell module that provides the cmdlets used to manage Microsoft Azure.
Tip
The Microsoft Azure PowerShell project is open source. If desired, you can download the source code for the project and use it, rather than using the official version. The project is hosted on GitHub at https://github.com/Azure/azure-powershell. In addition, each version of Microsoft Azure PowerShell can be obtained from https://github.com/Azure/azure-sdk-tools/releases.
To install Microsoft Azure PowerShell, we will use the following steps:
Install the Microsoft Web Platform Installer from http://www.microsoft.com/web/downloads/platform.aspx.
Open the Microsoft Web Platform Installer.
Search for
Microsoft Azure PowerShell
.Select Add for Microsoft Azure PowerShell (Standalone), as shown in the following screenshot:
Then click on Install.
Select Finish.
Finally, close the Web Platform Installer.
Prior to running cmdlets to manage Microsoft Azure, we need to connect to a Microsoft Azure subscription. There are three ways to connect to an Azure subscription:
Using Azure Active Directory credentials
Using a publish settings file
Using an uploaded management certificate
The third option, using an uploaded management certificate, requires developer tools to be installed on the local machine. This is outside the scope of this book. If you would like more information on using a management certificate with Microsoft Azure, refer to http://msdn.microsoft.com/en-us/library/azure/gg551722.aspx.
Connecting to Azure subscription using Azure Active Directory credential is a fairly simple process. One drawback, however, is that you need to enter your credentials each time you start a new Azure PowerShell session, or every 12 hours if the session is kept open.
Tip
To automate data center operations, entering credentials for each session might not be ideal. Therefore, consider using a publish settings file, as outlined in the next section, Connecting to Azure using a publish settings file, or consider using a management certificate (http://msdn.microsoft.com/en-us/library/azure/gg551722.aspx).
Use the following steps to connect to Microsoft Azure using Azure Active Directory credentials:
Open Microsoft Azure PowerShell from the Start menu.
Use the
Add-AzureAccount
cmdlet by entering it in the Command Prompt and pressing Enter, as shown here:When prompted, enter the e-mail address for the account used to manage Microsoft Azure and select Continue (the Microsoft account, using your e-mail address, used when creating an Azure subscription is automatically added to an instance of Azure Active Directory):
Enter the password for your account and select Sign in.
Once the sign-in is complete, the Command Prompt will indicate that the account has been added and will indicate which subscription has been selected, as shown here:
For many automated data center operations, manually entering user credentials for each PowerShell session (or every 12 hours) can be cumbersome. Alternatively, a publish settings file can be downloaded from Azure that allows PowerShell sessions to connect without entering user credentials. This is useful for highly automated processes that do not have human interaction (such as a scheduled backup job).
To connect to Azure using a publish settings file, we will use the following steps to retrieve and import an Azure publish settings file:
Open Microsoft Azure PowerShell from the Start menu.
Enter the
Get-AzurePublishSettingsFile
cmdlet and press Enter, as shown here:The Microsoft Azure portal will be opened in Internet Explorer. If prompted, enter your credentials to log in to the Azure portal.
If you have multiple subscriptions associated with your account, you will be prompted to select a subscription, as shown in the following screenshot:
When prompted to download the Azure publish settings file, save it to the local computer:
In the PowerShell window, use the
Import-AzurePublishSettingsFile
cmdlet (as shown below) to import the Azure publish settings file, which was downloaded in the previous step (PS C:\> Import-AzurePublishSettingsFile C:\Files\Azure.publishsettings
):
Whether we used the Azure Active Directory credentials method or Azure's publish settings file method, we are now connected to Microsoft Azure in our PowerShell session.
For more information on how to connect to Microsoft Azure with PowerShell and the cmdlets used in this section, refer to the following resources:
Create and upload a Management Certificate for Azure (http://msdn.microsoft.com/en-us/library/azure/gg551722.aspx)
The
Import-AzurePublishSettingsFile
cmdlet (http://msdn.microsoft.com/en-us/library/dn495124.aspx)The
Get-AzurePublishSettingsFile
cmdlet (http://msdn.microsoft.com/en-us/library/dn495224.aspx)The
Select-AzureSubscription
cmdlet (http://msdn.microsoft.com/en-us/library/dn495203.aspx)The
Add-AzureAccount
cmdlet (http://msdn.microsoft.com/en-us/library/dn722528.aspx)Microsoft Azure PowerShell Project on GitHub (https://github.com/Azure/azure-powershell)
Microsoft Web Platform Installer download (http://www.microsoft.com/web/downloads/platform.aspx)
Windows Management Framework 3.0 download (http://www.microsoft.com/en-us/download/details.aspx?id=34595)
Windows Management Framework 4.0 download (http://www.microsoft.com/en-us/download/details.aspx?id=40855)
Now that we are connected to our Microsoft Azure subscription in PowerShell, we will create a simple Azure website to illustrate what we can do with Azure PowerShell.
Note
Many of the cmdlets used to instantiate new services, such as a website, in Microsoft Azure include a -Location
parameter. This parameter specifies in which Azure data center the new service will be instantiated. To get a list of the data centers available to your subscription, use the Get-AzureLocation | Format-List -Property Name
command in PowerShell.
To create an Azure website, we will use the following steps:
Use the
New-AzureWebsite
cmdlet to create the new Azure website (PS C:\> New-AzureWebsite –Name "PowerShellAutomationIsAwesome" -Location "WestUS"
). The name specified will be used as part of the default hostname of the website and must be unique, as shown in the following screenshot:When complete, detailed information about the new website will be displayed as an output in the Command Prompt window:
By default, the URL to the newly created website will be the name used in step 1 with
.azurewebsites.net
appended to it. In this example, the new URL ishttp://powershellautomationisawesome.azurewebsites.net
. Navigate to the newly created website in a web browser to observe the results, as shown here:
For more information about the cmdlets used in this section, refer to the following resources:
The
New-AzureWebsite
cmdlet (http://msdn.microsoft.com/en-us/library/azure/dn495157.aspx)The
Get-AzureLocation
cmdlet (http://msdn.microsoft.com/en-us/library/azure/dn495177.aspx)
In this chapter, we became acquainted with Windows PowerShell. You learned how to connect Windows PowerShell to Microsoft Azure subscriptions. We will cover many advanced topics related to PowerShell in the course of this book; however, as we cover Azure-specific tasks; however, we will not dig into PowerShell itself (beyond the introduction in this chapter). If you are not yet familiar with using Windows PowerShell, refer to the Scripting with Windows PowerShell article on TechNet at http://technet.microsoft.com/en-us/library/bb978526.aspx to get better acquainted with the basic techniques and methodologies of scripting with PowerShell.
In the next chapter, we will explore using PowerShell to manage Azure storage accounts, including file, blob, table, and queue storage.