2012-08-29 98 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

相關問題