写主机输出未被重定向到logfile

时间:2013-06-25 09:42:12

标签: windows powershell powershell-v2.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

如果有人可以帮助我获取上述代码以便将日志文件通过电子邮件发送为附件,我将不胜感激。

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:4)

不喜欢这个功能我编写了一个函数来执行此操作,日志消息设置为仍然可以通过控制台显示:

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

Write-Log "..."
相关问题