views:

290

answers:

1

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

Any help would be greatly appreciated.

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
+1  A: 

Have you tried

 .\MyScript.ps1 2>&1 | tee -filePath c:\results.txt

2>&1 should be what you're looking for.

David Gladfelter
This is very close. I added the script I am using to test my desired functionality to the question. It is able to get the standard output and errors but not warnings. Any other ideas?
smaclell
You don't have a lot of good options for that. Read the cmdlet help topics for WRite-Warning and Write-Error and you'll see they're fundamentally different. Write-Warning writes to the host, Write-Error writes to the error output stream. You can redirect streams, but writing to the host goes to the host. Your only alternative is the WarningVariable, etc. common parameters. You should be able to make all warning/error/output data redirect to a variable using this. See the about_CommonParameters help topic for more info.
David Gladfelter
BTW, looks like you're not the first to run into this. I guy I used to work with has a good description of the problem here: http://keithhill.spaces.live.com/blog/cns!5A8D2641E0963A97!6926.entry
David Gladfelter
correction: _a_ guy I used to work with.
David Gladfelter
The solution Kieth used should work for us as a temporary solution. It seems like I need to rethink how we log within our cmdlets. Thank you for your help.
smaclell