In my last post, we looked at how you can publish your .NET Web Application Projects to IIS using Visual Studio and Web Deploy. We did this using the layman’s standard point-and-click. Menu options and buttons, galore.

But what if you want to automate the process? Why waste time clicking around when you could kick back with a calorie-free lemon-lime seltzer and let the machines do what they are good at.

Let’s take a look at how you would accomplish this using the command line, and in particular, PowerShell.

First, set up a variable to make things cleaner.

$Solution = "C:\Users\joe\Documents\Visual Studio 2015\Projects\MyWebApp\MyWebApp.sln"

Next, we’ll clean and build the project using parameters that tell msbuild to publish the app with the profile we created in the last post. The VisualStudioVersion parameter specifies which version the solution was created in and is necessary for the publishing process.

msbuild $solution /t:clean /t:build /p:DeployOnBuild=true /p:PublishProfile=MyPublishingProfile /p:Configuration=Release /p:VisualStudioVersion=14.0

Now, instead of importing the web deploy package zip in IIS, we’ll use the deploy script that was also generated.

$WebDeployScript = "C:/Users/joe/Documents/Visual Studio 2015/Projects/MyWebApp/MyWebApp/bin/publishing/MyWebApp.deploy.cmd"

First we run the script with the /T flag to test the deployment. If this runs with no errors, run the script again with the /Y flag.

& $WebDeployScript /T
& $WebDeployScript /Y

…Presto!

You have just deployed your web app using the command line.

Here’s the entire script, modify it to your liking and use it with your automated build server. You are automating your builds…aren’t you?

$Solution = "C:\Users\joe\Documents\Visual Studio 2015\Projects\MyWebApp\MyWebApp.sln"

msbuild $solution /t:clean /t:build /p:DeployOnBuild=true /p:PublishProfile=MyPublishingProfile /p:Configuration=Release /p:VisualStudioVersion=14.0

$WebDeployScript = "C:/Users/joe/Documents/Visual Studio 2015/Projects/MyWebApp/MyWebApp/bin/publishing/MyWebApp.deploy.cmd"

& $WebDeployScript /T
& $WebDeployScript /Y

If you have any questions, shoot me an email or a tweet!