Hi,
I was trying to search a string in logs that will update continuously in every second.
So i wrote the code to search the string if the string found in log it will triger an email.
and created the tasks in task scheduler to run the script in every 5 min.
But the issue is that if script found the “string” in log it will trigger mail in every 5 min continuoisly beause script is checking whole log file everytime.
Can you please suggest me is this possible to point last script checked the logs till the point and next time script will run and do not check whole log file it will check only from last script done.
$string = gci -path c:\folder\lofile.log | sort {$_.LastWriteTime} | select -last 1 | select-string -pattern “string”
#####################################################################
$fromaddress = “@gmail.com"
$toaddress = "@gmail.com”
$Subject = “testing”
$body = "Hi Team`
We got an error " +
$string +
“Please check”
$attachment = “c:\folder\lofile.log”
$smtpserver = “smtp.gmail.com”
####################################
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)