How to run a task triggered by an event only once

 

You might want to start or stop a task when a particular event is logged. For instance start a service or stop tracing. You can use the the Attach a Task To the Log option in the Event Viewer

The Attach a Task if found here

clip_image002

For example

clip_image004

Therefore you can create a simple batch file like this:

net stop spooler

pause

And add the batch here

clip_image006

After you have finished the wizard, you will find in the task scheduler the following task

clip_image008

You can here change the task here to your own needs, in this example I add event 222 as trigger

clip_image010

Now if event 222 is logged in the application log, the printer spooler is stopped

clip_image011

But now, if you want to run this only once, only the first 222 event and not the ones coming after? Simply add the following line to your batch file

net stop spooler

schtasks /Change /TN "\Event Viewer Tasks\Stop Printer Spooler" /DISABLE

pause

This will disable the task

clip_image013

Which you can see here

clip_image015

Hope this is helpful!

 

 

Credits for Dirk van Coeverden and Nicolas Uthurriague