2010-11-12 54 views
3

我有一個PowerShell腳本,我想寫入控制檯並通過一次調用寫入日誌文件。如何在Powershell中使用CR LF換行在一次調用中寫入控制檯和日誌文件

我這樣做...

Start-Transcript -Path $TargetDir\Log.log 
Write-Host "Stuff" 

...偉大的工程,但它產生的換行符是LF,這意味着我的日誌在地球上的每一個文本編輯器,很好看,除了記事本。

下面是我對這個...

function global:Write-Notepad 
(
    [string] $Message, 
    [string] $ForegroundColor = 'Gray' 
) 
{ 
    Write-Host "$Message`r" -ForegroundColor $ForegroundColor 
} 

...它寫入CR到每封郵件的結尾,但它似乎並沒有寫出這樣的行...

&$ACommand | Write-Notepad 

我不確定管路操作員期望的語法,但我非常感謝幫助。

回答

3

這是我看中的解決方案......

# This method adds a CR character before the newline that Write-Host generates. 
# This is necesary, because notepad is the only text editor in the world that 
# doesn't recognize LF newlines, but needs CR LF newlines. 
function global:Write-Notepad 
(
    [string] $Message, 
    [string] $ForegroundColor = 'Gray' 
) 
{ 
    Process 
    { 
     if($_){ Write-Host "$_`r" } 
    } 
    End 
    { 
     if($Message){ Write-Host "$Message`r" -ForegroundColor $ForegroundColor } 
    } 
} 
5

試試這個:

& $ACommand | Tee-Object -FilePath $TargetDir\Log.log | Write-Host 

三通對象將在同一時間發送管道對象以文件或變量,副本輸出。

相關問題