Solved

Powershell script to send email event trigger in Windows 2012

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

635 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