Power Shell

%3CLINGO-SUB%20id%3D%22lingo-sub-78276%22%20slang%3D%22en-US%22%3EPower%20Shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-78276%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%26nbsp%3B%3C%2FP%3E%3CP%3EI%20wanted%20to%20know%20if%20there%20is%20a%20script%20that%20can%20send%20an%20email%20when%20an%26nbsp%3Bevent%20is%20log%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-78276%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPower%20shell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-91845%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-91845%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-91837%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-91837%22%20slang%3D%22en-US%22%3EHere%E2%80%99s%20how%20it%20works%3A%3CBR%20%2F%3E%3CBR%20%2F%3E1)%20In%20Event%20viewer%2C%20find%20the%20event%20log%20you%20want%20to%20watch.%20Find%20the%20event%20you%20are%20looking%20for%20and%20right%20click%20it%20and%20choose%20%E2%80%9CAttach%20a%20Task%20To%20this%20Event%E2%80%A6%E2%80%9D%3CBR%20%2F%3E%3CBR%20%2F%3E2)%20Follow%20the%20wizard%20and%20when%20you%20get%20to%20the%20part%20about%20what%20to%20do%2C%20you%20can%20choose%20the%20default%20email%20option%2C%20or%20if%20you%20want%20to%20add%20some%20additional%20logic%2C%20choose%20to%20%E2%80%9CRun%20a%20program%E2%80%9D%3CBR%20%2F%3E%3CBR%20%2F%3E3)%20Copy%20the%20contents%20of%20the%20script%20below%20to%20a%20file%20with%20a%20.ps1%20extension%20and%20alter%20the%20script%20to%20fit%20your%20specific%20use%20case.%3CBR%20%2F%3E%3CBR%20%2F%3E4)%20Specify%20c%3A%5Cwindows%5Csystem32%5CWindowsPowerShell%5Cv1.0%5Cpowershell.exe%20as%20the%20name%20of%20the%20executable%20(do%20this%20even%20if%20you%20are%20using%20a%20later%20version%20of%20PowerShell)%3CBR%20%2F%3E%3CBR%20%2F%3E5)%20Specify%20the%20path%20and%20name%20of%20the%20script%20file%20you%20created%20above%20as%20the%20parameter.%3CBR%20%2F%3E%3CBR%20%2F%3E%24event%20%3D%20get-eventlog%20-LogName%20Application%20-source%20%22Put%20your%20source%20here%22%20-newest%201%3CBR%20%2F%3E%23get-help%20get-eventlog%20will%20show%20there%20are%20a%20handful%20of%20other%20options%20available%20for%20selecting%20the%20log%20entry%20you%20want.%3CBR%20%2F%3Eif%20(%24event.EntryType%20-eq%20%22Error%22)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24PCName%20%3D%20%24env%3ACOMPUTERNAME%3CBR%20%2F%3E%24EmailBody%20%3D%20%24event.Message%3CBR%20%2F%3E%24EmailFrom%20%3D%20%22Your%20Return%20Email%20Address%20%26lt%3B%24PCName%40yourdomain.com%26gt%3B%22%3CBR%20%2F%3E%24EmailTo%20%3D%20%22youremail%40yourdomain.com%22%3CBR%20%2F%3E%24EmailSubject%20%3D%20%22Your%20Event%20Log%20event%20was%20found!%22%3CBR%20%2F%3E%24SMTPServer%20%3D%20%22mailserver.yourdomain.com%22%3CBR%20%2F%3EWrite-host%20%22Sending%20Email%22%3CBR%20%2F%3ESend-MailMessage%20-From%20%24EmailFrom%20-To%20%24EmailTo%20-Subject%20%24EmailSubject%20-body%20%24EmailBody%20-SmtpServer%20%24SMTPServer%3CBR%20%2F%3E%7D%3CBR%20%2F%3Eelse%3CBR%20%2F%3E%7B%3CBR%20%2F%3Ewrite-host%20%22No%20error%20found%22%3CBR%20%2F%3Ewrite-host%20%22Here%20is%20the%20log%20entry%20that%20was%20inspected%3A%22%3CBR%20%2F%3E%24event%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%3CBR%20%2F%3ENow%2C%20anytime%20the%20event%20you%20specified%20is%20found%20in%20the%20log%2C%20your%20script%20will%20be%20triggered.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-78453%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-78453%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20You!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-78303%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-78303%22%20slang%3D%22en-US%22%3EThere%20plenty%20of%20these%2C%20just%20choose%20%3A)%3C%2Fimg%3E%20one%20of%20the%20examples%20%3CA%20href%3D%22https%3A%2F%2Fgallery.technet.microsoft.com%2FPowerShell-Script-to-Send-873dc0b6%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgallery.technet.microsoft.com%2FPowerShell-Script-to-Send-873dc0b6%3C%2FA%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

Hello, 

I wanted to know if there is a script that can send an email when an event is log

4 Replies
Highlighted
There plenty of these, just choose :) one of the examples https://gallery.technet.microsoft.com/PowerShell-Script-to-Send-873dc0b6
Highlighted

Thank You!

Highlighted
Here’s how it works:

1) In Event viewer, find the event log you want to watch. Find the event you are looking for and right click it and choose “Attach a Task To this Event…”

2) Follow the wizard and when you get to the part about what to do, you can choose the default email option, or if you want to add some additional logic, choose to “Run a program”

3) Copy the contents of the script below to a file with a .ps1 extension and alter the script to fit your specific use case.

4) Specify c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe as the name of the executable (do this even if you are using a later version of PowerShell)

5) Specify the path and name of the script file you created above as the parameter.

$event = get-eventlog -LogName Application -source "Put your source here" -newest 1
#get-help get-eventlog will show there are a handful of other options available for selecting the log entry you want.
if ($event.EntryType -eq "Error")
{
$PCName = $env:COMPUTERNAME
$EmailBody = $event.Message
$EmailFrom = "Your Return Email Address <$PCName@yourdomain.com>"
$EmailTo = "youremail@yourdomain.com"
$EmailSubject = "Your Event Log event was found!"
$SMTPServer = "mailserver.yourdomain.com"
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
}

Now, anytime the event you specified is found in the log, your script will be triggered.
Highlighted

Thank you!