2

I have a script running that mirrors two directories and updates a log file about what happens between the two directories. I want to parse this log file to get updates as they come in (while the script is running) and I want these to send me an email of what has changed, like a notification. Also - I don't want to get notified about the same bit of info more than once if possible?

I'm unsure on how I would go about doing this after reading through some of the command lists online.

Tldr; Really all i'm looking to do is when a line is added to the Logfile, parse the file to get the updated line, without getting any of the other lines.

Not all that strong with PowerShell so any help would be greatly appreciated.

Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54
flanelman
  • 39
  • 6

1 Answers1

4

You can have PowerShell get each new line as it comes in by passing the -Wait parameter to Get-Content. You can then pass that into a Foreach-Object scriptblock to perform whatever operation you need.

Get-Content $logfile -Tail 0 -Wait | foreach {
  "Another line was added: " + $_
}

The -Tail N argument causes Get-Content to only get the last N lines; in this case zero. This causes it to skip everything already in the file, and only pass newly added lines to your script block.

Note: This cmdlet will continue running until you cancel it with Ctrl+C (or otherwise stop its PowerShell process), which makes it much better for use in an interactive prompt than in a script.

Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54
  • Thanks so much I've been looking for ages for this! would I pass the email script into the foreach to get it to email each new line to myself? then that would email each line as it comes in right? Thanks again! :) – flanelman Jun 07 '16 at 04:45
  • You would put commands inside the `foreach {...}` block. Inside that block, the `$_` variable contains the full line that was added. – Ryan Bemrose Jun 07 '16 at 04:54
  • so i should email "$_"? as the body, as that contains the line? :) – flanelman Jun 07 '16 at 04:59
  • That's a good start, but it depends entirely on what you want in your e-mail. I'd probably build a string for the body that includes that line and some text explaining why my machine is sending me mail. :) – Ryan Bemrose Jun 07 '16 at 05:03
  • ah yeah that would make more sense haha. I put the command into Powershell and i've got loads of errors like: You must provide a value expression on the right-hand side of the '-' operator. At line:1 char:7 Unexpected token 'Tail' in expression or statement. At line:1 char:8 Unexpected token '0' in expression or statement. At line:1 char:13 Unexpected token '-Wait' in expression or statement. At line:1 char:15 An empty pipe element is not allowed. At line:1 char:21 Any idea why this is? – flanelman Jun 07 '16 at 05:05
  • 2
    The `-Wait` parameter has some pitfalls you might need to be aware of. *but this requires the process writing to the file to open, append, close it before Get-Content will work. If the writing process never closes the file then it won't work which is not the case with tail -f.* taken from [here](http://stackoverflow.com/a/4427285/52598) – Lieven Keersmaekers Jun 07 '16 at 05:50
  • 1
    @flanelman No idea what's causing those errors without seeing the exact command you entered. You might be using [the wrong `foreach`](https://stackoverflow.com/a/29148614). – Ryan Bemrose Jun 07 '16 at 06:19
  • What modifications can be made to use this in a script? – Nitish Upreti Jun 11 '20 at 18:45