Azure Power Shell: Azure Cloud Services for the Command Line Junkie -Part 3

Web UI looks good and pretty but if you really want to get work done at scale, manage, automate and administer: command line is your way. As a command line junkie from the UNIX world I thought of exploring what Azure PowerShell had to offer. And boy, I was quite blown by the ease of use, functionality and flexibility it comes with. Through this tutorial series, I will take you through various scenarios and functionality on Azure that is made easy using Azure Power Shell


Till now, I have covered the following services in Azure and how we use Power Shell to manage those:

Azure Powershell: Azure Websites for the command line junkies! – Part 1
Azure Power Shell: Azure Virtual Machines for the command line junkie!

In this section I will cover some key Cloud Service concepts and how we can manage cloud services using Azure Power Shell. Cloud services are generally the preferred mode of development for decoupled, multi tiered applications that can scale independently of each other. The two primary components are web role and worker role. The web role can be thought of as the web front facing interface where as the worker role can be thought of as a backend process role and the communication between them can be accomplished using either Storage queues or Service buses.

Getting Started

As always, the first thing that needs to be done is to assign a subscription in the power shell console:

   1: Add-AzureAccount
   2: Get-AzureSubscription
   3: Select-AzureSubscription "Azure Pass" -CurrentStorageAccountName $storageAccount


There are 2 main configuration files for Cloud Services. The configuration file (.csfg) and the cloud service definition (.csdef) file. These are xml formatted files and generally contain configuration parameters such as the number of roles, the number of instances supporting the roles, etc. You can either set the number of instances in the (.cscfg) using the element “Instance count” or using the power shell as shown:

   1: $csName="myCloudService"
   2: $csRole = "Demo.webrole"
   3: $RoleCount = 5
   4: Set-AzureRole -ServiceName $csName -RoleName $csRole -Slot Staging -Count $roleCount

Note: for the SLA, the instance  count should be at least 2 to ensure high availability.

If you were to create custom domains for your cloud service, you could create the A record or CNAME record by getting the required values either from the management portal in the ‘quick glance’ section or you could obtain it using the following commands:

   1: #For CNAME record
   2: $CsName = "MyCloudService"
   3: Get-AzureDeployment -ServiceName $CsName -Slot "Production" | Select Url
   5: #For A Record
   6: Get-AzureVM -ServiceName $csName | Get-AzureEndpoint | Select Vip

Reserved IPs are often useful if you would want to preserve the IP of a cloud service in case you delete one and would want to reuse the IP for the cloud service. Currently it is not possible to add an ip address to a Reserved Ip list, hence you are supposed to create the reserved IP before you create a cloud service to use the IP. Also important thing to note is that you can only create reserved IPs through the powershell. The following commands can be used to create and use a reserved IP:

   1: #Create a Reserved IP
   2: New-AzureReservedIP -ReservedIPName "contosocloudserviceip" -Location "East US"
   4: #Get the reserved IPs associated with a subscription
   5: Get-AzureReservedIP | Select ReservedIPName, Address

Now if you want to use the reserved IP in your cloud service you will need to add the <ReservedIP name> element in the < NetworkConfiguration> element of the configuration file (.cscfg).

If you want to configure SSL for your cloud service, the thumbprint will be incorporated in the service definition file (.csdef) and will appear in the <certificates> element of the configuration file (.cscfg). Now you can add the certificate file either using the Management portal or you could use the following commands to add the certificate to your cloud service:

   1: $cert = Get-Item <path to .pfx certificate on local system>
   2: $certPassword = "<password>"
   3: $csName = "MyCloudservice"
   4: Add-AzureCertificate -ServiceName $csName -CertToDeploy $cert -Password $certPassword

Deploying and Publishing a cloud service generally involves creating a packaged version (.cspkg) of your cloud service that can be uploaded on Azure. Packaging can be done in several ways and one of them is by using the CSPACK command line tool that comes with the Azure SDK. Once the package is created, publishing can be done using the management portal, visual studio or PowerShell. The following commands can be used to publish the cloud service:

   1: $CSLocation = "East US"
   2: $deployName = "NewDeploy"
   3: $pathCSPKG = Get-Item <Path of .cspkg>
   4: $pathCSCFG = Get-Item <Path of .cscfg>
   6: Publish-AzureServiceProject -Location $csLocation -Slot "Staging" -Package $pathCSPKG -Configuration $CSCFG -DeploymentName $deployName


Through this post I covered some of the options that can be explored using PowerShell for Cloud Services. Cloud Services including web and worker roles is powerful and gives the flexibility to accomplish a lot. The Management Portal has a lot of options and certain functions such as scaling, monitoring and diagnostics that cannot be accomplished using PowerShell and should definitely be explored. One more key topic is Service Bus which is important for communication between the web and worker roles. The various functions such as Queues, Topics, Relays and Event hubs have a lot to offer in cloud Services and should be explored.

Comments (0)

Skip to main content