将错误和输出写入文本文件和控制台

sma*_*ell 27 powershell redirect

我试图将执行脚本的整个输出(包括错误)同时写入控制台和文件.我尝试了几种不同的选择:

.\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 
Run Code Online (Sandbox Code Playgroud)

我希望我可以使用该文件来查看输出/错误.

编辑:

这是我目前的测试脚本.期望的结果是可以看到所有三个消息.

function Test-Error 
{
    echo "echo"
    Write-Warning "warning"
    Write-Error "error"       
}

Test-Error 2>&1 | tee -filePath c:\results.txt
Run Code Online (Sandbox Code Playgroud)

Dav*_*ter 23

你有没有尝试过:

 .\MyScript.ps1 2>&1 | tee -filePath c:\results.txt
Run Code Online (Sandbox Code Playgroud)

2>&1 是你在找什么

注意:此答案在PowerShell 1.0和2.0中运行良好,但在PowerShell 3.0及更高版本中仅捕获标准输出和错误.

  • 从PowerShell 3开始,您可以:`.\ MyScript.ps1*>&1 | tee -filePath c:\ results.txt`,这意味着重定向所有输出流,因为存在警告,调试和详细.见[about_Redirection](https://technet.microsoft.com/en-us/library/hh847746.aspx) (7认同)
  • 顺便说一句,看起来你不是第一个遇到这种情况的人.我以前和我一起工作的人对问题有一个很好的描述:http://keithhill.spaces.live.com/blog/cns!5A8D2641E0963A97!6926.entry (2认同)

小智 7

默认情况下,仅成功数据流会传递到输出文件。要引导错误和警告,您必须添加如下内容:

你的脚本 3>&1 2>&1 | 输出文件log.txt

Powershell 重定向运算符。


jdg*_*son 6

我对找到的任何答案都不满意,所以我混合了几个并想出了这个(在PowerShell 3.0+中):

$output = try{your_command *>&1}catch{$_}
Run Code Online (Sandbox Code Playgroud)

通过此,您可以捕获尝试使用生成的所有错误和输出your_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>
Run Code Online (Sandbox Code Playgroud)

当您将无效参数传递给现有命令时,它会捕获异常:

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
Run Code Online (Sandbox Code Playgroud)

如果您的命令没有任何问题,它会捕获输出:

PS C:\Users\jdgregson> $output = try{cat C:\valid-path.txt *>&1}catch{$_}
PS C:\Users\jdgregson> echo $output
this file is really here
Run Code Online (Sandbox Code Playgroud)

它也适用于你的例子:

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
Run Code Online (Sandbox Code Playgroud)