WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

NT Native Commands with WinBatch

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Microsoft Task Scheduler Command Line Utility

Keywords:
 schtasks Microsoft Task Scheduler Command Line AT SYSTEM NT AUTHORITY LOCALSERVICE NETWORKSERVICE Account Interactive Interact Desktop Display UAC



Notes:

Schtasks

http://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx

Schedules commands and programs to run periodically or at a specific time. Adds and removes tasks from the schedule, starts and stops tasks on demand, and displays and changes scheduled tasks.

To view the command syntax, launch cmd.exe and type:

c:> schtasks /?


Interactive Script Notes:

Starting with Windows Vista, processes running under the System account cannot interact with the desktop. This is a pain for scripts, but it was a necessary security improvement to Windows.

You have to rewrite such scripts to work differently. You could just give up on interacting with the desktop and just write an error log to a directory that can be read by the user later. Or you could split the script into two parts, one that runs in the System context to do the backup and another script that runs in the user context to display messages to the user.

The latter approach requires developing a method to pass messages between the two programs, which can be challenging to do reliably and efficiently (i.e. not waste too many CPU cycles). Named pipes is one possible method.

Another approach is to set your script to run two different ways. That is, run it under the System account when the user is not logged in and write all messages to a log. When the user is logged in, run the script under the user's account. Of course, this approach requires granting the user write access to the backup location (yet another challenge) and you have to deal with the situation where the user logs out while the backup is running (e.g. interrupt the backup and resume under the System account).

All things being equal, I'd just always run it under System, write a log, and figure out some way to alert the user to the log if any errors occurred. I hope that helps a little. Sorry there's no magic bullet for this issue.

Here's a possible workaround...

Change your scheduled task to run your script via psexec.exe interactively like this:

psexec.exe -accepteula -i -d -s mybackupscript.exe
Put psexec.exe in a folder that is on the path or in the same folder as your script and set that as the current directory in your task. Be sure to run the task as SYSTEM.

Note that the desktop interaction won't be direct. The user will get a flashing icon on the taskbar for your script. When that's clicked, the user will get the option to display the message. It's not the most elegant interface, but maybe it could be an interim solution for you.

P.S. If you want to test the psexec command line manually, you'll need to disable UAC. Otherwise, you will get an "access denied" message. UAC won't matter for the scheduled task because it's running as SYSTEM.

Reference: http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx


JT

Because this incompatibility also exists in Win2K, Microsoft created the Jt utility as part of the Microsoft Windows 2000 Resource Kit.

After you install the utility, you can go to the command prompt and type

Jt /? 
to see the full Help screen. For example,
 
 C:\> jt /se
 
Lists the pending tasks under the Task Scheduler.


The Microsoft Task Scheduler Command Line Utility, jt.exe, allows you to manage the Task Scheduler from the command line

usage:

JT {[options]|[@commandfile]}

options:

  @ <file>           - parse file
  /? [cmd]           - display help on [cmd], e.g. /? abj
  !<cmd>             - don't stop if command returns error
  /ABJ               - abort task
  /CSAGE             - convert SAGE tasks to tasks (Win9x only)
  /CTJ [<props>]     - create trigger in task
  /DTJ [<id>]        - delete trigger <id> (default 0) from task
  /EJ [<n> [T|F]]    - edit task page <n>, persist changes T/f
  /ENC <id1> <id2>   - enumerator clone <id1> from <id2> (see SCE command)
  /ENN <id> <n>      - enumerate next <n> items (see SCE command)
  /ENR <id>          - enumerator reset (see SCE command)
  /ENS <id> <n>      - enumerator skip forward by <n> (see SCE command)
  /GC                - get credential account name
  /GM                - get target machine
  /ISJQ <file>       - test <file> to see if it is a task
  /LJ <file>         - load task object from <file>
  /PJ                - print all properties of task
  /PRJ [<n>]         - print next <n> or today's remaining run times of task
  /PSJ [<id>]        - print trigger strings of <id> or all in task
  /PTJ [<id>]        - print trigger props of <id> or all in task
  /RJ                - run task
  /SAC <file>        - scheduler activate task (load tasks\<file>)
  /SAJ <file>        - scheduler add task (save as tasks\<file>)
  /SC <acct> <pwd>   - set task credentials
  /SCE <id>          - scheduler create enumerator in slot <id>=0..9
  /SD <file>         - scheduler delete task (delete tasks\<file>)
  /SE [<n>] [P]      - scheduler enum tasks <n> at a time, [P]rint
  /SJ <props>        - set task's properties
  /SM [<machine>]    - set machine (NULL = local machine if omitted)
  /SNJ <file>        - scheduler new task (replaces in-memory task object)
  /STJ [id] props    - set properties of task trigger <id> or 0
  /SVJ [<file>]      - save task to <file>
For detailed help use:
JT /? <command-name>, e.g. JT /? LJ.

Examples:

C:\>jt /se
[TRACE] Enumerating jobs and queues
  JSI005_State.job
  Windows Critical Update Notification.job

C:\>jt /se p
[TRACE] Enumerating jobs and queues
[TRACE] Activating job 'JSI005_State.job'
[TRACE] Printing all job properties

  ApplicationName:    'C:\Util\JSI005_State.bat'
  Parameters:         ''
  WorkingDirectory:   'C:\Util'
  Comment:            ''
  Creator:            'Jerry'
  Priority:           NORMAL
  MaxRunTime:         3600000 (0d  1:00:00)
  IdleWait:           10
  IdleDeadline:       60
  MostRecentRun:      07/18/2000 17:30:00
  NextRun:            07/19/2000 17:30:00
  StartError:         S_OK
  ExitCode:           0
  Status:             SCHED_S_TASK_READY
  ScheduledWorkItem Flags:
    DeleteWhenDone          = 0
    Suspend                 = 0
    StartOnlyIfIdle         = 0
    KillOnIdleEnd           = 0
    RestartOnIdleResume     = 0
    DontStartIfOnBatteries  = 0
    KillIfGoingOnBatteries  = 0
    RunOnlyIfLoggedOn       = 0
    SystemRequired          = 1
    Hidden                  = 0
  TaskFlags:          0

  1 Trigger

  Trigger 0:
    Type:            Daily
    DaysInterval:    1
    StartDate:       04/30/2000
    EndDate:         00/00/0000
    StartTime:       17:30
    MinutesDuration: 0
    MinutesInterval: 0
    Flags:
      HasEndDate      = 0
      KillAtDuration  = 0
      Disabled        = 0


[TRACE] Activating job 'Windows Critical Update Notification.job'
[TRACE] Printing all job properties

  ApplicationName:    'C:\WINNT\System32\wucrtupd.exe'
  Parameters:         ''
  WorkingDirectory:   'C:\WINNT\System32'
  Comment:            'This task is created by
Windows Critical Update Notification program
and should not be modified or removed.'
  Creator:            'Jerry'
  Priority:           NORMAL
  MaxRunTime:         259200000 (3d  0:00:00)
  IdleWait:           10
  IdleDeadline:       60
  MostRecentRun:      07/18/2000 12:15:59
  NextRun:            07/19/2000 13:16:00
  StartError:         S_OK
  ExitCode:           0
  Status:             SCHED_S_TASK_READY
  ScheduledWorkItem Flags:
    DeleteWhenDone          = 0
    Suspend                 = 0
    StartOnlyIfIdle         = 0
    KillOnIdleEnd           = 0
    RestartOnIdleResume     = 0
    DontStartIfOnBatteries  = 1
    KillIfGoingOnBatteries  = 0
    RunOnlyIfLoggedOn       = 1
    SystemRequired          = 0
    Hidden                  = 0
  TaskFlags:          0

  1 Trigger

  Trigger 0:
    Type:            Daily
    DaysInterval:    1
    StartDate:       07/19/2000
    EndDate:         00/00/0000
    StartTime:       13:16
    MinutesDuration: 1440
    MinutesInterval: 5
    Flags:
      HasEndDate      = 0
      KillAtDuration  = 0
      Disabled        = 0
		
JT.exe is apparently included in the windows 2000 resource kit.
Article ID:   W16548
File Created: 2013:01:11:10:34:16
Last Updated: 2013:01:11:10:34:16