将错误和输出写入文本文件和控制台 [英] Writing errors and output to a text file and Console

查看:56
本文介绍了将错误和输出写入文本文件和控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

推荐答案

您是否尝试过:

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

2>&1 就是你要找的

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

这篇关于将错误和输出写入文本文件和控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆