locked
Script to wait on log file that updates in real-time and sending each new line via email RRS feed

  • Question

  • Alright with the code below, which I removed all the actual information from my variables, I can get the email to send fine.

    When I test with Get-Content -Path "File" -tail 1 -wait and I update the file it prints to the console just fine but I am wanting it to where it will always send an email when a new line is added to the log.

    I have tried Out-String but that doesn't give desired results.

    I can't wrap my head around trying to get it to work.

    The log file contains something like this:

    [2017.02.08-17.03.12:192][742]2017.02.08_17.03.12: Zyahko joined this ARK!
    [2017.02.08-17.08.24:373][307]2017.02.08_17.08.24: Zyahko left this ARK!
    [2017.02.08-17.36.06:305][335]2017.02.08_17.36.06: Zyahko joined this ARK!
    [2017.02.08-17.41.40:337][572]2017.02.08_17.41.40: Zyahko left this ARK!
    [2017.02.08-17.52.07:065][824]2017.02.08_17.52.07: Zyahko joined this ARK!
    [2017.02.08-17.52.24:517][359]2017.02.08_17.52.24: Zyahko left this ARK!

    Each event is added as a new line when they occur. The Body variable is blank because I don't know how to get the results of the Get-Content command to go into the body of the email.

    Any insight would be appreciative.

    Thanks,

    $From = "From Address"
    $To = "To Address"
    $Subject = "Subject"
    $Body = ""
    $SMTPServer = "smtp server"
    $SMTPPort = "port"
    $Login = "email login"
    $Password = "password"
    $Credentials = New-Object System.Management.Automation.Pscredential -ArgumentList $Login, $Password
    $Dir = "C:\ark\ShooterGame\Saved\Logs\ServerGame*"
    $DirPath = "C:\ark\ShooterGame\Saved\Logs\"
    
    $Latest = Get-ChildItem -Path $Dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
    $Latest.name
    
    $File = (JOIN-PATH $DirPath $Latest.name)
    
    Get-Content -Path $File -Tail 1 -Wait | ForEach -Process (Send-MailMessage -From $From -to $To -Subject $Subject -Body -SmtpServer $SMTPServer -port $SMTPPort -Credential $Credentials)



    • Edited by Zyahko Wednesday, February 8, 2017 11:11 PM
    Wednesday, February 8, 2017 11:10 PM

Answers

  • My best guess is this is what you are asking:

    $maiprops = @{
    	From = '<from address>'
    	To = '<toaddress>'
    	Subject = 'Subject'
    	SMTPServer = 'smtp.dosmain.com'
    	SMTPPort = 25
    	Credentials = New-Object System.Management.Automation.Pscredential($Login, $Password)
    }
    $path = 'C:\ark\ShooterGame\Saved\Logs\ServerGame*'
    $dirPath = 'C:\ark\ShooterGame\Saved\Logs\'
    #'
    $file = Get-ChildItem -Path $path | 
    	Sort-Object LastAccessTime -Descending | 
    	Select-Object -First 1
    
    Get-Content -Path $file -Tail 1 -Wait 
    	ForEach-Object{
    		Send-MailMessage -Body $_ @mailprops
    	}


    \_(ツ)_/





    • Edited by jrv Thursday, February 9, 2017 12:12 AM
    • Marked as answer by Zyahko Thursday, February 9, 2017 2:52 AM
    Thursday, February 9, 2017 12:11 AM

All replies

  • What is it that you are asking?  Are you asking how to send mail?  What is it that you want in the "Body"?

    You should not place all of your code on one line. It serves no purpose and makes the code impossible to read.


    \_(ツ)_/

    Thursday, February 9, 2017 12:05 AM
  • Sorry I'm learning PowerShell as I go. I already got the sending email part down. I want the body to contain the line of text that occurs when the log is updated in real time.
    Thursday, February 9, 2017 12:10 AM
  • My best guess is this is what you are asking:

    $maiprops = @{
    	From = '<from address>'
    	To = '<toaddress>'
    	Subject = 'Subject'
    	SMTPServer = 'smtp.dosmain.com'
    	SMTPPort = 25
    	Credentials = New-Object System.Management.Automation.Pscredential($Login, $Password)
    }
    $path = 'C:\ark\ShooterGame\Saved\Logs\ServerGame*'
    $dirPath = 'C:\ark\ShooterGame\Saved\Logs\'
    #'
    $file = Get-ChildItem -Path $path | 
    	Sort-Object LastAccessTime -Descending | 
    	Select-Object -First 1
    
    Get-Content -Path $file -Tail 1 -Wait 
    	ForEach-Object{
    		Send-MailMessage -Body $_ @mailprops
    	}


    \_(ツ)_/





    • Edited by jrv Thursday, February 9, 2017 12:12 AM
    • Marked as answer by Zyahko Thursday, February 9, 2017 2:52 AM
    Thursday, February 9, 2017 12:11 AM
  • I will try that. Thank you very much
    Thursday, February 9, 2017 12:16 AM
  • Send-MailMessage : A parameter cannot be found that matches parameter name
    'SMTPPort'.
    At C:\Scripts\SendMessage.ps1:22 char:29
    +         Send-MailMessage -Body $_ @mailprops
    +                                   ~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], Paramet
       erBindingException
        + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm
       ands.SendMailMessage

    This is the error I get after I implemented your code into my script. I made sure I made it look like yours with the correct information. No clue what I messed up. I am using a different port so it works with my SMTP server

    The only thing that I did different because it wasn't sending anything to email was putting a | after -Wait

    Get-Content -Path $file -Tail 1 -Wait |

    • Edited by Zyahko Thursday, February 9, 2017 2:35 AM
    Thursday, February 9, 2017 2:33 AM
  • If the port is 25 - the default.  Just remove; otherwise the name should be just "Port".

    \_(ツ)_/

    Thursday, February 9, 2017 2:50 AM
  • Awesome so far it seems to work.

    Thank you very much for your time.

    Thursday, February 9, 2017 2:52 AM