PowerShell Exit Code - Call from MSBuild

I am invoking a PowerShell script from MSBuild. MSBuild is capable of capturing the return result, but believes that the project is built successfully.

The problem is that PowerShell exit code is not passed to the MSBuild command. Has anyone tried this before and were able to send exit code to 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> 

test.ps1 (of course, this will be an error)

 function CallFromMSBuild { Invoke-command {Powershell.exe C:\a.ps1} -computername $computers } 

When the MSBuild project started, it should have caught the problem and the assembly should have failed (instead, it is believed that the assembly was successful)

When I Call From 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 
+12
powershell msbuild
Apr 28 '11 at 19:57
source share
2 answers

Add exit $lastexitcode to test.ps1

After the comment:

Try this in test.ps1:

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

Basically, I don't think MSBuild is to blame here.

I tried your wrong Invoke-Command and $lastexitcode was set to 0, although the Invoke-Command command failed! You can check if this works with CMD or not by executing echo %errorlevel% and seeing that you got 1.

+4
Apr 28 2018-11-11T00:
source share

This question is the main answer to the main search engine. the best answer is from James Kovacs (from the glory of singing - i.e., He's kind of FizzBinned in PowerShell and MSBuild integration).

In general, in the ps1 file:

  • Insert $ErrorActionPreference='Stop' at the top of your script so that it is not the default, i.e. SilentlyContinue (the trap bit in the accepted answer has the same effect, but much more indirect and confusing)
  • stick with its function exec {... (or use psake itself)
  • end external exe calls in exec { x.exe }
  • no need to do any explicit exit ... things exit ...

By default, error handling powershell.exe myscript.ps1 exception like ERRORLEVEL from 1 from powershell.exe myscript.ps1 , that is, in MSBuild <Exec you do not need to do any deceptive actions, telling them to ignore exit codes, etc. (if you do not want to do something conditional on a specific exit code in which you want to make IgnoreExitCode="true" and capture it using the <Output element)

Finally, it's important to understand that $? exists in PowerShell $? , which is the result of the last expression (which does not matter if you are in ErrorAction='Stop' mode), which changes with every thing you do, while $ LastExitCode is the DOS exit code for the last .exe run in system. Details here - be sure to read the comments

+15
May 01 '11 at 10:41
source share



All Articles