Missing error output from powershell script

We’re running an inline powershell script step with a script like this:

./terraform.exe plan

Because of an issue with our Terraform state, that command is failing. But when it fails I don’t see the command output in Octopus’s logs:

10:26:49   Error    |       NotSpecified:
10:26:49   Error    |       At D:\Octopus\Work\20200401142641-219175-22221\Script.ps1:28 char:1
10:26:49   Error    |       + ./terraform.exe plan
10:26:49   Error    |       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10:26:49   Error    |       at <ScriptBlock>, D:\Octopus\Work\20200401142641-219175-22221\Script.ps1: line 28
10:26:49   Error    |       at <ScriptBlock>, D:\Octopus\Work\20200401142641-219175-22221\Bootstrap.Script.ps1: line 2402
10:26:49   Error    |       at <ScriptBlock>, <No file>: line 1
10:26:49   Error    |       at <ScriptBlock>, <No file>: line 1
10:26:50   Verbose  |       Process C:\Windows\system32\WindowsPowershell\v1.0\PowerShell.exe in D:\Octopus\Work\20200401142641-219175-22221 exited with code 1
10:26:50   Verbose  |       Updating manifest with output variables
10:26:50   Verbose  |       Updating manifest with action evaluated variables
10:26:50   Fatal    |       The remote script failed with exit code 1
10:26:50   Fatal    |       The action Terraform plan on the Octopus Server failed

If I RDP to the worker and run the same command I get much more useful output:

Error loading state: state snapshot was created by Terraform v0.12.23, which is newer than current v0.12.21; upgrade to Terraform v0.12.23 or greater to work with this state

If I manually run the command with outputs redirected I see that the message was sent to stderr:

./terraform.exe plan 2>stderr >stdout 3>stdwarn
# stdout contains normal output
# stderr contains the error message about state snapshot
# stdwarn is empty, as expected

My worker is running .net 4.8:

(Get-ItemProperty "HKLM:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full").Version
4.8.03761

So Octopus is terminating the script immediately when the error occurs, which is fine, but it doesn’t seem to capture the error output. How can I fix that?

Hi,

I was about to suggest that you may be encountering this issue, but then noticed you said you are running .NET 4.8. This was an example of a similar problem, but they were not on .NET 4.8 (perhaps they may be a clue in that thread anyway).

Are you running Octopus Cloud or self-hosted? And if self-hosted, which version?

The best work-around I can suggest in the mean-time is to redirect errors when invoking terraform.exe, and use it’s return code to decide whether to fail the step.

We are running self-hosted, 2019.12.0.

I updated my script to include $ErrorActionPreference = 'Continue'.

I then also check the exit code from the command to see if it succeeded or not. In this way, Octopus captures the error output like I wanted, even though I’m doing nothing to redirect it.