从powershell执行msbuild任务

Joc*_*ery 5 msbuild powershell-2.0

我关注此博客:http://sedodream.com/2010/04/26/ConfigTransformationsOutsideOfWebAppBuilds.aspx

它在这个问题中被链接为答案:Web.Config在Microsoft MSBuild之外进行转换?

每个步骤都按照描述工作,但我想从powershell脚本调用转换.我想在powershell中执行此命令msbuild trans.proj /t:Demo 我发现一些论坛说我应该使用invoke-expression

当我尝试时,我得到这个错误

电源外壳:

Invoke-Expression $msbuild transformCommand.proj /t:Demo
Run Code Online (Sandbox Code Playgroud)

结果

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
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

Invoke-Expression msbuild transformCommand.proj /t:Demo
Run Code Online (Sandbox Code Playgroud)

结果

 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
Run Code Online (Sandbox Code Playgroud)

小记:这是我第一次使用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>
Run Code Online (Sandbox Code Playgroud)

我的问题是:这可能吗?怎么可能呢?

编辑:

$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 
Run Code Online (Sandbox Code Playgroud)

这实际上做了我需要的一切......

如果有办法更多"独立"或更好的方式,请发布它.

ota*_*aku 8

在调用Invoke-Expression $ msbuild之前,请确保按如下所示定义PowerShell变量$ msbuild

$msbuild = "C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe"
Run Code Online (Sandbox Code Playgroud)