none
search txt content and send mail with result

    Question

  • Hi guys,

    I'm looking for a script that read a txt file, if it find result different from 0 warnings and 0 errors send a e-mail result with warning number, error number and date.

    [4/12/2013 3:00:51 AM] Finished with 0 warnings. (final line)
    [4/12/2013 3:00:51 AM] Finished with 0 errors. (final line)

    the problem is, the name file changes all day and on this case the script must be look for file based on last modified date.

     file name: 1_1_16f94c73-f73c-427c-8ab9-d319665fad15_635011662668970000_62272

    thanks,


    Certified (35) | ITIL | MCP | MCDST | MCSA1 | MCSE1 | MCT | MCTS Sharepoint | MCITP SQL | MCSA Windows 2012 | MTA Security | MCSE Windows 8 | MCSE Windows 2012 | Vencedor do Winthe7.com.br 2009, Quarto lugar Copa de Talentos Microsoft 2010

    Monday, April 15, 2013 12:22 PM

Answers

  • So start debugging.

    What will You get from second line ? If nothing, then there is no files in C:\Temp\Logging

    See what You get with pieces of code

    Get-ChildItem $Folder <--- You'll get all files from $Folder

    Get-Childitem $Folder | Sort LastWriteTime -Descending <--- you'll get all files in a descending order

    (Get-Childitem $Folder | Sort LastWriteTime -Descending)[0]  <-- you'll get first file from ordered list

    countinue  till You find error

    • Marked as answer by Adriano Neiva Tuesday, April 16, 2013 11:57 AM
    Tuesday, April 16, 2013 5:46 AM
  • tks Blindrood

     my final script

    $Folder = 'C:\Temp\Landisgyr\Logging'

    $Arquivo = Get-Content (Get-Childitem $Folder -exclude *export*.log | Sort LastWriteTime | select -last 1)

    $Errors   = $Arquivo[-1].split(" ")[-2]

    $Warnings = $Arquivo[-2].split(" ")[-2]

    If ($Errors -ne 0) {

                    $today = (Get-Date -Format F)

                    $emailFrom = CD2-ERROR@mail

                    $emailTo = adriano.oliveira@mail

                    $subject = "The number of errors in CD2 log is "+"$Errors"

                    $body = $today, "The number of errors in CD2 log is "+"$Errors" + ", Manually execute the C:\scripts\LGContentDeployment.exe tool"

                    $smtpServer = "smtpserver"

                    $smtp = new-object Net.Mail.SmtpClient($smtpServer)

                    $smtp.Send($emailFrom, $emailTo, $subject, $body)

    }


    Certified (35) | ITIL | MCP | MCDST | MCSA1 | MCSE1 | MCT | MCTS Sharepoint | MCITP SQL | MCSA Windows 2012 | MTA Security | MCSE Windows 8 | MCSE Windows 2012 | Vencedor do Winthe7.com.br 2009, Quarto lugar Copa de Talentos Microsoft 2010

    • Marked as answer by Adriano Neiva Tuesday, April 16, 2013 11:57 AM
    Tuesday, April 16, 2013 11:57 AM

All replies

  • To get file with least modify time use this:

    (Get-Childitem $Folder | Sort LastWriteTime -Descending)[0]

    To search for a string first get the dat using Get-Content and search for lines that match/notmatch specific criteria

    Get-Content (Get-Childitem $Folder | Sort LastWriteTime -Descending)[0].FullName | Where-Object {$_ -notmatch '0 (warning|errors)'}

    then just send email with lines found

    $Folder = 'D:\something'
    $Result = Get-Content (Get-Childitem $Folder | Sort LastWriteTime -Descending)[0].FullName | Where-Object {$_ -notmatch '0 (warning|errors)'}
    if($Result){
        Send-MailMessage -Body $Result -To me@me.me -SmtpServer smtp.me.me
    }

    Monday, April 15, 2013 12:51 PM
  • thank you Blindrood for your answer.

    on this case the script must be created using this code?

    $Folder = 'C:\Temp\Logging'
    (Get-Childitem $Folder | Sort LastWriteTime -Descending)[0]
    Get-Content (Get-Childitem $Folder | Sort LastWriteTime -Descending)[0].FullName | Where-Object {$_ -notmatch '0 (warning|errors)'}

    $Result = Get-Content (Get-Childitem $Folder | Sort LastWriteTime -Descending)[0].FullName | Where-Object {$_ -notmatch '0 (warning|errors)'}
    if($Result)
    {
        Send-MailMessage -Body $Result -To adriano.oliveira@adriano.com -SmtpServer smtp.domain.net
    }

    using this code above, I'm not receiving any error or result.

    tks


    Certified (35) | ITIL | MCP | MCDST | MCSA1 | MCSE1 | MCT | MCTS Sharepoint | MCITP SQL | MCSA Windows 2012 | MTA Security | MCSE Windows 8 | MCSE Windows 2012 | Vencedor do Winthe7.com.br 2009, Quarto lugar Copa de Talentos Microsoft 2010

    Monday, April 15, 2013 1:14 PM
  • So start debugging.

    What will You get from second line ? If nothing, then there is no files in C:\Temp\Logging

    See what You get with pieces of code

    Get-ChildItem $Folder <--- You'll get all files from $Folder

    Get-Childitem $Folder | Sort LastWriteTime -Descending <--- you'll get all files in a descending order

    (Get-Childitem $Folder | Sort LastWriteTime -Descending)[0]  <-- you'll get first file from ordered list

    countinue  till You find error

    • Marked as answer by Adriano Neiva Tuesday, April 16, 2013 11:57 AM
    Tuesday, April 16, 2013 5:46 AM
  • tks for your answer.

    I'm looking for the solution for this error now.

    Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified
     method is not supported.

    Regards


    Certified (35) | ITIL | MCP | MCDST | MCSA1 | MCSE1 | MCT | MCTS Sharepoint | MCITP SQL | MCSA Windows 2012 | MTA Security | MCSE Windows 8 | MCSE Windows 2012 | Vencedor do Winthe7.com.br 2009, Quarto lugar Copa de Talentos Microsoft 2010

    Tuesday, April 16, 2013 10:25 AM
  • tks Blindrood

     my final script

    $Folder = 'C:\Temp\Landisgyr\Logging'

    $Arquivo = Get-Content (Get-Childitem $Folder -exclude *export*.log | Sort LastWriteTime | select -last 1)

    $Errors   = $Arquivo[-1].split(" ")[-2]

    $Warnings = $Arquivo[-2].split(" ")[-2]

    If ($Errors -ne 0) {

                    $today = (Get-Date -Format F)

                    $emailFrom = CD2-ERROR@mail

                    $emailTo = adriano.oliveira@mail

                    $subject = "The number of errors in CD2 log is "+"$Errors"

                    $body = $today, "The number of errors in CD2 log is "+"$Errors" + ", Manually execute the C:\scripts\LGContentDeployment.exe tool"

                    $smtpServer = "smtpserver"

                    $smtp = new-object Net.Mail.SmtpClient($smtpServer)

                    $smtp.Send($emailFrom, $emailTo, $subject, $body)

    }


    Certified (35) | ITIL | MCP | MCDST | MCSA1 | MCSE1 | MCT | MCTS Sharepoint | MCITP SQL | MCSA Windows 2012 | MTA Security | MCSE Windows 8 | MCSE Windows 2012 | Vencedor do Winthe7.com.br 2009, Quarto lugar Copa de Talentos Microsoft 2010

    • Marked as answer by Adriano Neiva Tuesday, April 16, 2013 11:57 AM
    Tuesday, April 16, 2013 11:57 AM
  • Adriano,

    Poderia me ajudar?

    Preciso de algo parecido,

    Preciso que o script leia um log que estará sozinho em uma pasta e que dependendo do valor encontrado RC=0 envie um email e se diferente de 0 envie outro email.

    Obrigado desde já

    Thursday, July 03, 2014 8:46 PM
  • Adriano,

    Poderia me ajudar?

    Preciso de algo parecido,

    Preciso que o script leia um log que estará sozinho em uma pasta e que dependendo do valor encontrado RC=0 envie um email e se diferente de 0 envie outro email.

    Obrigado desde já

    Seria algo assim??

    $File="C:\*\*.log"

    $Line=Get-Content -Path $File | where { $_ -match “RC=0” }

    if (RC=0)
    {
    $EmailFrom = "*"
    $EmailTo = "*"
    $Subject = "*"
    $Body = "*"
    $SMTPServer = "*"
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("*");
    $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
    }
    else
    {
    $EmailFrom = "*"
    $EmailTo = "*"
    $Subject = "*"
    $Body = "*"
    $SMTPServer = "*"
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("*");
    $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
    }

    Thursday, July 03, 2014 8:47 PM
  • Por favor, comeceuma nova pergunta.


    ¯\_(ツ)_/¯

    Thursday, July 03, 2014 8:55 PM