Windows Azure – Deployment using Powershell

In my last post I described about how to create new service on windows azure using power shell.
In this post I am going to explain how can we do a production deployment using powershell.
The pre-requisites are again same-

  • Configure remote execution
  • Import windows azure powershell cmdlets.

This time we will be setting subscription differently. We will use publish file to set the subscription. You can get the publish file by –

  • Donwloading it manually by browsing directly to the site:https://windows.azure.com/download/publishprofile.aspx
  • You can use following powershell cmdlet- Get-AzurePublishSettingsFile. 
    This command will automatically download the publish file by taking you to windows azure web site.

Once you have publish file downloaded you can setup the subscription by following cmdlet-
Import-AzurePublishSettingsFile ‘PATH OF PUBLISH FILE\MySubs.publishsettings’

You can verify the subscription by following cmdlet-
Get-AzureSubscription

I am assuming you have created the service and you just want to have a production deployment. To do the deployment run following cmdlet-

#Variables Declaration
$service = “testpowershelldeploymentfromstorage” #this is the name of the cloud service you created
$slot = “production” #staging or production
$package = “PATH OF PACKAGE FILE\TestPowerShell.cspkg”
$configuration = “PATH OF CONFIG FILE\ServiceConfiguration.Cloud.cscfg”
$deploymentLabel = “PowerShell Deployment”

# Deployment cmdlet
New-AzureDeployment -Slot $slot -Package $package -Configuration $configuration -label $deploymentLabel -ServiceName $service

There you go. You will see the outputs and after it is done you can go to azure portal and see the deployment.

Windows Azure – Create Service using PowerShell

Windows Azure PowerShell has set of cmdlets which can be used to create a new windows azure service.

Before starting there are couple of pre-requisite to run below script-

To make sure  you can run remote signed scripts in PowerShell you need to run following command on powershell command prompt-

Set-ExecutionPolicy RemoteSigned

I am assuming you must have installed windows azure powershell cmdlets. if not please install the Windows Azure Cmdlets (http://www.windowsazure.com/en-us/manage/downloads/).

Now you need to import the cmdlets by using following command-
Import-Module “C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\Azure\Azure.psd1

Following is the set of cmdlets which can be combined and used in step wise manner to create new azure service:-

Before creating any service on windows azure, one need to connect to windows azure subscription. There are multiple ways to connect to the subscription. One way is to connect using publish file. Publish file can be downloaded from Azure portal.  I will probably discuss that approach in some other post. Right now I am going to explain the other approach where you will require

  • Subscription-Id -This can be found from Windows Azure portal
  • Subscription Name- Name of the azure subscription
  • Certificate Thumbnail – This is required to create a trusted connection to remote windows azure subscription. Required certificate should be uploaded to windows azure subscription’s certificate store.

For above information you can create powershell variables and use it though out the script.
Following script can be run to connect the azure subscription –

# Variable Declaration
$subid = ‘SUBSCRIPTION-ID. THIS CAN BE FOUND ON AZURE PORTAL’
$subsName=’NAME OF THE SUBSCRIPTION. THIS CAN BE FOUND FROM AZURE PORTAL’
$cert = Get-Item Cert:\CurrentUser\My\THUMBNAIL OF CERTIFICATE
$serviceName = ‘PUT THE NAME OF THE SERVICE TO BE CREATED’
$ServiceLabel = ‘LABEL FOR THE NEW SERVICE’
$Affinitygroup = ‘LOCATION WHERE NEW SERVICE WILL BE DEPLOYED. E.G., WEST US’

#Set the subscription
Set-AzureSubscription -SubscriptionName $subsName -SubscriptionId $subid -Certificate $cert

Once you are connected, you can get the details of subscription detail by following cmdlet –

Get-AzureSubscription

Now to create new service within the subscription following cmdlets can be used-

New-AzureService -ServiceName $serviceName -Label “MyTestService” -Location $Affinitygroup

Once you run the above command the new service will be created on the subscription.

I will be discussing another approach to connect to windows azure subscription and deploy service using package file and configuration file.

Windows Azure PowerShell

Windows Azure PowerShell is a powerful scripting environment thatcan be used to control and manage the windows azure environment. Windows Azure PowerShell gives set of commandlets which can be used to create amd manage windows azure services, web sites and VMs.

I was looking for way to automate deployments and found that the windows azure powershell can be one of the tools which can be used for most of the things we do on windows azure platform like –

  • Connect to Windows Azure subscription
  • Create New Windows Azure Service
  • Create New Deployment
  • Remove existing deployments and services
  • Provisioning and de-provisioning of VMs and many more.

As I am going deep and learning the PowerShell I am kind of fascinated to it. I will start a series on what I understand of PowerShell and will come up with scripts to automate the whole deployment process.

I am also planning to come up with some kind of .Net Application which will be kind of wrapper around PowerShell but with an easy interface.