Microsoft Azure Automation And Cmdlets

One of the primary tools in use today for automating Microsoft Azure from the Windows platform is Cmdlets. They give you the power to provision Compute and other services on the fly just by using a Console.
What is Cmdlet

A cmdlet is a lightweight command that is used in the Microsoft PowerShell environment. The Windows PowerShell runtime invokes these cmdlets within the context of automation scripts that are provided at the command line. The Windows PowerShell runtime also invokes them programmatically through Windows PowerShell APIs. 
A cmdlet is a compiled piece of .NET code. Cmdlets handle object input and output as well as usually playing nice and well with the (object-based) pipeline. Cmdlets have no direct representation in the file system, as they are not programs or similar. They exist solely within PowerShell. You can use the Get-Command Cmdlet to query all available Cmdlets, functions, etc.
By now you might have in your mind -- Why should one even care about a Console when we already have an Interactive Portal? Here we will see why exactly we need it.
The Windows Azure Portal is really good at what it does but it is not meant for Automation.
Let's see a comparison of Web Deployment Methods: 

Deploying an Application in a Traditional Data Center
  1. Order server and networking hardware.
  2. Wait for hardware to ship.
  3. Install and configure networking hardware.
  4. Install and configure server hardware (apply firmware updates as needed).
  5. Install a base operating system on the server hardware.
  6. Patch the base operating system.
  7. Install software applications and roles.
  8. Deploy applications.
  9. Repeat steps 3 through 8 (and likely steps 1 and 2, depending on how accurate the initial planning was) for staging, development, and testing environments. 
Deploying an Application in the Cloud (Without Automation)
  1. Launch the management portal.
  2. Create and configure each virtual machine.
  3. Patch the base operating system.
  4. Install software applications and roles.
  5. Deploy applications.
  6. Repeat steps 2 through 5 for staging, development, and testing environments.  
Deploying an Application in the Cloud (with Automation) 
  1. Identify repeatable processes.
  2. Create automation configuration and scripts for step 1.
  3. Deploy scripts for application.
  4. Repeat step 3 for staging, development, and testing environments. 
You can see that Deployment on Cloud with Automation tops the flexibility and time consumption.
How to Start with Windows Azure Automation using Cmdlets
The Microsoft Azure PowerShell cmdlets are officially supported on Windows 2008 R2, Windows 7, Windows 8/8.1, and Server 2012/2012 R2. Microsoft PowerShell comes with all these Operating Systems by default but for all the Cmdlets, they come in separate. So, there are specific Cmdlets for different purposes,  for example, for Microsoft Azure, SQL Server, Web Automation and like that. For Microsoft Azure automation we need to have Azure Powershell
  • Go to Microsoft Azure home page
  • Click the Downloads link on the page (See at the very bottom of the page)
  • Click the link for Command Line Tools
  • The installation can take several minutes because there is a dependency on the Microsoft Azure SDK, which has its own set of dependencies. 
Setting Up Your Environment 
When the installation for the cmdlets is complete, you can choose your method of running them. You can launch PowerShell either by clicking the PowerShell icon on your computer or by running powershell.exe.

There is another tool for Powershell called Powershell ISE (Integrated Scripting Environment). I prefer to use this one. Being a Visual Studio user I always like IntelliSense and Code-Completion! I think you'll like it too.
You can start it by running "powershell_ise.exe" .
Powershell ISE really looks more powerful and has many more commands to perform many more operations.
To do anything with your Azure subscription using Azure Powershell you need to Authenticate first. This can be done in two ways:
  • Authenticating with a Certificate
  • Using Microsoft Azure AD to Authenticate with Powershell 
Step 2: Authenticating with a Certificate,
The easiest way of doing Authentication with a certificate is to download ".publishsettings" file from Microsoft Azure using the "Get-AzurePublishSettingsFile" Cmdlet.
You will be prompted to download a .publishsettings file after successful login. Be careful with the file as it contains your subscription information along with a newly generated Management Certificate. Whoever has access to this file has access to your subscription.
Next step is to import the .publishsettings file into Azure Powershell using "Import-AzurePublishSettings" Cmdlet.
On successful import it will show you the account details and now the console is ready to talk to your Microsoft Azure subscription. 
Step 2: Using Microsoft Azure AD to Authenticate with Powershell,
Use "Add-AzureAccount" Cmdlet to Authenticate using Microsoft Azure Active Directory.
It will prompt you for your Microsoft Azure subscription login credentials and return a Token to Azure Powershell for making authenticated requests. The returned token remains valid for 12 hours. After 12 hours you need to authenticate again using the same Cmdlet. 
And that's it, your Powershell console is configured.
You can manage your subscription with these following Cmdlets:
  • Get-AzureSubscription: returns and enumerates subscriptions that have been imported or manually configured with the Set-AzureSubscription cmdlet.

  • Select-AzureSubscription: helps switching between different subscriptions in your account.

  • Set-AzureSubscription: allows you to add a subscription to the stored settings or change properties on an existing subscription.

  • Remove-AzureSubscription: helps removing the subscription from local PowerShell configuration.
We will see more on these Cmdlets in further discussions. 
In this article we learned that we can use Microsoft Azure Powershell, an advanced console application, for easier manipulation of Microsoft Azure resources using few simple commands. We have learned how to get started with it. In the next article we will learn how to setup and configure a Web site just by using Microsoft Azure Powershell and Cmdlets.
Thanks for reading!