问题
I want to see the output of the command in the console and save it in a file so I have these two options:
When I use command | tee output.txt
somehow it generates no output file at all but it works as usual in the console.
When I use command 2>&1 | tee output.txt
it generates a fine output file but the text in the console appears in red.
Is there any way to either fix the first option or let the text appear as usual in the second one? I am using Windows PowerShell (Windows 10) and the Programm I am using this for is liquibase 3.5.5. just for the case that this is important.
回答1:
In PowerShell, redirecting stderr lines from an external program to PowerShell's success stream via 2>&1
wraps those lines in [System.Management.Automation.ErrorRecord]
instances, which is why you're seeing the red output (without the redirection, the stderr lines are passed through to the console, without coloring).
A simple workaround is to convert these objects to strings explicitly, which outputs the original lines (PSv3+ syntax; built-in alias %
for ForEach-Object
used for brevity):
... 2>&1 | % ToString | Tee-Object output.txt
回答2:
If you are in DOS then you can leverage powershell (from DOS) and use tee-object do more or less a tee like in Linux.
C:> powershell some_command ^| tee-object -FilePath some_outputfile
The '^' escapes the pipe so that the pipe applies to powershell and not DOS.
Powershell and tee-object should come with Windows as standard so nothing to install!
来源:https://stackoverflow.com/questions/52970939/right-usage-of-21-tee-or-tee-with-powershell