Commandline Schtasks - Run once and Delete

Discussion in 'Windows Server' started by Jan K, Feb 25, 2005.

  1. Jan K

    Jan K Guest

    Hello there,

    I desperately need a way to create tasks via the commandline (bat file) that
    have the setting "Delete the task if it is not scheduled to run again"
    enabled (checked in the GUI).

    I have found no flag or switch in the documentation and helpfile for
    schtasks.exe. Is there any existing?

    Is there any other way to change the default setting so that all scheduled
    tasks are created with this setting checked (e.g. via a registry setting or
    similar)?

    A script is running and creating tasks up to 50 times per day, and the
    accumulation of the tasks that already has executed is not a desired feature
    that we want on a production server.

    Any help appreciated!
     
    Jan K, Feb 25, 2005
    #1
    1. Advertisements


  2. You can schedule the following batch to start when the computer starts.

    It then deletes all tasks that are scheduled to never start again.
    It wakes up every 15 minutes and checks again.

    @echo off
    setlocal ENABLEDELAYEDEXPANSION
    :again
    @ping -n 901 127.0.0.1>nul
    for /f "Tokens=*" %%a in ('schtasks /Query^|FIND "Never"') do (
    set task=%%a
    set work=!task:*Never=!
    set work=!work: =!
    set task=!Task:Never=!##
    set task=!task: =!
    set task=!task: ##=!
    set task=!task:##=!
    if "!work!" EQU "" SCHTASKS /DELETE /F /TN "!task!"
    )
    goto again

    Jerold Schulman
    Windows Server MVP
    JSI, Inc.
    http://www.jsiinc.com
     
    Jerold Schulman, Feb 25, 2005
    #2
    1. Advertisements


  3. I post a more accurate solution at tip 9100 in the 'Tips & Tricks' at http://www.jsiinc.com
    ( http://www.jsiinc.com/SUBS/tip9100/rh9100.htm )


    Jerold Schulman
    Windows Server MVP
    JSI, Inc.
    http://www.jsiinc.com
     
    Jerold Schulman, Feb 25, 2005
    #3
  4. Jan K

    Jan K Guest

    Hi Jerold,

    Thanks very much for the updated answer - that will definitely do the trick!

    (I'm sligthly disappointed thought that there's was no flag created, when
    the team developed the replacement for AT.EXE for setting the option to
    delete the task once it the task has been run - as it's within the GUI)

    Regards,
    Jan
     
    Jan K, Feb 27, 2005
    #4
  5. Jan K

    lowspeed Guest

    You know its weird if you go to the GUI sched tasks there is an option to
    delete the task if it only runs one time.

    Shouldn't there be a switch for command line as well ?


    Thanks,
    Oren.
     
    lowspeed, Feb 28, 2005
    #5
  6. Jan K

    Jan K Guest

    Hey Oren,

    Funnily enough, as a colleague of mine pointed out to me, there *IS* a
    switch for it, but here's the funny part: There seems to be different
    versions of SCHTASKS.EXE depending on which system you are running.

    For instance on my XP Professional system (fully patched, Service Pack II),
    the switch is non-existing and will result in an "Invalid Argument/Option"
    error, but on the Windows Server 2003 the switch is there (/z)!

    So basically, one could say that it's a case of RTFM but a slightly more
    sophisticated one!

    Jan
     
    Jan K, Feb 28, 2005
    #6
  7. Jan K

    lowspeed Guest

    Wow great stuff !

    I'm running win2003 server.

    Thanks !
     
    lowspeed, Feb 28, 2005
    #7
  8. Jan K

    lowspeed Guest

    Just a little follow up question - OT.

    Where can i find the log errors associated with the schedtasks ?


    Thanks,
    Oren.
     
    lowspeed, Feb 28, 2005
    #8
  9. Jan K

    Todd J Heron Guest

    In Control Panel open the Scheduled Tasks folder > on the Menu bar click
    Advanced > View Log.
     
    Todd J Heron, Feb 28, 2005
    #9
  10. Jan K

    lowspeed Guest

    Thanks,

    The doc on microsoft site is very limited hence the next question:

    Anyone know if there's a number limit on the tasks scheduled ?


    Thanks,
    Oren.
     
    lowspeed, Mar 1, 2005
    #10
  11. Jan K

    Todd J Heron Guest

    I have never seen or heard of a published limit.
     
    Todd J Heron, Mar 1, 2005
    #11
  12. Jan K

    Jan K Guest

    Just to clarify, the switch is /Z (not /z).

    Jan
     
    Jan K, Mar 1, 2005
    #12
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.