Reputation: 4658
I am trying to write the entire output (errors included) of an executing script to the console and a file at the same time. I have tried several different options:
.\MyScript.ps1 | tee -filePath C:\results.txt # only the output to the file
.\MyScript.ps1 2> C:\results.txt # only the errors to the file and not the console
.\MyScript.ps1 > C:\results.txt # only the output to the file and not the console
My hope was that I could use the file to review the output/errors.
EDIT:
This is my current test script. The desired results is that all three messages can be seen.
function Test-Error
{
echo "echo"
Write-Warning "warning"
Write-Error "error"
}
Test-Error 2>&1 | tee -filePath c:\results.txt
Upvotes: 33
Views: 146729
Reputation: 403
By default only the Success stream of data is passed on to the Output file. To direct errors and warnings you will have to add something like this :
your script 3>&1 2>&1 | Out-file log.txt
Upvotes: 8
Reputation: 4213
Have you tried:
.\MyScript.ps1 2>&1 | tee -filePath c:\results.txt
2>&1
is what you're looking for
Note: This answer works great in PowerShell 1.0 and 2.0, but will capture ONLY standard output and errors in PowerShell 3.0 and later.
Upvotes: 29
Reputation: 1587
I wasn't satisfied with any answer I was finding, so I mixed a few and came up with this (in PowerShell 3.0+):
$output = try{your_command *>&1}catch{$_}
With this you can capture all errors and output that are generated by trying to use your_command
.
It catches exceptions when you use a non-existent command:
PS C:\Users\jdgregson> $output = try{your_command *>&1}catch{$_}
PS C:\Users\jdgregson> echo $output
your_command : The term 'your_command' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.
At line:1 char:15
+ $output = try{your_command 2>&1}catch{$_}
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (your_command:String) [], Comman
dNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\jdgregson>
It catches exceptions when you pass invalid arguments to an existing command:
PS C:\Users\jdgregson> $output = try{cat C:\invalid-path.txt *>&1}catch{$_}
PS C:\Users\jdgregson> echo $output
cat : Cannot find path 'C:\invalid-path.txt' because it does not exist.
At line:1 char:15
+ $output = try{cat C:\invalid-path.txt 2>&1}catch{$_}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\invalid-path.txt:String) [Ge
t-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCo
ntentCommand
And it catches the output if there was no problem with your command at all:
PS C:\Users\jdgregson> $output = try{cat C:\valid-path.txt *>&1}catch{$_}
PS C:\Users\jdgregson> echo $output
this file is really here
It works for your example too:
PS C:\Users\jdgregson> $output = try{Test-Error *>&1}catch{$_}
PS C:\Users\jdgregson> echo $output
echo
WARNING: warning
Test-Error : error
At line:1 char:15
+ $output = try{Test-Error *>&1}catch{$_}
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorExcep
tion
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorExceptio
n,Test-Error
Upvotes: 11
Reputation: 39
I couldn't get both errors and results in the same file. A workaround that worked for me:
.\MyScript.ps1 2> C:\errors.txt | tee -filePath C:\results.txt
Update:
I have worked further and I used Start-Transcript
and Stop-Transcript
in my mode to capture everything and it worked!
Upvotes: 3