Event log mail forwarding 2012 R2

Hi All

Was looking to use the script below to trigger events within the event log 2012 R2.  Is there anyway that I can either:

Attach a copy of the log entry
Or copy the content inside the message.

The current script is below - has anyone done this?

Send-MailMessage -to <recipient email address> -Subject "Backup Success" -body "The server backup job on server1 completed successfully" -smtpserver <your mail server> -from <sender email addreess>
BYRONJACKSONAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubsunCommented:
Here is a simple TechNet article on how to create a event log trigger, this should work for Windows Server 2012 also. Please check and let me know if you have any questions.

http://blogs.technet.com/b/jhoward/archive/2010/06/16/getting-event-log-contents-by-email-on-an-event-log-trigger.aspx
0
BYRONJACKSONAuthor Commented:
I actually attempted this but could not complete the 1st stage as email functions have been depreciated in 2012 R2.  I was attempting to do this in Powershell and forward out using SMTP.  Any suggestions?
0
Steven HarrisPresidentCommented:
In regards to the Article Response:

Start by experimenting with the following and verifying the output in the console, or ISE:

$Event = Get-EventLog -LogName System -InstanceId 4740 -Newest 1
$Event | format-list -property *

Open in new window


Can you tell me what type of output you receive?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

SubsunCommented:
In that case you can create event trigger to start a program (in this scenario a PowerShell script). Use the following code to get the event and mail it to your email ID.
$event =  Get-eventlog -LogName Application -Source "Your Event source here" -Newest 1
#get-help get-eventlog will give you other options available for selecting the log entry you want.
if ($event -ne $null)
{   $EmailBody = ($event | FL * | Out-String)
    $EmailFrom = "Your Email Address <$PCName@yourdomain.com>"
    $EmailTo = "youremail@yourdomain.com" 
    $EmailSubject = "The server backup job on server1 completed successfully"
    $SMTPServer = "mail.yourdomain.com"
    
    Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -body $EmailBody -SmtpServer $SMTPServer
}

Open in new window

Ref : http://technet.microsoft.com/en-us/library/hh849834.aspx
0
BYRONJACKSONAuthor Commented:
Hi both,

If I run Get-EventLog -LogName Security -Newest 1 with the format out string all is great and the output was given correctly.  However if I run this $Event = Get-EventLog etc all runs but the mail is sent without content - no errors are given.

I have used both ideas and combined them - Why did they get rid of the email function Grr? lol

Any ideas?

#PowerShell must run with elevated permissions:
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

{  
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
#Powershell is now elevated

$Event = Get-EventLog -LogName Security -Newest 1
$MailBody= Format-list * | Out-String

$MailSubject= "User Lockout Notification"

$SmtpClient = New-Object system.net.mail.smtpClient
Send-MailMessage -SmtpServer mysmtpserver -From myadminemail  -to myadmingroupemaill -Subject "TEST"
  -Body $Event.Message + "`r`n`t" + $Event.TimeGenerated

$MailMessage.IsBodyHtml = 0
$MailMessage.Subject = $MailSubject
$MailMessage.Body = $MailBody
$SmtpClient.Send($MailMessage)
0
SubsunCommented:
What if you try my code?
0
BYRONJACKSONAuthor Commented:
It fails  Subsun - hang on will run again and post the errors
0
BYRONJACKSONAuthor Commented:
This is what I get

Get-eventlog : No matches found
At line:1 char:11
+ $event =  Get-eventlog -LogName Application -Source 1001 -Newest 1
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Get-EventLog], ArgumentException
    + FullyQualifiedErrorId : GetEventLogNoEntriesFound,Microsoft.PowerShell.Commands.GetEventLogCommand

No mail is sent
0
SubsunCommented:
probably your source is wrong..

-Source 1001
SourceCorrect..
$event =  Get-eventlog -LogName Application -Source "windows Error Reporting" -Newest 1

Open in new window

0
BYRONJACKSONAuthor Commented:
Hi - Thank you it is inserting data now .  Is there a way that I can improve the data format.  At the moment the string is not readable. Would like to send in a vertical list or table format is this possible?

Also with the source should this be change per event type - ie; Security etc.  Sorry if this sounds vague - not that experienced with Powershell
0
SubsunCommented:
You can get it in any format.. :-)..  In what format you need? pls give a sample..
0
BYRONJACKSONAuthor Commented:
Hi Subsun - a table? or

Data 1
Data 2
Data 3 etc

Also with the source will this need to be changed per event type? ie Security - Application etc?  I will probably only be looking at GPO audit events....

Thanks for your help - Appreciated!
0
SubsunCommented:
So are you trying to report more than one event in same mail?
0
BYRONJACKSONAuthor Commented:
Actually all I want to do is to fire and email to some selected events within the security log of the event viewer.  The PS script will be attached to a task that triggers on the event arriving in the log,  As for multiple events - no not in a single mail - just a single event per mail  but it just needs to be formatted in a tabular way .

Hope this explains?

Byron
0
SubsunCommented:
Try..
$event =  Get-eventlog -LogName Application -Source "Your Event source here" -Newest 1
#get-help get-eventlog will give you other options available for selecting the log entry you want.
if ($event -ne $null)
{   
		$a = "<style>"
		$a = $a + "BODY{background-color:white;}"
		$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
		$a = $a + "TH{border-width: 1px;padding: 1px;border-style: solid;border-color: black;background-color:White}"
		$a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:white}"
		$a = $a + "</style>"

    $EmailBody = ($event | Select-Object TimeGenerated,EntryType,Source,InstanceID,Message | ConvertTo-HTML -head $a | Out-String)
    $EmailFrom = "Your Email Address <$PCName@yourdomain.com>"
    $EmailTo = "youremail@yourdomain.com" 
    $EmailSubject = "The server backup job on server1 completed successfully"
    $SMTPServer = "mail.yourdomain.com"
    
    Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -BodyAsHtml $EmailBody -SmtpServer $SMTPServer
}

Open in new window

0
BYRONJACKSONAuthor Commented:
Ahhh I see where I went wrong! so if I do this:

$event =  Get-eventlog -LogName Security -Source "Microsoft Windows security" -InstanceId 4720 -Newest 1

would this work for me in order to pull specific events?  It is just hanging at the moment
0
SubsunCommented:
Try with

-Source "Microsoft-Windows-Security-Auditing"

Open in new window

1
BYRONJACKSONAuthor Commented:
Thank you both for your solutions - really helped me out!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Legacy OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.