I have a PowerShell script that outputs to stdout and stderr. I'd like to tee those to a couple log files at the same time.
What's the best way to do this?
A couple things I've considered:
- Wrap write-error and write-output with a function that does the tee.
- I don't like this much because (a) I lose my easy ability to just put a value on a line and have it automatically output and (b) I'd have to wrap every place that I'm calling something, including native exe's, that may have output and wrap those as well.
- Wrap in an outer script that does the tee.
- This is a little better but now I have the issue of needing to duplicate the
param()block from the inner script in the outer script. I could move the param() block entirely but I also want to call the inner script sometimes for testing purposes (i.e. not get the tee). I suppose I could make that an optional behavior in the outer script..
- This is a little better but now I have the issue of needing to duplicate the
I remember back when I used to write some perl there was a convenient way to redirect all stdout/err output globally, via a hook. Is something like this possible with PowerShell?