-1

im trying to create a script that will check the contents of a log file, using windows powershell. The powershell script is checking the log file that is created from another windows application. The script is the following:

$smtpServer = "MailServer"
$fdate = Get-Date -Format yyyyMMdd
$fname = "C:\tmp\"+$fdate+".log"

$content=Get-Content $fname -wait | where { $_ -match ": exception" }|
foreach {

$line=$_
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "admin@xyz.com"
$msg.ReplyTo = "logs@xyz.com"
$msg.To.Add("logs@xyz.com")
$msg.subject = "Exception"
$msg.body = $line
$smtp.Send($msg)

Write-Host $line 
}

The script above is not able to read the new additions of the log file. If for example i have 10 lines in the log file and i start the script now it will check only the current 10 lines. If any new lines are added from the application while the script is running, the script is not able to check the newly added lines! Any recommendations for a proper solution to that issue? Did anyone tried to do something similar using C/C++ or java? Thank you

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
gron gron
  • 69
  • 1
  • 2
  • 4
  • I think you will have to look for filesystemwatcher http://gallery.technet.microsoft.com/scriptcenter/Powershell-FileSystemWatche-dfd7084b – Loïc MICHEL Aug 14 '14 at 13:36
  • Some limitations on `-wait`. See http://stackoverflow.com/questions/19919180/get-content-wait-not-working-as-described-in-the-documentation – andyb Aug 14 '14 at 15:03
  • thank you for the suggestions. I'll have a look! – gron gron Aug 18 '14 at 08:40

1 Answers1

0

Also you can use these: Multithreading with Jobs in PowerShell and PowerShell Multithreading

Community
  • 1
  • 1
ekostadinov
  • 6,880
  • 3
  • 29
  • 47