从 powershell 执行 msbuild 任务



我正在关注这个博客:http://sedodream.com/2010/04/26/ConfigTransformationsOutsideOfWebAppBuilds.aspx

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

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

当我尝试时,我收到此错误

Powershell:

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

结果

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

我也试过:

Invoke-Expression msbuild transformCommand.proj /t:Demo

有结果

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

小提示:这是我第一次使用Powershell。

TransformCommand.proj

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

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

编辑:

$a = "PathtransformCommand.proj /t:Demo"
#Invoke-Expression $msbuild $a
Start-Process -FilePath "C:WindowsMicrosoft.NETFrameworkv4.0.30319msbuild.exe" -ArgumentList $a | Write-Host 

这实际上可以满足我所需的一切。

不过,如果有办法做到这一点更"独立"或更好的方法,请发布它。

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

$msbuild = "C:WindowsMicrosoft.NETFrameworkv4.0.30319msbuild.exe"

我最终做了以下操作来构建具有多个参数的解决方案

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

Developer Command Prompt for VS... %PATH%的所有差异添加到 powershell,那么您可以在 powershell 中运行msbuild,就像在命令提示符下一样,一切似乎都正常。您还需要复制变量%VCInstallDir%

这是我为了解差异而编写的Powershell函数。

function Find-Path-Differences {
    $defaultPath = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
    $env:path.split(';') | Sort-Object -Unique | Where-Object { $defaultPath.split(';') -notcontains $_ }  | Foreach-Object { "`$env:path += `";$_`"`r" } | echo
    echo "function VCInstallDir {return `"$VCInstallDir`"}`r"
}

如果您打开 powershell 并键入 echo $profile,这将为你提供一个文件路径。创建或打开文件,然后将函数粘贴到其中,当 Powershell 启动时,该函数将可用。然后在Developer Command Prompt ...中运行以下命令:

powershell Find-Path-Differences

然后会弹出一堆这样的行:

$env:path += ";C:Program Files (x86)Microsoft Visual Studio2017Enterprise\MSBuild15.0bin"
...
$env:path += ";C:WindowsMicrosoft.NETFrameworkv4.0.30319"

将这些行从echo $profile复制并粘贴到文件中,然后打开一个新的PowerShell窗口,现在可以在PowerShell中使用msbuild

最新更新