2012-08-29 96 views
2

我目前有一个脚本执行如下: 。\ script.ps1“param1”“param2”2> & 1 | tee -filePath buildlog.txt.ps1脚本中的PowerShell tee-object?

我无法找到一种方法来执行以下操作..记录到脚本中封装的控制台和文件。 。 \ script.ps1“参数1”“参数2”

这是我在做尝试:

powershelltest.ps1

param([string]$paramOne, [string]$paramTwo) 

function DoWork() 
{ 
Write-Host '3' 
} 

function WriteLogFile() 
{ 
    DoWork 
# The following would not be captured by Start-Transcript & Stop-Transcript 
# program.exe .... 
Write-Host '4' 
} 

function CollectorFunction() 
{ 
    Write-Host '2' 
WriteLogFile; 
    Write-Host '5' 
} 

Write-Host '1' 
CollectorFunction 2>&1 | tee -filePath c:\log.foo 

回答

4

如果你想要写日志文件不使用Write-Host。使用Write-Output或因为Write-Output是默认e.g不使用Write-*都:

Write-Output 'hello' > foo.txt 

等同于:

'hello' > foo.txt 

Write-Output将输出发送到标准输出流(即1)。使用Write-Error将输出发送到错误流(即2)。这两个流可以重定向。 Write-Host或多或少直接写入主机UI,完全绕过输出流。

在PowerShell中V3,你也可以重定向以下流:

The Windows PowerShell redirection operators use the following characters 
to represent each output type: 
    * All output 
    1 Success output 
    2 Errors 
    3 Warning messages 
    4 Verbose output 
    5 Debug messages 
1

我会用Start-Transcript。唯一需要注意的是它不捕获标准输出,仅输出Write-Host。所以,你只需要管从传统的命令行应用程序的输出Write-Host

Start-Transcript -Path C:\logs\mylog.log 
program.exe | Write-Host 
+0

启动成绩单不赶标准输出非内置命令的,就像你说的;但是我看到它从*版本4.0开始捕获Powershell命令的标准输出(例如捕获'Write-Out') – jpaugh

相关问题