Using the Windows Azure Platform PowerShell Cmdlets

Realize the full potential of Windows Azure with this superb Cookbook that has over 80 recipes for building advanced, scalable cloud-based services. Simply pick the solutions you need to answer your requirements immediately.

 

Microsoft Windows Azure Development Cookbook

Microsoft Windows Azure Development Cookbook

Over 80 advanced recipes for developing scalable services with the Windows Azure platform

        Read more about this book      

(For more resources on this subject, see here.)

Getting ready

If necessary, we can download PowerShell 2 from the Microsoft download center at the following URL:

http://www.microsoft.com/download/en/details.aspx?id=11829

We need to download and install the Windows Azure Platform PowerShell cmdlets. The package with the cmdlets can be downloaded from the following URL:

http://wappowershell.codeplex.com/

Once the package has been downloaded, the cmdlets need to be built and installed. The installed package contains a StartHere file explaining the process.

How to do it...

We are going to use the Windows Azure Platform cmdlets to retrieve various properties of a Windows Azure subscription and a hosted service in it.

  1. Create a PowerShell script named Get-Properties.ps1 and insert the following text:

    $subscriptionId = 'SUBSCRIPTION_ID'
    $serviceName = 'SERVICE_NAME'
    $thumbprint = 'THUMBPRINT'
    $getCertificate = Get-Item cert:\LocalMachine\My\$thumbprint

    Add-PSSnapin AzureManagementToolsSnapIn

    Get-HostedServices -SubscriptionId $subscriptionId
    -Certificate $getCertificate
    Get-AffinityGroups -SubscriptionId $subscriptionId
    -Certificate $getCertificate
    Get-HostedProperties -SubscriptionId $subscriptionId
    -Certificate $getCertificate -ServiceName $serviceName

  2. Launch PowerShell.
  3. Navigate to the directory containing Get-Properties.ps1.
  4. Invoke the cmdlets to retrieve the properties:

    .\Get-Properties.ps1

How it works...

In step 1, we create the PowerShell script to invoke the get hosted service properties, list affinity groups, and get hosted service properties operations in the Windows Azure Service Management REST API. We need to provide the subscription ID for the Windows Azure subscription, the name of the hosted service, and the thumbprint for a management certificate uploaded to the Windows Azure subscription. In the script, we retrieve the X.509 certificate from the Personal (My) certificate store on the local machine level. If necessary, we can specify the current user level, instead of the local machine level, by using CurrentUser in place of LocalMachine when we define $getCertificate.

In steps 2 and 3, we set up PowerShell.

In step 4, we invoke the script using a .\ syntax to demonstrate that we really want to invoke an unsigned script in the current directory.

There's more...

PowerShell supports an execution policy to restrict the PowerShell scripts that can be run on a system. If the current execution policy does not permit the Windows Azure Service Management cmdlets to run, then the execution policy can be changed to remote signed by invoking the following at the command prompt:

C:\Users\Administrator>PowerShell -command "Set-ExecutionPolicy
RemoteSigned"

This sets the global PowerShell execution context. PowerShell 2 introduced a command-line switch allowing it to be set only for the current invocation:

C:\Users\Administrator>PowerShell -ExecutionPolicy RemoteSigned

Azure Management cmdlets

Cerebrata has released a commercial set of Azure Management cmdlets that are more extensive than the Windows Azure Service Management cmdlets. The following PowerShell script retrieves the list of affinity groups for a Windows Azure subscription, including the GUID identifier not available on the Windows Azure Portal:

$subscriptionId = 'SUBSCRIPTION_ID'
$thumbprint = 'THUMBPRINT'
$getCertificate = Get-ChildItem
-path cert:\LocalMachine\My\$thumbprint

Add-PSSnapin AzureManagementCmdletsSnapIn

Get-AffinityGroup -SubscriptionId $subscriptionId
-Certificate $getCertificate

We need to provide the subscription ID for the Windows Azure subscription, and the thumbprint for a management certificate uploaded to the Windows Azure subscription. In the script, we retrieve the X.509 certificate from the Personal (My) certificate store on the local machine level. If necessary, we can specify the current user level, instead of the local machine lever, by using CurrentUser in place of LocalMachine when we define $getCertificate.

We can use the following command to retrieve the list of Windows Azure locations:

Get-AzureDataCenterLocation -SubscriptionId $subscriptionId
-Certificate $getCertificate

Summary

In this article we saw how to use the Windows Azure Platform PowerShell cmdlets to invoke various service operations in the Windows Azure Service Management REST API.


Further resources on this subject:


Books to Consider

comments powered by Disqus
X

An Introduction to 3D Printing

Explore the future of manufacturing and design  - read our guide to 3d printing for free