I wrote a PS script for checking below-listed strings[$string] in my log and send a mail with the relevant logs attachment if one of that pattern matches on that logs.
This script is working as expected, but my challenge is now to check how to exclude already alerted strings. For example:
My log file overwrites every 4 mins or sometimes faster than 4 mins which is based on the call traffic. Since my script is running every 3 mins, sometimes I get numerous alert emails for the already notified error.
So, how to check if that error is new or already alerted before sending a mail to me? ....I am new to Powershell scripting. Thanks in advance!
script:-
~~~~~~~~~~~~~
$string='(Unable to connect to the remote server|A serious|Socket error - cannot connect|Fetch returned HTTP error|"Open error|URL=http")'
$path1="D:\Program Files\AVP\*.txt"
$Target = "D:\Program Files\AVP\Avp_Logs.zip"
if (Select-String -Path $path1 -pattern $string)
{
set-alias sz "C:\Program Files\7-Zip\7z.exe"
$count=(Select-String -Path $path1 -pattern $string).length
$string1=Select-String -Path $path1 -pattern $string
sz a $Target $Source -ssw ;
$body1=("The Error:-"+$string+": "+"Error Count"+" "+$count+"::")
$string1 | ConvertTo-Html | Out-File 'E:\PowershellScripts\test.html'
$body=""
$body=$body1+$body2
Send-MailMessage -From "alert@domain.com" -To user@domain.com-Attachments $Target -Subject "A Seriuos Error on server1,please check" $body -BodyAsHtml -SmtpServer domain.com
}
else
{
write-host "nothing to process"
}