Solved

Powershell script to send email event trigger in Windows 2012

Posted on 2016-08-23
3
33 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 21

Accepted Solution

by:
yo_bee earned 500 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 34

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

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

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article will help you understand what HashTables are and how to use them in PowerShell.
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…

705 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

21 Experts available now in Live!

Get 1:1 Help Now