0

I'm using System Center Orchestrator to run a PowerShell script that does some stuff. The component that executes the script is able to fetch the results of Write-Output and Write-Host but other streams are lost. But I'd really like those other streams to be available, especially the standard error stream.

Is there any way to setup redirection for stderr to stdout?

I know you can do this for individual commands doing 2>&1 but as the script has quite a few lines it would be kind of tedious to append this to every line in the script. I could execute a script within the script but that would lead to some other problems. My guess would be that it should be possible to set this up using C# but I'm not sure what to look for in that case.

Seth
  • 1,215
  • 15
  • 35
  • 1
    You can get those streams if you wrap your script into another call of `powershell.exe` via `start-process` cmdlet with supplied `-redirectstandarderror` parameter. Then your wrapper script should parse all the streams. I wonder though, what problems would arise if you run a powershell within a powershell and wait for it to finish? – Vesper May 03 '17 at 09:54
  • PowerShell within Orchestrator is enough of a headache without nesting (for once the native activity runs only 32 bit). The activity that executes the script won't fail if you start a nested PowerShell. As you don't get any console output by default you would be left wondering what went wrong with your script. On the other hand you have Integration packs that do get you the output but lack the ability to directly map variables to items you want to put on the data bus. It's not an issue with PowerShell but rather the interaction between PowerShell and the component that does the execution. – Seth May 03 '17 at 10:59
  • Try Start-Transcript / Stop-Transcript. Already answered at [redirect PowerShell](https://stackoverflow.com/questions/1215260/how-to-redirect-the-output-of-a-powershell-to-a-file-during-its-execution) – un-CharlieH Sep 20 '19 at 23:18
  • @un-CharlieH It's complicated. Thanks for bringing it up. It's been some time and as far as I can remember it didn't work because Orchestrator ... does interesting things behind the scenes or rather is just non interactive. See [Start-transcript in a background process](https://stackoverflow.com/questions/16710613/start-transcript-in-a-background-process). I've accepted the supplied answer though it's not a perfect fit. – Seth Sep 23 '19 at 05:20

1 Answers1

0

You can get those streams if you wrap your script into another call of powershell.exe via start-process cmdlet with supplied -redirectstandarderror parameter. Then your wrapper script should parse all the streams.

If you are unable to call a nested Powershell process due to say some local variables that are set by the orchestrator, you can use Write-Output $error as a means to log all (up to $MaximumErrorCount) errors into the standard output. Not exactly the error stream, but a start. Also this will still get populated if your $ErrorActionPreference is set to "SilentlyContinue".

Vesper
  • 18,599
  • 6
  • 39
  • 61