Scan in file time-date in format over a timespan

Hi All,

Ik have writen a small sript that can search for specific time and date in a specific format and the error name in a log file. We want to run this in a task scheduler in an interval of 10 min…I want to make the script scan that it looks for the current date and time - 10 minutes earlier in that logfile. So then the script runs at 3pm…it needs to look for all entries with the time and date from today in a DD:MMM;YYYY HH:MM format from 2:50pm until 3 pm.

I have create the script now that it only looks for current date do it will pick up all todays enties with that error and mail it…But the problem is now when it picks up the error it will send from that moment every 10min an email…and that is a little to much…Can someone help me.


it’s nearly impossible to recommend something meaningful for you without seeing your code. So please share your code. (… formatted as code please :wink: )

Hi Olaf…

I hope this is what you mean. i am pretty new in this

$CurrentServer = $env:COMPUTERNAME
$WantFile = “D:\Temp\PHP LOG\php-7.1.11_errors.log”
$date = Get-Date -format “dd-MMM-yyyy”
$AttachmentContainer = “D:\Temp\PHP LOG\EMAIL Attachment”

$FileExists = Test-Path $WantFile
If ($FileExists -eq $True){

        write-host "File is found and script can continue"


        write-host "File is not found and script can not continue"


$ContainerExists = Test-Path $AttachmentContainer
if($ContainerExists -eq $false){
New-Item -ItemType directory -Path “D:\Temp\PHP LOG\EMAIL Attachment” | Out-Null
write-host “Directory D:\Temp\Logs\PHP\EMAIL Attachment\ has been created”
write-host “Directory already exists, script will continue.”

$Lookup = get-content “D:\Temp\PHP LOG\php-7.1.11_errors.log”
If ($Lookup -imatch $date -and “PHP Fatal Error”){

        Write-host "Tekst has been found, script will be excecuted."
    $GetLastData = Get-Item -path $wantfile | Get-Content -tail 200 | out-file "D:\Temp\PHP LOG\EMAIL Attachment\Error.log"
        start-sleep -seconds 2

   $From = 'xxxx@xxxx.nll'
   $To = ''
   $Subject = 'BCS PhP Fatal Error'
   $Body = 'BCS Opleidingsomgeving heeft een fatale fout geconstateerd'
   $attachment = 'D:\Temp\PHP LOG\EMAIL Attachment\Error.log'
   $SMTPServer = ''
   $SMTPPort = "25"
          Send-MailMessage -From $from -To $To -SmtpServer $SMTPServer -Port $SMTPPort -Subject $subject  -Body $Body -Attachments $Attachment

        Remove-Item "D:\Temp\PHP LOG\EMAIL Attachment" -Force -recurse
        write-host "Email has send. Script is terminated."

Remove-Item “D:\Temp\PHP LOG\EMAIL Attachment” -Force -recurse
Write-host “Text has not been found, scrip is terminated.”



sorry for the late Answer. I think I’ve got an idea of what you want to do. I see some room for improvement in your code. To make it proper it would be necessary to analize some sample log entries. Could you please post a few lines of the log file containing some errors and some “non errors” … from sensitive information sanitized of course. (formatted as code as well please :wink: … it’s the icon named “preformatted text (Ctrl+Shift+C)” )

Thanks in advance.

Hey Olaf,

I am having weekend now. Monday ill post the info you requested!

Have a good weekend in advance

Outlook voor iOS downloaden

Oh cool … a long Weekend. Enjoy it.

'till Monday

[22-Feb-2021 12:41:05 Europe/Amsterdam] getLogGebruikerSessieId called while user session not
[22-Feb-2021 14:16:59 Europe/Amsterdam] PHP Fatal error:
[22-Feb-2021 14:17:32 Europe/Amsterdam] PHP Fatal error:
[22-Feb-2021 14:17:38 Europe/Amsterdam] PHP Fatal error:
[25-Feb-2021 14:17:59 Europe/Amsterdam] PHP Fatal error:
[25-Feb-2021 14:45:59 Europe/Amsterdam] PHP Fatal error:

The first line is a ‘non error’ all others are…but if i want to run the script every 10 min i dont want to get an email because he found only on date that way my mailbox keeps piling up.

I streamlined your code a little bit … should work actually. Try it!

$WantFile = 'D:\Temp\PHP LOG\php-7.1.11_errors.log'
$AttachmentPath = 'D:\Temp\PHP LOG\EMAIL Attachment\Error.log'
$TenMinutesAgo = (Get-Date).AddMinutes(-10)

If (Test-Path $WantFile) {
    $LogContent = 
    Get-Content -Path $WantFile |
    Where-Object { $_ -match 'PHP Fatal Error' } |
    ForEach-Object {
        $Line = $_ -replace '[\][]' -split ' '
            DateTime = Get-Date ($Line[0..1] -join ' ')
            Site     = $Line[2]
            Message  = $Line[3..($Line.count)] -join ' '
    } |
    Where-Object { $_.DateTime -gt $TenMinutesAgo }

    If ($LogContent) {
        $AttachmentContainer = Split-Path -Path $AttachmentPath -Parent
        if (-not(Test-Path $AttachmentContainer)) {
            New-Item -ItemType directory -Path $AttachmentContainer | Out-Null
        $LogContent | Out-File $AttachmentPath

        $SendMailMessageParams = @{
            From        = 'xxxx@xxxx.nll'
            To          = ''
            Subject     = 'BCS PhP Fatal Error'
            Body        = 'BCS Opleidingsomgeving heeft een fatale fout geconstateerd'
            Attachments = $AttachmentPath
            SMTPServer  = ''
            Port        = 25
        Send-MailMessage @SendMailMessageParams
        Remove-Item -Path $AttachmentContainer -Recurse -Force

Seems to work but i need the feedbacks in the code…i want to see if it has found the text or not…also the get-date will not give the format as i need it…

get date will give me: monday 1 march 2021 12:21:27

The code I posted is just a suggestion. For now I removed everything what’s not necessary for the basic purpose of the code. You are allowed to change or to extend or to adapt it to your needs. :wink:

I understood, that this code should run scheduled/unattended. Whatfor do you need console output then? I’d recommend to add some debug messages if needed or some verbose messages if you like but having a lot of console output especially with Write-Host is considered bad style most of the time. :wink: