Answered by:
Email alert when an Event ID is triggered

Question
-
Hello,
I've googled a few methods on how to get emailed when an event code is triggered, but as yet I've not got it working.
I need to get emailed when the print spooler stops on our Citrix server.
Log Name: System
Source: Service Control Manager
Event ID: 7036
Level Information
I have then followed this:
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.here is my script:
$event = get-eventlog -System -source "Service Control Manager" -newest #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 = "myemailaddress@mydomain.com" $EmailSubject = "Your Event Log event was found!" $SMTPServer = "my email server IP" 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 }
Then I restart the printer spooler server and can see the event code is triggered, but no email.
The task scheduler event says
Task Scheduler successfully completed task "\Event Viewer Tasks\Citrix Printer Spooler alert" , instance "{1ed8a325-8a49-47ae-bd8b-e36c7fd43793}" , action "powershell.exe" with return code 2147942401.
What could be wrong?
Thanks
- Edited by TB303 Friday, December 2, 2016 10:37 AM
Friday, December 2, 2016 10:35 AM
Answers
-
Check properly. Read the Error
Event = Get-Eventlog -LogName System -Source "Service Control Manager" -Newest 1
It needs to be:
$Event = Get-Eventlog -LogName System -Source "Service Control Manager" -Newest 1
- Marked as answer by TB303 Tuesday, December 6, 2016 4:49 PM
Tuesday, December 6, 2016 2:01 PM
All replies
-
Might also try this action for task.
Regards, Dave Patrick ....
Microsoft Certified Professional
Microsoft MVP [Windows Server] Datacenter Management
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.Friday, December 2, 2016 6:11 PM -
Wrong:
get-eventlog -System -source "Service Control Manager" -newest
Correct
Get-EventLog -Logname System -Source "Service Control Manager" -Newest 10- Proposed as answer by Kirill Nikolaev Saturday, December 3, 2016 12:58 AM
Friday, December 2, 2016 8:47 PM -
Still no email and I've added it to our relay exchange policy:
PS C:\Windows\system32> C:\Batch files\PrintSpoolAlert.ps1 Index Time EntryType Source InstanceID Message ----- ---- --------- ------ ---------- ------- 117958 Dec 05 11:53 Information Service Control M... 1073748860 The WinHTTP Web Proxy Auto-Discovery Service service entered the stopped state. 117957 Dec 05 11:51 Information Service Control M... 1073748860 The Citrix Print Manager Service service entered the running state. 117956 Dec 05 11:51 Information Service Control M... 1073748860 The Print Spooler service entered the running state. 117955 Dec 05 11:51 Information Service Control M... 1073748860 The Print Spooler service entered the stopped state. 117954 Dec 05 11:51 Information Service Control M... 1073748860 The Citrix Print Manager Service service entered the stopped state. 117953 Dec 05 11:42 Information Service Control M... 1073748860 The Citrix Print Manager Service service entered the running state. 117952 Dec 05 11:42 Information Service Control M... 1073748860 The Print Spooler service entered the running state. 117951 Dec 05 11:42 Information Service Control M... 1073748860 The Print Spooler service entered the stopped state. 117950 Dec 05 11:42 Information Service Control M... 1073748860 The Citrix Print Manager Service service entered the stopped state. 117948 Dec 05 11:41 Information Service Control M... 1073748860 The Windows Error Reporting Service service entered the stopped state. No error found Here is the log entry that was inspected: PS C:\Windows\system32>
Monday, December 5, 2016 11:56 AM -
I tested this on Microsoft Virtual Lab
$Event = Get-Eventlog -LogName System -Source "Service Control Manager" -Newest 10 $EmailBody = $event.Message | Out-String $EmailFrom = 'administrator@contoso.com' $EmailTo = 'alexw@contoso.com' $EmailSubject = "Your Event Log event was found!" $SMTPServer = 'mbx1' Write-host "Sending Email" Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -body $EmailBody -SmtpServer $SMTPServer
Monday, December 5, 2016 5:49 PM -
That one you tested didn't work for me. I did try this though to proved email worked and it did:
Send-MailMessage –From test@mydomain.co.uk –To me@mydomain.co.uk –Subject “Test Email” –Body “Test E-mail (body)” -SmtpServer smtp
Tuesday, December 6, 2016 12:14 PM -
If I run in ISE I gues
Get-Event : A positional parameter cannot be found that accepts argument '='. At C:\Batch files\printerspooler.ps1:1 char:1 + Event = Get-Eventlog -LogName System -Source "Service Control Manager" -Newest 1 ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-Event], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetEventCommand Sending Email Send-MailMessage : Cannot validate argument on parameter 'Body'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. At C:\Batch files\printerspooler.ps1:8 char:81 + ... lSubject -body $EmailBody -SmtpServer $SMTPServer + ~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage
Tuesday, December 6, 2016 12:17 PM -
Check properly. Read the Error
Event = Get-Eventlog -LogName System -Source "Service Control Manager" -Newest 1
It needs to be:
$Event = Get-Eventlog -LogName System -Source "Service Control Manager" -Newest 1
- Marked as answer by TB303 Tuesday, December 6, 2016 4:49 PM
Tuesday, December 6, 2016 2:01 PM -
Body will empty because $Event doesn't exist.Tuesday, December 6, 2016 2:02 PM
-
works thanks!!Tuesday, December 6, 2016 4:49 PM
-
Just one last thing when the event appears the powershell script starts which is great, but we see the powershell script flisker up on the screen, can we hide this as this will be on a Citrix server with multiple users connecting to it.
I need to add the hsotname to the subject line, how can I embed $env:computername
I tried this but it failed
$EmailSubject = $env:computername "Your Event Log event was found!"
Thanks
- Edited by TB303 Tuesday, December 6, 2016 6:46 PM
Tuesday, December 6, 2016 4:57 PM -
works thanks!!
You are WelcomeTuesday, December 6, 2016 8:04 PM -
On General Tab
Select Run whether user is logged on or not
On Action Tab
Add Agruments
-File D:\Event.ps1 -WindowStyle Hidden- Edited by Vincent Karunaidas Tuesday, December 6, 2016 8:36 PM
Tuesday, December 6, 2016 8:32 PM -
$EmailSubject = "$($env:computername): Your Event Log event was found!"Tuesday, December 6, 2016 8:34 PM
-
Almost there now.
Seems the event code I'm using is quite generic (7036) so I'm getting alerts on other events.
Can the script look in the body of the event log and server for "The Citrix Print Manager Service service entered the stopped state." and just alert me on that?
Thanks
Wednesday, December 7, 2016 12:19 PM -
Did you try anything
You won't learn PowerShell if you don't put any effort. Will be good for your Career
Post the code which you have tried.
Wednesday, December 7, 2016 12:30 PM