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

时间:2018-12-07 02:02:26

标签: c++ windows powershell

感谢您抽出宝贵时间帮助我,

我正在尝试创建Powershell脚本以从日志文件读取。如果找到“买”或“卖”字样,请给我发送电子邮件通知,该脚本应仅读取最后一行,并且只能读取一次,否则应读取多个通知

日志文件的位置 “ 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
   }
`

2 个答案:

答案 0 :(得分:0)

将您的最后一部分更改为以下内容,我认为-wait开关在这种情况下不适合。 使用数组存储所有已发送的项目。

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

}

答案 1 :(得分:0)

我认为您需要做的是

  1. 检查循环中的最新日志文件
  2. 记住最后一行包含“购买”或“出售”的行,以免一遍又一遍地发送与电子邮件相同的行

也许是这样的:

$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
}

如您所见,我还将文件的LastAccessTime更改为LastWriteTime,因为我认为这更合适。 测试之后,不要忘记将To = "sender@email.com"的这一行更改为To = $email

希望有帮助

相关问题