Solved

Event log mail forwarding 2012 R2

Posted on 2014-09-04
18
327 Views
Last Modified: 2014-09-09
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>
0
Comment
Question by:BYRONJACKSON
  • 9
  • 8
18 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 40303819
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
 

Author Comment

by:BYRONJACKSON
ID: 40303908
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
 
LVL 18

Accepted Solution

by:
Steven Harris earned 100 total points
ID: 40303962
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
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 400 total points
ID: 40303983
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
 

Author Comment

by:BYRONJACKSON
ID: 40305429
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
 
LVL 40

Expert Comment

by:Subsun
ID: 40305441
What if you try my code?
0
 

Author Comment

by:BYRONJACKSON
ID: 40305469
It fails  Subsun - hang on will run again and post the errors
0
 

Author Comment

by:BYRONJACKSON
ID: 40305483
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
 
LVL 40

Expert Comment

by:Subsun
ID: 40305602
probably your source is wrong..

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

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:BYRONJACKSON
ID: 40305823
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
 
LVL 40

Expert Comment

by:Subsun
ID: 40305836
You can get it in any format.. :-)..  In what format you need? pls give a sample..
0
 

Author Comment

by:BYRONJACKSON
ID: 40305850
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
 
LVL 40

Expert Comment

by:Subsun
ID: 40305857
So are you trying to report more than one event in same mail?
0
 

Author Comment

by:BYRONJACKSON
ID: 40305896
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
 
LVL 40

Expert Comment

by:Subsun
ID: 40305945
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
 

Author Comment

by:BYRONJACKSON
ID: 40306203
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
 
LVL 40

Expert Comment

by:Subsun
ID: 40306221
Try with

-Source "Microsoft-Windows-Security-Auditing"

Open in new window

1
 

Author Closing Comment

by:BYRONJACKSON
ID: 40312109
Thank you both for your solutions - really helped me out!
0

Featured Post

The problems with reply email signatures

Do you wish that you could place an email signature under a reply? Well, unfortunately, you can't. That great Exchange/Office 365 signature you've created will just appear at the bottom of an email chain. What a pain! Is there really no way to solve this? Well, there might be...

Join & Write a Comment

Welcome to my series of short tips on migrations. Whilst based on Microsoft migrations the same principles can be applied to any type of migration. My first tip is around source server preparation. No migration is an easy migration, there is a…
Many admins will agree: WSUS is is a nice invention but using it on the client side when updating a newly installed computer is still time consuming as you have to do several reboots and furthermore, the procedure of installing updates, rebooting an…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now