title: parse log file using powershell, and send email notification
body: <p>Thank you for taking time and helping me out,</p>
  
  <p>I am trying to create powershell script to read from log file. If "Buy" or "sell" word found then send me an email notification, The script should read last line only and once only, else multiple notifications</p>
  
  <p>location of the log file
  "C:\Program Files\LMFX MetaTrader 4 Terminal\MQL4\Logs\20181206.log"</p>
  
  <pre><code>&gt; tail -5 (last 5 entry of the log file) 0  19:44:20.644    indicator1
  &gt; EURUSD,Daily: initialized 0   19:44:20.644    indicator2 EURUSD,Daily:
  &gt; initialized 0 19:44:20.645    indicator3 EURUSD,Daily: initialized
  &gt; 0 19:44:20.646    indicator4 EURUSD,Daily: initialized
  &gt; 0 19:44:20.659    indicator5 EURUSD,Daily: Alert:  ! BUY !  -  EURUSD
  &gt; 0 19:44:20.659    indicator5 EURUSD,Daily: Alert:  ! SELL !  -  EURUSD
  </code></pre>
  
  <p>`</p>
  
  <pre><code>#Powershell Script
  $logDir = "C:\Program Files\LMFX MetaTrader 4 Terminal\MQL4\Logs"
  
  function Send-ToEmail([string]$email){
  $user = "Sender@email.com"
  $pass = ConvertTo-SecureString -String "PASSWORD" -AsPlainText -Force
  $cred = New-Object System.Management.Automation.PSCredential $user, $pass
  $body = ":("
  $mailParam = @{
      To = "Sender@email.com"
      From = "ALERT ALERT &lt;Reciever@email.com&gt;"
      Subject = "ALERT ALERT ALERT ALERT"
      Body = $body
      SmtpServer = "smtp.gmail.com"
      Port = 587
      Credential = $cred
      #Attachments = "none"     
  }
  
  # Send the email with all parameters
  Send-MailMessage @mailParam -UseSsl
  </code></pre>
  
  <p>}</p>
  
  <pre><code># create a variable to store the previous log line in
  $previousLogLine = ''
  while ($true) {
  $latestLog = Get-ChildItem -Path $logDir -Filter '*.log' | Sort-Object 
  LastWriteTime -Descending | Select-Object -First 1
  Write-Host "Reading from $($latestLog.Name)"
  
  $logLine = Get-Content -Path $latestLog.FullName -Tail 1
  # if this line is different from the previously stored line
  # and it contains either "sell" or "buy", then send the email
  if ($logLine -ne $previousLogLine -and $logLine -match 'sell|buy') { 
      Send-ToEmail -email "Reciever@email.com"
      # remember this line to compare with the line we get in the next 
      iteration
      $previousLogLine = $logLine
      }
      Start-Sleep -Seconds 1
      cls
     }
  `
  </code></pre>