PowerShell退出代码 - 从MSBuild调用

San*_*eev 12 msbuild powershell

我正在从MSBuild调用PowerShell脚本.MSBuild能够捕获返回的输出,但认为项目已成功构建.

问题是PowerShell的退出代码没有传递给MSBuild中的命令.有人曾经试过这个并且能够向MSBuild抛出退出代码吗?

testmsbuild.proj

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="DesktopBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <ScriptLocation>c:\scripts\test.ps1</ScriptLocation>
  </PropertyGroup>

  <Target Name="AfterDropBuild" >
        <Exec Command="powershell.exe -NoProfile -Noninteractive -command &quot;&amp; { $(ScriptLocation)%3Bexit $LASTEXITCODE }&quot; " >
        <Output TaskParameter="ExitCode" PropertyName="ErrorCode"/>
        </Exec>
   </Target>    

 </Project>
Run Code Online (Sandbox Code Playgroud)

test.ps1(当然这会出错)

function CallFromMSBuild {

   Invoke-command {Powershell.exe C:\a.ps1} -computername $computers
} 
Run Code Online (Sandbox Code Playgroud)

当触发MSBuild项目时,它应该已经发现问题并且构建应该失败(认为构建成功了)

当我从MSBuild打电话时

C:\Scripts>msbuild testmsbuild.proj /t:AfterDropBuild
Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.225]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 4/28/2011 2:46:29 PM.
Project "C:\scripts\testmsbuild.proj" on node 1 (AfterDropBuild target(s)).
AfterDropBuild:
  powershell.exe -NoProfile -Noninteractive -command "& { c:\scripts\test.ps1;e
  xit $LASTEXITCODE }"
  Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argu
  ment is null or empty. Supply an argument that is not null or empty and then tr
  y the command again.
  At C:\install\test.ps1:3 char:58
  +    Invoke-command {Powershell.exe C:\a.ps1} -computername <<<<  $computers
      + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParameterBind
     ingValidationException
      + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
     Shell.Commands.InvokeCommandCommand

Done Building Project "C:\scripts\testmsbuild.proj" (AfterDropBuild target(s)).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.04
Run Code Online (Sandbox Code Playgroud)

Rub*_*ink 15

这个问题是主要搜索引擎的最佳答案.在最好的答案是这样的詹姆斯·科瓦奇(的psake成名-也就是说,他还挺FizzBinned在PowerShell和的MSBuild集成).

总之,在ps1文件中:

  1. 坚持$ErrorActionPreference='Stop'在脚本的顶部,这不是默认值,这是SilentlyContinue(trap接受的答案中的位具有相同的效果,但更加间接和混乱)
  2. 坚持他function exec {...(或使用psake本身)
  3. 包装外部EXE的调用 exec { x.exe }
  4. 不需要做任何明确的exit ...事情

默认错误处理将异常ERRORLEVEL从1 传播为1 powershell.exe myscript.ps1,即在MSBuild中<Exec你不需要做任何欺骗,告诉它忽略退出代码等等(除非你想对特定的退出代码做一些有条件的事情) ,你想要做什么IgnoreExitCode="true",并用<Output元素捕获它)

最后要理解的一点是,在PowerShell中,有一个$?是最后一个表达式的结果(如果你处于ErrorAction='Stop'模式状态,这是不相关的)随着你所做的每一件事而改变,而$ LastExitCode是'DOS'出口系统中触发的最后一个.exe的代码.详细信息 - 请务必阅读评论


man*_*lds 4

添加exit $lastexitcode到test.ps1

评论后:

在 test.ps1 中试试这个:

trap {Write-Host -foreground red $_.Exception.Message; exit 1; continue} Invoke-command   {Powershell.exe C:\a.ps1} -computername $computers
Run Code Online (Sandbox Code Playgroud)

基本上,我不认为 MSBuild 这里有问题。

我尝试了你的错误,尽管 Invoke-Command 失败了,但Invoke-Command它被设置为 0!$lastexitcode你可以通过 cmd 本身来测试这是否有效,echo %errorlevel%看看你是否得到 1。