Solved

Powershell script to send email event trigger in Windows 2012

Posted on 2016-08-23
3
115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 23

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 35

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

752 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