0

I have written a powershell to find a pattern in log files and then redirect them to a log file. I wanted to have headers for each of the server and hence wrote the code as below:

But the header files are not redirected to the log file. Any suggestion please.

Code as mentioned below:

    $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

I would also appreciate if anyone can help me in getting a code in addition to the above so that log file emailed as an attachment.

4

2 回答 2

4

不喜欢这个功能我写了一个函数来做到这一点,日志消息设置为仍然可以通过你的控制台看到:

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

Write-Log "..."
于 2013-06-25T12:31:11.527 回答
4

Write-Host写入控制台而不是管道。检查Out-FileAdd-Contentcmdlet 以向文件添加内容。Send-MailMessage有关如何发送电子邮件,请参阅 cmdlet 的帮助。

于 2013-06-25T10:12:31.330 回答