How to start a Windows task on `Workstation Unlock` and add 2 or more triggers?

8

1

I'm using Windows 7, and I want to make a scheduled task with my options using the Task Scheduler command-line mode utility that is called "SchTasks".

But there are some problems using Schtasks:

  1. Is there a way to add "on Workstation Unlock" trigger?
  2. Is there a way to add two or more triggers?

I want to have all the options on the command line like when I'm using the GUL Task Scheduler Application.

Amirreza Nasiri

Posted 2013-07-04T18:13:35.183

Reputation: 2 418

Answers

10

Option 1

Event ID 4801 corresponds to The workstation was unlocked. You can turn on logging for this event as I explain in my answer here.

Now you can use schtasks as follows to create the task triggered by this event:

schtasks /Create /RU "Username" /SC ONEVENT /MO "*[System[Provider[@Name='Microsoft-Windows-Security-Auditing'] and EventID=4801]]" /EC Security /TN "Taskname" /TR "Drive:\path to\program.exe" /F

Here I've used the /EC parameter to define the Event Channel (in this case the Security log). The MO or Modifier parameter is used to specify the XPath filter required to match events we are interested in.


However, you might state that you can use an On workstation unlock trigger without needing to turn on logging for the event, and you would be correct of course. The available triggers for a task are as follows:

1

The ones I've marked in green can be specified using schtasks' /SC parameter:

/SC   schedule     Specifies the schedule frequency.
                   Valid schedule types: MINUTE, HOURLY, DAILY, WEEKLY,
                   MONTHLY, ONCE, ONLOGON, ONSTART, ONIDLE, ONEVENT.

The ones I've marked in red don't seem to have corresponding schtasks options. There may be an obscure way to create tasks using such triggers, but till date I haven't found it and am inclined to think that it's just not possible (the lack of easily understandable yet detailed documentation about schtasks' parameters doesn't help either).

There is a workaround of course, which leads us to (drum-roll please)...

Option 2

Simply create the task with the required triggers (more than one if you want) using the Task Scheduler UI and export it as an XML. Now of course you can import the XML on demand and recreate the task perfectly:

schtasks /Create /TN "Taskname" /XML "ExportedTask.xml"

Karan

Posted 2013-07-04T18:13:35.183

Reputation: 51 857

Option 2 is, in fact, awesome! – Peter Mortensen – 2015-12-18T08:29:42.317

schtasks /Delete /TN "Taskname" may be useful if repeating the command to create the task. – Peter Mortensen – 2016-01-14T17:34:39.493

also.. for logging the event, you're using gpedit which is only present in Windows Enterprise. How can this be done in Windows HOME computers? – alpha_989 – 2018-08-04T18:58:22.477

Very useful. Tnx ! – Amirreza Nasiri – 2013-07-05T21:23:35.133

0

For me what worked is to use the EventID 42 and /EC System parameters as follows based on this post: Logging power events (sleep & wake up) to the event log at Microsoft Community.

So the following schtasks worked great for me:

schtasks /create /sc onevent /mo "*[System[(EventID=42)]]" /EC System /tn task1 /tr "batchfile.bat"

alpha_989

Posted 2013-07-04T18:13:35.183

Reputation: 623

0

Instead of turning on auditing of the unlock event the following code will work as is.

schtasks /Create /SC ONEVENT /MO "*[System[(EventID=4624)]] and  *[EventData[Data[9]="7"]]" /EC Security /TN "PF9I" /TR "\"C:\Program Files\Five9PlantronicsInterface\MyApps.bat"\" /F

The trick lies in the fact that Data[9] corresponds to the property LogonType and the value of 7 corresponds to the unlock event.

aliotta

Posted 2013-07-04T18:13:35.183

Reputation: 1