代码之家  ›  专栏  ›  技术社区  ›  smaclell

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

  •  26
  • smaclell  · 技术社区  · 14 年前

    .\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 
    

    我希望我能用这个文件来检查输出/错误。

    这是我当前的测试脚本。所期望的结果是可以看到所有三条消息。

    function Test-Error 
    {
        echo "echo"
        Write-Warning "warning"
        Write-Error "error"       
    }
    
    Test-Error 2>&1 | tee -filePath c:\results.txt
    
    3 回复  |  直到 7 年前
        1
  •  25
  •   JasonMArcher TWE    7 年前

    你试过:

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

    2>&1

    注意:这个答案在PowerShell 1.0和2.0中非常有效,但在PowerShell 3.0和更高版本中只捕获标准输出和错误。

        2
  •  7
  •   JasonMArcher TWE    7 年前

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

    $output = try{your_command *>&1}catch{$_}
    

    有了它,您可以捕获通过尝试使用 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>
    

    向现有命令传递无效参数时,它会捕获异常:

    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
    

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

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

    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
    
        3
  •  3
  •   Ramakant Dadhichi    5 年前

    默认情况下,只有成功的数据流被传递到输出文件。要指示错误和警告,必须添加以下内容:

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

    Powershell redirection operators.

        4
  •  2
  •   jdgregson Mark    7 年前

    我无法在同一个文件中同时获得错误和结果。一个对我有用的解决方法:

    .\MyScript.ps1 2> C:\errors.txt | tee -filePath C:\results.txt

    我工作得更深入 Start-Transcript Stop-Transcript 在我的模式下,它捕捉一切,它工作!