locked
Multiple Pattern Selections to be put in Email RRS feed

  • Question

  • Hello, 

    Very new at PowerShell, I am on a 2012 r2 azure server here.  What I am trying to do is somehow place the pattern that is found in the email below. The script goes out to the log file and searches for two distinct patterns. If I finds one of them it will send an email to the users . This task will run every ten minutes.

    1. How can I make the pattern that it finds be placed into the corresponding email

    2. How can I tell it to ignore the pattern that it found after the email is sent. Otherwise I get the same email every 10 Minutes.

    Thank you for the help,

    David

    $dir = "c:\logs"

    $latest = Get-ChildItem -Path c:\logs\workerservice*.* | Sort-Object LastAccessTime -Descending | Select-Object -First 1
    $computername = $env:COMPUTERNAME
    $Pattern = "System.data.SQLClient.SQLException"
    $Pattern2= "Pause"
    Get-Content $latest -Wait | Select-string -Pattern $Pattern,$Pattern2 | foreach {

    Send-MailMessage -From " noreply-test@hello.com" -To "User02 Billjones@goodby.com" -Subject "Windows Server Alert on $computername" -Body " A $pattern alert or a $pattern2 in the service has been logged on $computername server. Please stop and restart the worker service in windows services and check the error log located at  $latest." -Priority High  -SmtpServer "192.168.1.1"

    write $latest | out-file -filepath C:\logs\server.txt -Append
    Exit
    }
    • Moved by Bill_Stewart Monday, April 30, 2018 9:31 PM Unanswerable drive-by question
    Monday, February 26, 2018 6:33 PM

All replies

  • Get-Content $latest -Wait |
         Select-string -Pattern $Pattern,$Pattern2 -List | 
       
    out-file -filepath C:\logs\server.txt

    \_(ツ)_/


    • Edited by jrv Monday, February 26, 2018 6:54 PM
    Monday, February 26, 2018 6:52 PM
  • Hello,

    The out-file is a secondary process that is only part of the equation. Thank you for the input. I will work that into the script. What I also need is to be able to put the correct pattern it finds into the body of the email automatically.

    Thank you ,

    David

    Monday, February 26, 2018 7:49 PM
  • $var1 = Get-Content $latest -Wait |
         Select-string -Pattern $Pattern,$Pattern2 -List 


    \_(ツ)_/

    Monday, February 26, 2018 7:53 PM
  • This causes the script to just hang. I removed the line 

    Get-Content $latest -Wait | Select-string -Pattern $Pattern,$Pattern2 | foreach {  as I assume this Is what you are suggesting. I removed the  } also. I doesn't error out . It just sits there , cursor blinking

    Thank you

    Monday, February 26, 2018 8:39 PM
  • Why are you using "Wait".  That will hang until a new line is added to the file.

    It is not possible to guess at what you are trying to do.

    Read the help for the CmdLets you are trying to use until you understand how they work then write your code and post back with a clear question.


    \_(ツ)_/

    Monday, February 26, 2018 8:42 PM
  • My apologies.  

    Let me see what I find.

    Thank you. 

    Monday, February 26, 2018 8:53 PM