Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell script to send email event trigger in Windows 2012

Posted on 2016-08-23
3
Medium Priority
?
512 Views
Last Modified: 2016-09-20
I need help getting a powershell script to work in Windows 2012.  The script needs to be able to trigger an alert when security event logs 4722 gets triggered.  The email needs to include who enabled the account and what account was enabled.

My reference is to the following blog

http://www.ryadel.com/en/event-viewer-send-notification-e-mail-messages-with-powershell/

My powershell script looks like this


# -------------------------------------
# Ryadel.com - Powershell script to send an e-mail through the Event Viewer
# -------------------------------------
#
# To test this script you can use Powershell to write your own test error log entry in the following way:
# -------------------------------------
# New-EventLog –LogName Security –Source "Test"
# Write-EventLog –LogName Security –Source "Test" –EntryType Error –EventID 4722 –Message "This is a test message."
 
$event = get-eventlog -LogName Security -newest 4722
#get-help get-eventlog will show there are a handful of other options available for selecting the log entry you want.
#example: -source "your-source"
 
# "Error" - send only error
if ($event.EntryType -eq "Error")
{
    $PCName = $env:COMPUTERNAME
    $EmailBody = $event | format-list -property * | out-string
    $EmailFrom = "$PCName <noreply@me.com>"
    $EmailTo = "me@me.com"
    $EmailSubject = "New Event Log [Security]"
    $SMTPServer = "172.x.x.x"
    Write-host "Sending Email"
    Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -body $EmailBody -SmtpServer $SMTPServer
}
else
{
    write-host "No error found"
    write-host "Here is the log entry that was inspected:"
    $event
}

getting the following error when running

get-eventlog : Requested registry access is not allowed.
At C:\temp\new1.ps1:11 char:10
+ $event = get-eventlog -LogName Security -newest 4722
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-EventLog], SecurityException
    + FullyQualifiedErrorId : System.Security.SecurityException,Microsoft.PowerShell.Commands.GetEventLogCommand
0
Comment
Question by:tools2teach
3 Comments
 
LVL 24

Accepted Solution

by:
yo_bee earned 2000 total points
ID: 41768110
This is not powershell, but sounds like what you are trying to commplish.  
https://blogs.technet.microsoft.com/jhoward/2010/06/16/getting-event-log-contents-by-email-on-an-event-log-trigger/

Here is another way to skin this cat using the xml data from the triggered event.

https://vijredblog.wordpress.com/2014/03/21/task-scheduler-event-log-trigger-include-event-data-in-mail/
0
 

Author Comment

by:tools2teach
ID: 41768131
Thanks for the blogs.  I was able to get these to work with one of the domain controllers that is on 2008.   Unfortunately, the email feature in task manager does not work in Windows 2012 standard.   It was something that Microsoft decided to disable.   They put it back in in 2012 r2.
0
 
LVL 36

Expert Comment

by:Seth Simmons
ID: 41806179
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: yo_bee (https:#a41768110)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

seth2740
Experts-Exchange Cleanup Volunteer
0

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For anyone that has accidentally used newSID with Server 2008 R2 (like I did) and hasn't been able to get the server running again because you were unlucky (as I was) and had no backups - I was able to get things working by doing a Registry Hive rec…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

927 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