2011-04-27 159 views
4

夥計 我需要將命令輸出重定向到2個文件,在一個文件中重定向stdout流, 到另一個文件重定向stderr流。 是否可以在cmd中執行PowerShell?redirect命令輸出

回答

4

對於PowerShell的

讓我們說你有my.ps1象下面這樣:

"output" 
Write-Error "error output" 
exit 1 

你可以這樣做:

.\my.ps1 2>stderr.txt | Tee-Object -file stdout.txt 

你在相應的文件得到輸出和錯誤。

更多關於三通對象:

http://technet.microsoft.com/en-us/library/dd347705.aspx

更多關於捕捉所有流:

https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=297055&SiteID=99

+0

謝謝您的回答,這是正確的,但它不是我發現。我正在編寫通過「WScript.shell」執行hg(Mercurial)的C#程序,我如何將輸出從那裏重定向到2個文件stdout和stderr? – Dzmitry 2011-04-28 08:21:32

+0

原來的問題有點不清楚。您是否正在尋找:[Windows腳本主機(WSH)輸出重定向](http://support.microsoft.com/kb/278411)? – 2011-04-28 09:23:35

1

在PowerShell中,你可以使用衆所周知的重定向操作符>>>2>2>>重定向標準輸出和錯誤。

首先確保設置:

$erroractionpreference.value__=1

然後使用重定向。

例子:

ls C:\ 2> stderror.txt > stdoutput.txt # write output on stdoutput.txt

ls foo 2> stderror.txt > stdoutput.txt # write output on stderror.txt unless foo exists