windows 7 - Task Scheduler from Command-Line?

06
2014-06
  • Amirreza Nasiri

    I'm using Windows7 and I want to make a scheduled task with my options using Task Scheduler Command line mode utility that called "SchTasks".
    But there is some problems using Schtasks :
    1) Is there anyway to add "on Workstation Unlock" Trigger ?
    2) Is there anyway to add two or more triggers ?
    I Want to have all the options in Command line like when I'm using GUL Task Scheduler Application.

  • Answers
  • Karan

    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"
    

  • Related Question

    windows 7 - How can I use Task Scheduler to schedule a .jar file?
  • Beef

    I have an executable jar file and I am trying to create a scheduled task for every day using windows Task Scheduler I go through the following steps:

    open Task Scheduler -> Create Basic Task -> name task -> set trigger to daily -> set the time for run -> select start a program for Action -> browse and select my .jar file -> finish

    However it does not run, I saw on the internet that the start a program action only works with .exe files, is there a way to get this to run my .jar file? By the way I am using windows 7 and have the latest jdk/sdk for java.


  • Related Answers
  • grawity

    Schedule java.exe or javaw.exe instead, with the following command-line arguments:

    javaw -jar myfile.jar
    

    (On Windows, java is for console-based programs while javaw is for graphical ones.)

  • anzenketh

    You might be able to create a batch file to run the java program.

    @echo off
    java filepathto.jar
    

    However if the java program can not be run without any user input it would be useless.

  • SaintWacko

    A slightly roundabout way would be to download this Batch to EXE converter, use it to convert anzenketh's batch file to an exe with the setting Invisible Application, and including the jar file. This way you could directly tell task scheduler to run the resulting exe, and you would only have one exe to keep track of instead of the jar and the bat.