简体   繁体   English

使用Powershell解析日志文件,并发送电子邮件通知

[英]parse log file using powershell, and send email notification

Thank you for taking time and helping me out, 感谢您抽出宝贵的时间来帮助我,

I am trying to create powershell script to read from log file. 我正在尝试创建Powershell脚本以从日志文件读取。 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 如果找到“买”或“卖”字,请给我发送电子邮件通知,该脚本应仅读取最后一行,并且只能读取一次,否则应读取多个通知

location of the log file "C:\\Program Files\\LMFX MetaTrader 4 Terminal\\MQL4\\Logs\\20181206.log" 日志文件“ C:\\ Program Files \\ LMFX MetaTrader 4 Terminal \\ MQL4 \\ Logs \\ 20181206.log”的位置

> tail -5 (last 5 entry of the log file) 0  19:44:20.644    indicator1
> EURUSD,Daily: initialized 0   19:44:20.644    indicator2 EURUSD,Daily:
> initialized 0 19:44:20.645    indicator3 EURUSD,Daily: initialized
> 0 19:44:20.646    indicator4 EURUSD,Daily: initialized
> 0 19:44:20.659    indicator5 EURUSD,Daily: Alert:  ! BUY !  -  EURUSD
> 0 19:44:20.659    indicator5 EURUSD,Daily: Alert:  ! SELL !  -  EURUSD

` `

#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 <Reciever@email.com>"
    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

} }

# 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
   }
`

Change your last part to following, I don't think -wait switch is suitable in this case. 将您的最后一部分更改为以下内容,我认为-wait开关在这种情况下不适合。 Use an array to store all items sent. 使用数组存储所有发送的项目。

while ($true) {
$sent = @()
Get-Content -Path "C:\Program Files (x86)\Tickmill MT4 Client Terminal\MQL4\Logs\$latest" -Tail 1 | %{if(($_ -match "sell" -or $_ -match "buy") -and $sent -notcontains $_){Send-ToEmail  -email "receiver@email.com"; $sent += $_}}

start-sleep 2

}

I think what you need to do is to: 我认为您需要做的是:

  1. check the latest log file inside the loop 检查循环内的最新日志文件
  2. remember the last line that contained "buy" or "sell" in order not to send the same line as email over and over again 记住最后一行包含“购买”或“出售”的行,以免一遍又一遍地发送与电子邮件相同的行

Perhaps something like this: 也许是这样的:

$logDir = "C:\Program Files (x86)\Tickmill MT4 Client 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 <Reciever@email.com>"
        Subject = "ALERT : #tail last line"
        Body = $body 
        SmtpServer = "smtp.gmail.com"
        Port = 587
        Credential = $cred
        #Attachments = "none"     
    }

    # Send the email with all parameters
    Send-MailMessage @mailParam -UseSsl
 }

# 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 "receiver@email.com"
        # remember this line to compare with the line we get in the next iteration
        $previousLogLine = $logLine
    }

    Start-Sleep -Seconds 2
}

As you can see, I've also changed LastAccessTime into LastWriteTime for the file, because I think that is more appropriate. 正如你所看到的,我也变LastAccessTimeLastWriteTime的文件,因为我认为这是比较合适的。 After testing, don't forget to change this line To = "sender@email.com" into To = $email . 测试之后,不要忘记将To = "sender@email.com"这一行更改为To = $email

Hope that helps 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM