Weekend Scripter: Getting Started with Microsoft Azure and PowerShell
Summary: Microsoft senior technical evangelist talks about getting started with virtual machine’s in the cloud by using Microsoft Azure and Windows PowerShell.
Microsoft Scripting Guy, Ed Wilson, is here. Today we have as a guest blogger, Keith Mayer.
Keith Mayer is a senior technical evangelist at Microsoft. He focuses on the Windows infrastructure, data center virtualization, systems management, and private cloud. Keith has over 20 years of experience as a technical leader of complex IT projects, in diverse roles such as network engineer, IT manager, technical instructor, and consultant. He has consulted and trained thousands of IT professionals worldwide about the design and implementation of enterprise technology solutions.
You can find Keith online at http://KeithMayer.com.
And now, here’s Keith…
Microsoft Azure Infrastructure Services provides the ability to easily provision or migrate storage, virtual machines, and virtual networks onto the global Azure cloud platform by using a cost-effective Pay-As-You-Go model. Since the launch of Azure infrastructure services in April 2013, there’s been a ton of interest from IT pros regarding a number of key scenarios involving Azure virtual machines, such as:
- Off-site backup – Back up to Azure Storage as an alternative to (or in addition to) backing up to a disk or tape for a cost effective off-site backup solution.
- Disaster recovery – Provision a set of virtual machines in Azure to replicate on-premises data for use as a recovery failover option in the event of an on-premises disaster.
- On-demand development and test labs – Rather than allocate on-premises data center resources for the unpredictable capacity needs of development and test environments, provision them in Azure on an as-needed basis.
- Internet-scale web applications – Hosting Internet web apps from an on-premises data center can be challenging for many organizations due to the availability and scale needed to support peak periods of user requests. Host these web apps in Azure to take advantage of on-demand scaling of application workloads and high-availability, multihomed Internet backbone connectivity.
And these scenarios are just the start.
To begin leveraging the elastic capabilities of Azure to its fullest, we need to be able to handle provisioning and management in an automated way—and, that’s where Windows PowerShell comes in!
In this post, we’ll step through the tasks involved in leveraging the Azure PowerShell module to:
- Connect Windows PowerShell to your Azure subscription
- Provision storage in Azure by using Windows PowerShell
- Build virtual machines in Azure by using Windows PowerShell
To learn more about the basics of the Azure infrastructure services, you may also be interested in the “Early Experts” Cloud Quest and our scenario-based Cloud Labs Step-by-Step Guides. Both are FREE online study resources that provide hands-on lab exercises for leveraging Azure and building key IT pro cloud scenarios.
Here comes the Azure PowerShell module!
To automate the provisioning and management of Window Azure, you’ll need the Azure PowerShell module. The Azure team makes this module available as a free download, and this module provides approximately 180 Windows PowerShell cmdlets for automating Azure storage, virtual machines, virtual networks, and other cloud services.
To get started, you’ll need the following prerequisite items:
- An Azure subscription – If you don’t currently have a paid subscription, you can sign-up for a FREE trial subscription on the Azure account site.
- Azure PowerShell module – Download and install the Azure PowerShell module on a PC running Windows PowerShell 2.0 or Windows PowerShell 3.0.
After you satisfy these prerequisites, you’ll be prepared to follow along with the rest of this post.
Connect PowerShell to your Azure subscription
After downloading and installing the Azure PowerShell module, you need to perform the following initial steps from Windows PowerShell to configure connection information for your Azure subscription. After you perform these steps the first time, the configuration is saved locally, and you do not need to perform these steps again.
1. Set the Windows PowerShell script execution policy to RemoteSigned:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
2. Import the Azure PowerShell module:
3. Download the Azure Publish Settings file for your Azure subscription. When you run this cmdlet, a web browser launches and asks you to sign in. Sign in with the same user name and password credentials that you used when you activated your Azure subscription.
Note When you save your Publish Settings file, specify a short easily-typed file name to save your file. You need to type the complete path to this file name in the next step.
4. Import the downloaded Azure Publish Settings file into your Windows PowerShell environment. This updates your Windows PowerShell environment with the connection and certificate information that is needed to connect and authenticate to your Azure subscription.
Import-AzurePublishSettingsFile -PublishSettingsFile "full path to downloaded file"
Note After you import the downloaded Publish Settings file, store it in a secure location. The Publish Settings file contains certificate information that can be used to authenticate to your Azure subscription, and as such, it should be treated with the same security precautions that you normally observe for storing administrative credentials.
After you complete the steps in this section, Windows PowerShell is configured to connect to your Azure subscription. To test this connection, enter the following cmdlet to connect to Azure and enumerate your subscription information:
Success! The Azure subscription results from the Get-AzureSubscription cmdlet.
Provision storage in Azure by using Windows PowerShell
Prior to provisioning virtual machines in Azure, we need to set up an Azure storage account so that we have a location where virtual machines can store their associated virtual hard disks (VHDs). Each storage account in Azure can be used as a storage volume that grows dynamically as more data is copied into it—up to 100 TB in size per storage account!
Provision a new storage account in Azure by using the following steps:
1. Get a list of valid Azure data center locations for provisioning storage and compute services. You need to specify a data center location as part of provisioning new resources.
Note All data center locations do not provide the same AvailableServices, as shown in the previous command output. You need to select a data center location that provides both the Storage and PersistentVMRole services to provision storage and virtual machines. For this post, I will use the East US data center location, because that’s the location closest to where I’m stationed.
2. Create an Azure affinity group that specifies the data center location you selected. Affinity groups in Azure are used to collect storage, virtual machines, and virtual networks that are used for a common set of applications. They help place these components within a reduced number of network hops in the selected Azure data center location to improve performance between related services. In the following command, I provision a new Azure affinity group named pslab-group in the East US data center location:
New-AzureAffinityGroup -Name pslab-group -Location "East US"
3. Next, let’s provision a new Azure storage account that uses the affinity group defined previously to determine the data center location. In this example, I set up a new storage account named pslabstorage:
New-AzureStorageAccount -StorageAccountName pslabstorage -AffinityGroup pslab-group
Note Azure storage account names are required to be globally unique. If you receive an error message that states the name already exists, try using a different name.
4. Our new Azure storage account is provisioned, but we want to set this new storage account as our default storage location for our subscription by using the following command:
Get-AzureSubscription | Set-AzureSubscription -CurrentStorageAccount pslabstorage
Now, our new Azure storage account is provisioned and we’re ready to provision a new virtual machine in the cloud!
Build virtual machines in Azure by using Windows PowerShell
To start building a new virtual machine in the cloud, we need to select an appropriate virtual machine image, and then use that image to build a new virtual machine.
1. Generate a list of available Azure platform images for new virtual machines:
There are several platform images that will be returned for virtual machines, which are available for Windows Server 2012, Windows Server 2008 R2 with SP1, SQL Server 2012, BizTalk, SharePoint Server 2013, and several Linux distributions. When you select the platform image for building a new virtual machine, you need to supply the ImageName value. Unfortunately, as shown earlier, the ImageName value can be a very long value to enter! It’s much easier to use the Label value to select the appropriate image, and then return the related ImageName value into a variable as follows:
$VMImage = @(Get-AzureVMImage | Where-Object -Property Label -Match "Windows Server 2012 Datacenter, May 2013").ImageName
Perfect! Now $VMImage is storing the ImageName for our selected platform image, which we can use when provisioning a new virtual machine on the Azure cloud platform.
2. Prior to provisioning our new virtual machine, let’s set a few Windows PowerShell variables for some of the common parameters that you may want to customize, such as the virtual machine name, Admin user name, and Admin password:
$myVMName = "pslabvm01"
3. Now, we’re ready to build our new virtual machine in Azure. To configure on our new virtual machine during the provisioning process, we use the New-AzureQuickVM cmdlet to specify the virtual machine image, virtual machine name, and local Admin credentials. In addition, we’ll specify the Azure affinity group that we configured earlier so our storage and virtual machines will be organized in a common affinity group to improve performance.
New-AzureQuickVM -ImageName $VMImage -Windows -Name $myVMName -ServiceName $myVMName -AdminUsername $myAdminName -Password $myAdminPwd
After a few minutes…Presto! Our new virtual machine is created in the Azure cloud (as depicted in the previous screenshot) with an OperationStatus of Succeeded.
Note The value used for the ServiceName parameter is required to be globally unique. If you receive an error message that states this name already exists, try using a different ServiceName value.
4. Our new virtual machine is now provisioned, and we can check on its status by using the Get-AzureVM cmdlet:
Get-AzureVM -Name $myVMName -ServiceName $myVMName
Note Provisioning a new virtual machine takes a few minutes, so be sure to check the InstanceStatus property to determine when provisioning is complete. When the virtual machine is fully provisioned and online, this property returns a value of ReadyRole as indicated in the previous output.
5. At this point, you can use the following cmdlets to interact with the new virtual machine to stop, start, and restart the virtual machine operating system state:
Stop-AzureVM -Name $myVMName -ServiceName $myVMName
Start-AzureVM -Name $myVMName -ServiceName $myVMName
Restart-AzureVM -Name $myVMName -ServiceName $myVMName
Now you can manage this virtual machine by signing in to the Azure account site using the same credentials that you used when you activated your Azure subscription.
Congratulations! But keep learning!
You’ve completed the provisioning process for a new virtual machine in Azure infrastructure services by using Windows PowerShell! Now that you’ve walked through the basic steps involved in using the Azure PowerShell module with Azure infrastructure services, leverage these additional resources to continue your learning:
- “Early Experts” Cloud Quest – Build a multiple server lab network based on Windows Server 2012 by leveraging the Azure cloud platform to host your lab virtual network and virtual machines.
- Microsoft Azure Cloud Labs – Build key IT pro scenarios in the cloud by using Azure infrastructure services.
Keith, this is an excellent post. Thank you for sharing it with us.
Join me tomorrow when I have a guest blog post by Alan Byrne about the Office 365 Web Service—it is not to be missed!
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy