2

I'm using a powershell script to execute a terraform command, and when I have it inline with my code, I get terraform info printed to the host, which is what I want.

Once I wrapped the command in a function, I'm not getting the terraform output (only get the error response if there is one).

Is there a way to get the output to return from the function in real time (so I'm not sitting around wondering what is happening)?

EX:

Terraform init

vs

Function Example 
{
    PARAM($dirPath)
    cd $dirPath
    terraform init
}
Example -dirPath "C:\Test\"

Additionally, is there a way to actually capture if there is an error in the terraform response and halt the script? I've already set erroractionpreference = 'Stop' but that doesn't seem to capture the terraform error.

Xanderu
  • 747
  • 1
  • 8
  • 30

1 Answers1

1

As requested, the comment as answer.

To capture output from an external application, you may need to redirect the error output stream to the success output stream in order to receive them both.
To do that, you can use the following in your fuction:

terraform init 2>&1

The 2> redirects the error output stream (stderr) to the success stream (stdout) with &1 and by doing so, you will merge both streams.

All of this is explained MUCH better than I can in the answers to this question by manojlds and especially mklement0

Theo
  • 57,719
  • 8
  • 24
  • 41