How to create a TaskSchedule based on an EventViewer event and import it using Powershell

How to create a TaskSchedule based on an EventViewer event and import it using Powershell?

This is useful when triggering a distinct set of instructions is needed or execute something specific based on an Event registered in EventViewer. 

For example, as a workaround, the service can be restarted when a known error is noticed.

Creating the Task

  1. Open the EventViewer
  2. Select the Event
  3. Right Click it and select "Attach task to this Event..."


4. Name the task


5. Press Next
6. Press Next
 
7.Select "Start a program" in the Action page


8. Press Next
9. Select the Program and its arguments
              For example we are running a powershell script called alerts.ps1
              Program\Script: powershell
              Add arguments (optional): -File "C:\Users\your_username\Desktop\alert.ps1"

              
10. Press Next
11. Select "Open the Properties dialog for this task when i click Finish" to tweak the configuration further
12. Press Finish
13. Select the User and privileges where the task should run and Configure for your OS version


14. Press OK

 

Exporting the task

 
  1. Open TaskScheduler
  2. Go to Event Viewer Tasks under the Library
  3. Select your newly created task
  4. Click Export
  5. Chose a destination and Filename
  6. Press Save
  • Creating it on another machine using Powershell

  1. Open Powershell
  2. Run the Command
    PS C:\> Register-ScheduledTask -Xml (get-content 'XMLFILEPATH' | out-string) -TaskName "Weekly System Info Report"