Executing msbuild task from powershell

2019-06-28 02:28发布

问题:

I am following this blog: http://sedodream.com/2010/04/26/ConfigTransformationsOutsideOfWebAppBuilds.aspx

it's linked in this question as answer: Web.Config transforms outside of Microsoft MSBuild?

Every step works as described but I want to call the transformation from a powershell script. I want to do this command in powershell msbuild trans.proj /t:Demo I found some forums saying that I should use invoke-expression

When I try I get this error

Powershell:

Invoke-Expression $msbuild transformCommand.proj /t:Demo

Result

Invoke-Expression : Cannot bind argument to parameter 'Command' because it is null.
At D:/Somewhere
+ Invoke-Expression <<<<  $msbuild transformCommand.proj /t:Demo
    + CategoryInfo          : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpre
   ssionCommand

I also tried:

Invoke-Expression msbuild transformCommand.proj /t:Demo

With result

 D:\Somewhere
Invoke-Expression : A positional parameter cannot be found that accepts argument 'transformCommand.proj'.
At D:\Redgate\Communited.Timeblockr.Api\PreDeploy1.ps1:14 char:18
+ Invoke-Expression <<<<  msbuild transformCommand.proj /t:Demo
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Expression], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeExpressionCommand

Small note: this is my first time using powershell.

TransformCommand.proj

<Project ToolsVersion="4.0" DefaultTargets="Demo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <UsingTask TaskName="TransformXml"
             AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>
    <Target Name="Demo">
        <TransformXml Source="Web.Test.config"
                      Transform="transform.xml"
                      Destination="Web.acceptatie.config"/>
    </Target>
</Project>

My questions are: Is this possible? And how is it possible?

Edit:

$a = "Path\transformCommand.proj /t:Demo"
#Invoke-Expression $msbuild $a

Start-Process -FilePath "C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe" -ArgumentList $a | Write-Host 

This actually does everything I need..

Still if there are ways to do this more "indepent" or better ways, please post it.

回答1:

Make sure you define the PowerShell variable $msbuild as below before calling Invoke-Expression $msbuild

$msbuild = "C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe"


回答2:

I ended up doing the following to build a solution with multiple parameters

&$msbuildExe ('solution.sln','/verbosity:q','/p:configuration=Release','/t:Clean,Build')
if (!$?) {
    echo "!!! ABORTING !!!";pause;exit    
}