Create/Publish/Delete AzureWebsites using Azure PowerShell

In this short tutorial, I am going to narrate step by step process on how to create an AzureWebsite and publish a Visual Studio 2013 ASP.Net MVC Website using Microsoft Azure PowerShell. Even though we can perform the same operations from Azure Management Portal (and also through Visual Studio), we get more flexibility and control while automating deployment and maintenance tasks from PowerShell. One can create simple scripts which can perform end to end deployment activity (a deployment is not just deploying website, it usually involves creating Databases, Users etc.,).

To get started, download and install last Microsoft Azure PowerShell – May 2015 release. Once installed, login to Azure Subscription in PowerShell by following this tutorial.

Lets create a sample AzureWebsite by using following command –

 New-AzureWebsite -Location "East US" -Name "intstringsdemo" 

We can see PowerShell executed the command successfully –

image

On successful creation, we can navigate to Azure Portal and see our newly created site –

image

We can browse to the site –

image

Using New-AzureWebsite command, we can perform following operations as well. I will narrate below operations in separate tutorials.

  1. We can associate a Local Git and online Github Repositories for continuous deployments.
  2. we can specify alternate hostname like www.abc.com

Once an AzureWebsite is created, we can deploy our ASP.Net MVC code to it using PowerShell. First create a ASP.Net MVC4 Project in VS 2013.

image

Make sure we set Copy Local = True for following References (this is because these references are not copied to the Azure and when I try to access the AzureWebsite after deployment, I got missing references error)

  1. System.Web.Http.dll
  2. System.Web.Http.WebHost.dll
  3. System.Net.Http.Formatting.dll

Now we need to create a publish profile. Publish profile creation is a one time activity. Once created we can use the this publish profile to automate build activity using msbuild, Right click solution, select Publish.

image

Select Custom option.

image

Click OK and Select File System (select a appropriate folder).

image

Click Next and Select following options.

image

Click Publish.  VS will successfully builds and publishes code to the selected folder.

Now to automate build process, we can use following build command. If msbuild command doesn’t work (I mean PowerShell doesn’t recognize msbuild command) in PowerShell console, then probably Environment variable might be missing for for .Net 4. So we can add C:\Windows\Microsoft.NET\Framework\v4.0.30319 to PATH environment variable.

msbuild C:\Users\venvemula\Desktop\IntstringsDemo\IntstringsDemo.sln /p:DeployOnBuild=true /p:PublishProfile=Test 

Above command will publish the VS project to the local file system (as stated in publish profile). After a successful build, we get following message (there will be many interim messages to show build progress) –

image

As a next step, we need to package the published folder to zip file and move the file to AzureWebsite.

To Zip the folder content, use below commands –

$source = "C:\users\venvemula\desktop\pub"
$destination = "C:\users\venvemula\desktop\pub.zip"
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($Source, $destination) 

image

Once we have Zip File, we can upload the contents  to AzureWebsite using following Azure Cmdlet –

 Publish-AzureWebsiteProject -Package C:\Users\venvemula\Desktop\pub.zip -Name intstringsdemo 

On successful publish, we have following messages displayed on PowerShell Console.

image

Now we can visit the AzureWebsite, and see our upload site using following command –

 Show-AzureWebsite -Name intstringsdemo 

image

To delete an AzureWebsite, we can use following command –

 Remove-AzureWebsite -Force -Name intstringsdemo 

Here I am going to wrap this tutorial. In next set of tutorials, we are going to see Github Automated Deployments, Load-balancing with Traffic Manager, Streaming Logs etc., Stay Tuned!!! Happy Coding!!!

You may also like...