2013-06-25 127 views
0

我已經寫了一個PowerShell來查找日誌文件中的模式,然後將它們重定向到日誌文件。 我想爲每個服務器設置標頭,因此編寫如下代碼:寫主機輸出沒有被重定向到日誌文件

但是頭文件沒有重定向到日誌文件。 請任何建議。

代碼如下所述:

$date_value=Get-Date -Format ddMMM 
    #$file_path="D:\Temp\Arun\mu.log" 
    $file_path1="\\server1\d$\Temp\Arun\abc.log" 
    $file_path2="\\server2\d$\Temp\Arun\abc.log" 
    $file_path3="\\server3\d$\Temp\Arun\abc.log" 
    # Set the match patterns in the three clusters 
    $match_pattern1=select-string -pattern $date_value $file_path1 
    $match_pattern2=select-string -pattern $date_value $file_path2 
    $match_pattern3=select-string -pattern $date_value $file_path3 
    $LogDir="D:\temp\arun\Log\" 
    $DTS=Get-Date -Format "yyyy_MM_dd_HH_mm_ss" 
    $LogFile = $LogDir+"fetch_data."+$DTS+".log" 
    $OldLog = $LogFile+".old" 
    Write-Host "**********************************************************" >> $LogFile 
    Write-Host "Checking log for today's entries in SERVER1" >> $LogFile 
    Write-Host "**********************************************************" >> $LogFile 
    $match_pattern1 >> $LogFile 
    Write-Host "**********************************************************" >> $LogFile 
    Write-Host "Checking log for today's entries in SERVER2" >> $LogFile 
    Write-Host "**********************************************************" >> $LogFile 
    $match_pattern2 >> $LogFile 
    Write-Host "**********************************************************" >> $LogFile 
    Write-Host "Checking log for today's entries in SERVER3" >> $LogFile 
    Write-Host "**********************************************************" >> $LogFile 
    $match_pattern3 >> $LogFile 

我也希望如果有人能幫助我在得到另外一個代碼的上方,作爲電子郵件的附件該日誌文件。

+4

只是將其刪除!或者使用'write-output'代替。 –

+0

您是否考慮過使用[Start-Transcript](http://ss64.com/ps/start-transcript.html)/ [Stop-Transcript](http:// ss64 .com/ps/stop-transcript.html)cmdlet取而代之? –

回答

4

Write-Host寫入控制檯而不是管道。檢查Out-FileAdd-Content cmdlet是否向文件添加內容。有關如何發送電子郵件,請參閱Send-MailMessage cmdlet的幫助。

4

不喜歡這個功能,我寫了一個函數來做到這一點,日誌信息設爲仍可通過控制檯可見:

FUNCTION Write-Log ($message) { 
Write-Host $message 
$message | Out-File $LogFile -Append 
} 

Write-Log "..." 
相關問題