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
Solved

Powershell script to send email event trigger in Windows 2012

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A safe way to clean winsxs folder from your windows server 2008 R2 editions
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…
This tutorial will walk an individual through the process of configuring basic necessities in order to use the 2010 version of Data Protection Manager. These include storage, agents, and protection jobs. Launch Data Protection Manager from the deskt…

840 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