Neil McLoughlin
Neil McLoughlin

Reputation: 19

Scan error logs using powershell for errors in the last 24 hours

I am after a powershell script which will scan through a load of log files in a certain directory and then output all the lines which have an error on them to a seperate text file or even better a html file. Any ideas how i could achieve this?

Thanks in advance!

Cheers

Neil

Upvotes: 1

Views: 5757

Answers (2)

Kalpesh
Kalpesh

Reputation: 11

$files = Get-ChildItem "D:\TEMP\*.log"
foreach ($file in $files) 
{
    $count=Get-Content $file.fullName | Select-String "Fail|error|warning" 
    if(@($count).count -gt 0) 
    {
        $msg = new-object Net.Mail.MailMessage
        $msg.From = "[email protected]"
        $msg.To.Add("[email protected],[email protected]")
        $msg.Subject = "ERROR: FTP"
        $msg.Body = "The attached SFTP Log has errors. Please look ASAP and Fix."
        $smtpServer = "smtp.office365.com" 
        $smtp = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
        $smtp.EnableSsl = $true 
        $smtp.Credentials = New-Object System.Net.NetworkCredential("[email protected]", "XXXXXXXXX"); 
        $att = new-object Net.Mail.Attachment($file.fullName)
        $msg.Attachments.Add($att)
        $smtp.Send($msg)
        $att.Dispose()
        write-host $file.fullName
    }
}

Upvotes: 1

Shay Levy
Shay Levy

Reputation: 126722

This will get all lines that have the word 'error' in them and export the lines to a text file:

Get-ChildItem <path> -Filter *.log | 
Select-String -Pattern error -AllMatches -SimpleMatch | 
Foreach-Object {$_.Line} | 
Out-File .\errors.log

Upvotes: 0

Related Questions