Conor
Conor

Reputation: 593

Powershell script completes but exits with error code 1

$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.

Upvotes: 5

Views: 62587

Answers (2)

Nefariousity
Nefariousity

Reputation: 127

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

exit 0

Credit to Maximilian Burszley's comment in the comments

Upvotes: 6

Rich Moss
Rich Moss

Reputation: 2384

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.

Upvotes: 4

Related Questions