Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke MSBuild from PowerShell using & operator?

Tags:

powershell

I've just tested this on PowerShell v1.0. Setup is as follows:

 Id CommandLine  -- -----------   1 $msbuild = "C:\Windows\Microsoft.NET\Framework\v3.5\msbuild.exe"   4 $a = "C:\some\project\or\other\src\Solution.sln /target:Clean /target:Build" 

.

This line fails with an unintuitive error message:

 Id CommandLine  -- -----------   5 & $msbuild $a 

.

This line fails because & expects the first argument to be the command itself.

 Id CommandLine  -- -----------  10 & "$msbuild $a" 

.

This line works:

 Id CommandLine  -- -----------  16 cmd /c "$msbuild $a" 

.

Please explain. I'm more interested in why the & syntax isn't working, than an MSBuild-specific workaround.

like image 365
Peter Seale Avatar asked May 15 '09 15:05

Peter Seale


People also ask

How do I run MSBuild from command prompt?

To run MSBuild at a command prompt, pass a project file to MSBuild.exe, together with the appropriate command-line options. Command-line options let you set properties, execute specific targets, and set other options that control the build process.

How do I invoke PowerShell?

Click Start, type PowerShell, and then click Windows PowerShell. From the Start menu, click Start, click All Programs, click Accessories, click the Windows PowerShell folder, and then click Windows PowerShell.

What is MSBuild command?

Builds the targets in the project file that you specify. If you don't specify a project file, MSBuild searches the current working directory for a file name extension that ends in proj and uses that file. You can also specify a Visual Studio solution file for this argument.


1 Answers

Ugh.

$collectionOfArgs = @("C:\some\project\or\other\src\Solution.sln",      "/target:Clean", "/target:Build") & $msbuild $collectionOfArgs 

This works. & takes a collection of arguments, so you must split up strings containing multiple arguments into a collection of string arguments.

like image 131
Peter Seale Avatar answered Sep 18 '22 21:09

Peter Seale