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.
Create the Task:
- Open the EventViewer
- Select the Event
- Right Click it and select "Attach task to this Event..."
- Name the task
- Press Next
- Press Next
- Select "Start a program" in the Action page
- Press Next
- Select the Program and its arguments
- For example run a powershell script called alerts.ps1
- Program\Script: powershell
Add arguments (optional): -File "C:\Users\your_username\Desktop\alert.ps1"
- Press Next
- Select "Open the Properties dialog for this task when i click Finish" to tweak the configuration further
- Press Finish
- Select the User and privileges where the task should run and Configure for the OS version
-
Press OK
Exporting the Task:
- Open TaskScheduler
- Go to Event Viewer Tasks under the Library
- Select your newly created task
- Click Export
- Chose a destination and Filename
- Press Save.
Creating it on Another machine using Powershell
- Open Powershell
- Run the Command
PS C:\> Register-ScheduledTask -Xml (get-content 'XMLFILEPATH' | out-string) -TaskName "Weekly System Info Report"