Octopus step fails when redirecting stderr with 2>&1

I try to run the below PowerShell code snippet in Octopus, but when adding the 2>&1 redirection, it immediately fails. Without the redirection it will show the correct terraform error messages and will execute commands after. My main goal would be to redirect the stderr into a file together with the command output. Any ideas on how this can be solved? (Command is written into the temp.ps1 file, becuse otherwise PS thinks 2>&1 is a terraform flag.)

  $command = 'terraform plan -var-file="dev.tfvars"'  
  Write-Highlight "Running Terraform Plan"
  $command > terraform.ps1
  Write-Host $command 
  .\temp.ps1 2>&1 | tee $filename

HI @k1tt1.k4t4,

Thanks for reaching out and welcome to the forums!
Sorry to hear you’re experiencing issues.

Within Octopus you can redirect errors to different outputs using Service Messages.

Would wrapping your script in your message with Service Messages perhaps allow you to capture the output you require?

Write-Host "##octopus[stderr-progress]"
Start-Transcript -Path $filename

$command = 'terraform plan -var-file="dev.tfvars"'  
Write-Highlight "Running Terraform Plan"
$command > terraform.ps1
Write-Host $command

Write-Host "##octopus[stderr-default]"

Using this method, any errors that occur between the service messages would be redirected and will not halt your deployment.

I hope this helps! Please let me know how you get on or if you have any questions at all.

Kind Regards,

Hey Adam,

Unfortunately it only put below listed information into the output file and not the actual output of the terraform plan command. My aim is to redirect the errors coming from the terraform plan command into this file.

Windows PowerShell transcript start
Start time: 20211025092512
Username: xxxxx
RunAs User: xxxxx
Machine: xxxx (Microsoft Windows)
Host Application: C:\Windows\system32\WindowsPowershell\v1.0\PowerShell.exe -NoLogo -NonInteractive -ExecutionPolicy Unrestricted -Command Set-PSDebug -Trace 0;Try {. {. 'E:\Octopus\Work\20211025082458-151334-1616\Bootstrap.Octopus.FunctionAppenderContext.ps1'  if ((test-path variable:global:lastexitcode)) { exit $LastExitCode }};} catch { throw }
Process ID: 7800
PSVersion: 5.1.14393.4583
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14393.4583
BuildVersion: 10.0.14393.4583
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
Transcript started, output file is Output-345-non-prod.ANSI
Running Terraform Plan
Windows PowerShell transcript end
End time: 20211025092512

Hi, again @k1tt1.k4t4,

Thanks for clarifying and sorry for the non-answer.

Would you be able to try changing just one line from your original script, please?

Instead of:
$command = 'terraform plan -var-file="dev.tfvars"'

Can you try this?:
$command = '&{terraform plan -var-file="dev.tfvars"} 2>&1 > $filename'

This way, when the Terraform command runs, the outputs should be redirected to your $filename path.

Please let me know if this helps or if you’re still experiencing issues!

Kind Regards,

This did not help either. What I experience is that it redirects the Success Stream, but not the error to the file. Non of the two streams is visible in the Octopus Console after the redirection, but only the Success stream is visible in the file.

Hi @k1tt1.k4t4,

Sorry to hear that this didn’t help.

Would you be able to send over the process JSON for the project that you’re using within Octopus for this?
This is so I can get a better idea of how everything is configured and better reproduce the issue you’re experiencing.

Kind Regards,

Well, I would, but that does not tell anything about the failing part as it’s a PowerShell step.
The main issue here is:

  1. I run a terraform plan command and there are some lines that go to the stderr
  2. When testing it locally without Octopus being involved, stderr redirection to stdout works and the errors are written into the file together with stdout
  3. When trying to redirect stderr in Octopus, terraform errors are not shown in the file, only the stdout.

This line works locally, but does not do what I ecpect in Octopus:
(Invoke-Expression 'terraform plan -var-file="dev.tfvars" -no-color') 2>&1 | Out-File file.ANSI

Hi there,


I’ll look into this further in order to reproduce this inside Octopus or find a workaround that will allow you to obtain the desired results.

I’ll let you know when I have more information for you.

Thank you for your patience so far!

Kind Regards,

Hi @k1tt1.k4t4,

I’ve hopefully got an answer for you!

As I’m not sure exactly on your implementation, I was able to have the Terraform logs printed to a separate file by setting environment variables on the machine.

Within Octopus, on a ‘Run a Script’ step, I used the following:

$filename = "../TerraformErrors.txt"
terraform plan

This resulted in the trace level logs for the ‘terraform plan’ command being sent to the text file specified in $filename, which is the root of the work folder for the Tentacle in my case.

I hope this helps!

Kind Regards,

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.