How to do the exact same functionality as the Publish inside visual studio from powershell

Mike picture Mike · Nov 4, 2011 · Viewed 7.7k times · Source

I want to know how to publish from PowerShell the same way I can from Visual Studio.

When you launch Visual Studio with MVC project for example; then right-click on the project and select "Publish"; a dialog box comes up that you fill out a few settings in the click Publish again. It magically pushes your website out as expected. It also creates a file to hold those settings. projectName.Publish.xml.

That file is the reason I want to be able to use this in exactly the same manner. There are a couple of settings that could manipulated in powershell to push the application to multiple sites with differing configurations.

I have looked at "msbuild target:publish" which only produces (run from the directory containing the .csproj):

MSBUILD : error MSB1009: Project file does not exist.
Switch: target:publish 

I have tried "msbuild projectfile target:publish" which produces:

MSBUILD : error MSB1008: Only one project can be specified.
Switch: target:build

I've also looked at several msdeploy variations. It can't be this difficult!? If you can right-click in studio there has be something that performs that task.

I'm starting to wonder if this requires a full moon and cutting the head off a chicken. (I've spent to much time on this!)

**EDIT: I tried Lee's suggestion:

msbuild target:publish Mvc.csproj

Which produces this error:

MSBUILD : error MSB1008: Only one project can be specified.
Switch: Mvc.csproj

For switch syntax, type "MSBuild /help"

**EDIT: From DaveE's suggestion, I tried:

msbuild target:publish Mvc.csproj

Which produces this error:

MSBUILD : error MSB1009: Project file does not exist.
Switch: target:publish=PassportHealth.PatientSimple.Mvc.csproj

I also went back to the documentation for the "Publish" target (http://msdn.microsoft.com/en-us/library/ms165431.aspx) which seems pretty clear that the syntax is:

msbuild target:publish

So after rereading this several times my conclusion is this: the publish target is for one-click deployments of .exe files not MVC web sites. I have found http://weblogs.asp.net/scottgu/archive/2010/09/13/automating-deployment-with-microsoft-web-deploy.aspx which speaks to this matter fairly clearly. I don't see how to utilized the "publish.xml" file in this via the msdeploy command yet though.

Answer

Jimmy picture Jimmy · Nov 5, 2011

The projectName.publish.xml files are only used via Visual Studio for the One-Click publishing. For MSBuild, you need to pass a bunch of parameters directly on the commandline (/p:Param1=value1;Param2=value2;...). For example:

/p:DeployOnBuild=True /p:DeployTarget=MsDeployPublish /p:CreatePackageOnPublish=True /p:MSDeployPublishMethod=InProc /p:MSDeployServiceUrl=localhost /p:DeployIisAppPath="Default Web Site/NewOrleansJazz" /p:UserName=domain\user /p:Password=myPassword (source)

Note that in the VS11 Preview (released at the BUILD conference), there is a new file replacing the projectName.publish.xml: MyPublishSettings.pubxml. This file is basically a combination of the publish.xml file and the projectName.wpp.targets rolled into one (yet also split for each profile instead of having them all saved into publish.xml, e.g. TestSettings.pubxml, ProductionSettings.pubxml), and can be used from the command line in a simpler fashion using msbuild MyProject.csproj /t:WebPublish /p:PublishProfile="myProfile"

[edit:] a couple notes re: the info in your post:

  1. The error about only specifying a single project is probably due to specifying the target incorrectly. Other than the project name, all parameters passed to msbuild start with a /, e.g. /t[arget]:foo /p[roperty]:var=value
  2. The Publish target is actually not related to Web Publishing. IIRC, it's related to publishing a OneClick application. In VS2010, you should use /t:build /p:DeployOnBuild=true for Web projects to use the publishing tasks (as used above). New in VS11 is the /t:WebPublish which also takes a simpler set of parameters, namely the .pubxml file you wish to use.
    • additional note: when building a solution file, /t:WebPublish will fail as the target is not defined at the solution level. It can only be used directly on the project file for your Web project. You can still use /p:DeployOnBuild=true at the solution level as the parameter value is passed to each project being built, but will be ignored by any projects not consuming that value.