In PowerShell, I'm unable to write error stream output to a variable.
Running the command without saving the output to a variable will result in output from any stream being visible, but alas, I require the output to be written to a variable.
& terraform $action $arguments
Initially I started using this. However, only the success stream is written to the variable (as expected).
$res = & terraform $action $arguments
So I consulted the docs for about_Redirection, but the trouble is, when I redirect the error stream (or all streams) to the success stream, I still only see the success stream written to the variable. I've made several attempts, all of which failed.
$res = & terraform $action $arguments 2>&1
$res = & terraform $action $arguments *>&1
$res = & terraform $action $arguments *>&1 | ForEach-Object { $_.ToString() }
However, of if I redirect the error stream to a file then the stream is written as expected.
$res = & terraform $action $arguments 2>> terraform-errors.log
How can I write the output from all streams to a variable?