5
$myValue= (.\command.exe arguments | select -first 1)

I run the above code in my Azure Devops pipeline, $myValue contains the value I'm expecting, but I get:

##[error]PowerShell exited with code '1'.

returned once my task completes... Does anyone have any idea why? I don't know if this is a syntax problem, or if my command.exe does something strange to affect the exit code.

6
  • Are you sure that .\command.exe arguments doesn't cause an error? move it to a try{ ... } catch { Write-Error $_ } block to see what happens Commented May 15, 2019 at 17:20
  • 4
    I don't see you doing exit 0 anywhere in your snippet. Commented May 15, 2019 at 18:12
  • @Moerwald powershell's exception handling is exceptionally funky with external executables. That won't accomplish anything. Commented May 15, 2019 at 18:13
  • can you share you whole script? Commented May 15, 2019 at 18:13
  • 1
    thanks - all useful comments. Sorry I couldn't share the full code, but thanks for giving useful feedback given the limited info - guess it is just powershell being buggy as I tried the try catch loop and my .exe runs fine Commented May 16, 2019 at 9:07

2 Answers 2

6

Putting this at the end of my script prevented the error message:

exit 0

Credit to Maximilian Burszley's comment in the comments

Sign up to request clarification or add additional context in comments.

Comments

4

Without seeing the whole script I will guess that this is due to a known issue with Powershell: Select -First populates the ErrorVariable even on success. It's reproducible with this code:

PS > 1..2 | Select -First 2 -ErrorVariable X; $X | select *
 1
 2

RequestingCommandProcessor : Select-Object
 Message : System error.
 Data : {}
 InnerException :
 TargetSite : Void ProcessRecord()
 StackTrace : at Microsoft.PowerShell.Commands.SelectObjectCommand.ProcessRecord()
 at System.Management.Automation.CommandProcessor.ProcessRecord()
 HelpLink :
 Source : Microsoft.PowerShell.Commands.Utility
 HResult : -2146233087

You can ignore any errors with $Error.HResult -eq -2146233087 as a workaround.

1 Comment

Github indicates it was fixed in Powershell 7.0.0 Preview 6 as Issue 10840.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.