Powershell脚本中的MSBuild - 我如何知道构建是否成功?

时间:2022-03-22 16:23:18

I am writting a build script with Powershell. The scripts performs various operations such as getting the latest source code off the SVN, backups, etc., and builds the solution using MSBuild:

我正在使用Powershell编写构建脚本。这些脚本执行各种操作,例如从SVN获取最新的源代码,备份等,并使用MSBuild构建解决方案:

cmd /c C:\WINDOWS\Microsoft.NET\Framework\v3.5\msbuild.exe "C:\Dev\Path\MyProjct.sln" /p:Configuration=Release 

After this instruction, I only want to execute the rest of the script if the compilation succeeded. How can I check this?

在这条指令之后,如果编译成功,我只想执行其余的脚本。我怎么检查这个?

The project is a web project, so it's not so easy to check for an output, but I would guess some variables would contain the compilation result. Also since I call msbuild with cmd /c, am I going to be able to access these variables?

该项目是一个Web项目,因此检查输出并不容易,但我猜一些变量将包含编译结果。此外,因为我用cmd / c调用msbuild,我能够访问这些变量吗?

2 个解决方案

#1


38  

Check the value of $LastExitCode right after the call to MSBUILD. If it is 0, then MSBUILD succeeded, otherwise it failed.

在调用MSBUILD后立即检查$ LastExitCode的值。如果为0,则MSBUILD成功,否则失败。

BTW there is no need to use cmd /c. Just call MSBUILD.exe directly. We do that in PowerShell build scripts all the time.

顺便说一句,没有必要使用cmd / c。只需直接调用MSBUILD.exe即可。我们一直在PowerShell构建脚本中这样做。

#2


10  

To just check for success/failure, use the automatic variable $?.

要检查成功/失败,请使用自动变量$?。

PS> help about_Automatic_Variables


    $?
       Contains the execution status of the last operation. It contains
    TRUE if the last operation succeeded and FALSE if it failed.

for example:

例如:

msbuild
if (! $?) { throw "msbuild failed" }

#1


38  

Check the value of $LastExitCode right after the call to MSBUILD. If it is 0, then MSBUILD succeeded, otherwise it failed.

在调用MSBUILD后立即检查$ LastExitCode的值。如果为0,则MSBUILD成功,否则失败。

BTW there is no need to use cmd /c. Just call MSBUILD.exe directly. We do that in PowerShell build scripts all the time.

顺便说一句,没有必要使用cmd / c。只需直接调用MSBUILD.exe即可。我们一直在PowerShell构建脚本中这样做。

#2


10  

To just check for success/failure, use the automatic variable $?.

要检查成功/失败,请使用自动变量$?。

PS> help about_Automatic_Variables


    $?
       Contains the execution status of the last operation. It contains
    TRUE if the last operation succeeded and FALSE if it failed.

for example:

例如:

msbuild
if (! $?) { throw "msbuild failed" }