Windows Vista Forums

Scripting the creation of complex Scheduled Tasks
  1. #1


    Mike Short Guest

    Scripting the creation of complex Scheduled Tasks

    Hi,

    I want to script the creation of a scheduled task with multiple
    schedules enabled, to run on XP and W2K3.



    An example schedule: I have a task I want to run at 8pm Mon-Thu, 10pm
    Fri and at 8am on the last day of each month.

    This can be done easily enough from the Scheduled Tasks Applet (just
    check the Multiple Schedules checkbox from the task properties) but I
    can't find a way to script this. Well, OK, SendKeys or AutoIt or
    similar but that needs an interactive session.

    AT, SchTasks and Win32_ScheduledJob only seem to support a single
    schedule.

    I know I could create multiple tasks running the same process, each
    with it's own schedule but the above really is an example. I know I
    could create 3 tasks for the example but the real problem I have could
    potentially require a dozen or more schedules.

    Anyone got another solution?

      My System SpecsSystem Spec

  2. #2


    Bernie Guest

    Re: Scripting the creation of complex Scheduled Tasks

    From your example data, all tasks are starting on the hour. If that's
    consistant then you could have the script run every hour, on the hour, and
    check what needs to be done at that time. That would move the multiple
    operations from Scheduled Tasks and out into your script.

    Depending on how large your script gets and if you have trouble with the
    hourly testing for operation, maybe you could have a small tight script that
    checks for things to do that hour and then it envolkes another script that
    is longer and has all the operations in it.

    If you use Scheduled Tasks you said it could require a dozen or more. How
    many more? A dozen entries in the Task Scheduler I think would be much
    easier to manage than you reinventing the scheduling in your script. Have
    you tried the Task Scheduler solution?

    Bernie


    "Mike Short" <michael.short@xxxxxx> wrote in message
    news:bf7cb506-8dc2-4025-830a-e33ce17d34d7@xxxxxx

    > Hi,
    >
    > I want to script the creation of a scheduled task with multiple
    > schedules enabled, to run on XP and W2K3.
    >
    > An example schedule: I have a task I want to run at 8pm Mon-Thu, 10pm
    > Fri and at 8am on the last day of each month.
    >
    > This can be done easily enough from the Scheduled Tasks Applet (just
    > check the Multiple Schedules checkbox from the task properties) but I
    > can't find a way to script this. Well, OK, SendKeys or AutoIt or
    > similar but that needs an interactive session.
    >
    > AT, SchTasks and Win32_ScheduledJob only seem to support a single
    > schedule.
    >
    > I know I could create multiple tasks running the same process, each
    > with it's own schedule but the above really is an example. I know I
    > could create 3 tasks for the example but the real problem I have could
    > potentially require a dozen or more schedules.
    >
    > Anyone got another solution?


      My System SpecsSystem Spec

  3. #3


    Mike Short Guest

    Re: Scripting the creation of complex Scheduled Tasks

    > Depending on how large your script gets and if you have trouble with the

    > hourly testing for operation, maybe you could have a small tight script that
    > checks for things to do that hour and then it envolkes another script that
    > is longer and has all the operations in it.
    >
    That's a good idea. If no other solution presents itself, that would
    certainly work. Actually, something tells me that this is what I will
    end up having to do.

    > If you use Scheduled Tasks you said it could require a dozen or more. How
    > many more? A dozen entries in the Task Scheduler I think would be much
    > easier to manage than you reinventing the scheduling in your script. Have
    > you tried the Task Scheduler solution?
    The "how many more" is, er, probably not many more but the scheduling
    may need to be adapted. A dozen task scheduler entries isn't a
    problem until you want to suspend execution. I'd need to write
    another enable/disable script to make sure I got them all.

    I have tried the manual Task Scheduler solution and, yes, it works
    but... I have hundreds of target servers spread across the globe,
    needing different TZs and (depending on the working week there)
    different schedules. Automation is the watch-word here. It is a
    scripting question, after all.

      My System SpecsSystem Spec

  4. #4


    Larry Serflaten Guest

    Re: Scripting the creation of complex Scheduled Tasks


    "Mike Short" <michael.short@xxxxxx> wrote

    > Anyone got another solution?
    You could bit flag the tests and assign masks to the tasks.

    For example, you run a script every hour that has multiple tests.
    If its 8pm on a Mon-Thu, bit 1 of the schedule flag gets set.
    If its 1pm on a Friday, bit 2 gets set.
    If its 8am on the last day of the month, bit 3 gets set.

    Then you keep a list of tasks with their associated schedule masks.
    Any task that needs to run Mon-Thur, has bit 1 of its mask set.
    Any task that needs to run 1pm on Friday has bit 2 of its mask set.
    And so on. If it needs to run at both those times, then both bits are
    set, etc...

    Once your hourly script has run you have a schedule flag you can
    compare (using And) against the task masks. Any tasks whose
    mask has the same bit(s) set as the flag needs to run at that time.

    You could try to organize it, or you could get more complex (using
    and And mask and an Or mask) or you could just go ad hoc, adding
    new tests (and bit positions) as new schedules emerge. Anything you
    try is not going to be trivial, but certainly doable, depending on how
    much effort you want to spend at it....

    Just another idea....
    LFS




      My System SpecsSystem Spec

  5. #5


    Larry Serflaten Guest

    Re: Scripting the creation of complex Scheduled Tasks


    "Mark D. MacLachlan" <markdmac@xxxxxx> wrote

    > I think the only real solution you will have is to schedule your task
    > multiple times for each day and time that you want it to run.

    > CreateTask strComputer,"Notepad.exe","1300",Monday or Tuesday
    > CreateTask strComputer,"Notepad.exe","1100",Thursday or Friday
    How would that pick up something like, 8am on the last day of the month?
    (or the last Friday of the month, etc)

    While I noticed you used bit flags for the days of the week, I still think the OP
    would need to create separate tests for each schedule and use a flag to indicate
    the scheduled task(s) need to run. To do that, an overall manager would need to
    run every hour to perform the tests....

    LFS



      My System SpecsSystem Spec

  6. #6


    Mark D. MacLachlan Guest

    Re: Scripting the creation of complex Scheduled Tasks

    Larry Serflaten wrote:

    >
    > "Mark D. MacLachlan" <markdmac@xxxxxx> wrote
    >

    > > I think the only real solution you will have is to schedule your
    > > task multiple times for each day and time that you want it to run.
    >

    > > CreateTask strComputer,"Notepad.exe","1300",Monday or Tuesday
    > > CreateTask strComputer,"Notepad.exe","1100",Thursday or Friday
    >
    > How would that pick up something like, 8am on the last day of the
    > month? (or the last Friday of the month, etc)
    >
    > While I noticed you used bit flags for the days of the week, I still
    > think the OP would need to create separate tests for each schedule
    > and use a flag to indicate the scheduled task(s) need to run. To do
    > that, an overall manager would need to run every hour to perform the
    > tests....
    >
    > LFS
    Larry the code I posted was just a sample and would require further
    tweaking to setup specific dates and times. The key issue here I think
    is that the desire is to use a scripted solution while also utilizing
    the advanced features of the GUI interface which is not script enabled.

    One could grab the last day of the month in the following fashion:

    For M = 2 To 13
    If M < 13 Then
    FirstOfMonth = M & "/1/" & Year(Date)
    LastOfMonth = CDate(FirstOfMonth) -1
    WScript.Echo LastOfMonth
    Else
    LastOfMonth = "12/31/" & Year(Date)
    WScript.Echo LastOfMonth
    End IF
    Next

    There might be a more elegant way of doing that but that is what comes
    to my mind at the moment.

    Based on the data provided, I believe the requested solution is not
    possible as a scripted solution. Instead Mike would need to rethink
    the requirements and architect around what options are possible in the
    AT command.

    A suggested alternative would be to use AT to schedule the running of a
    VBScript that could then make the inteligent decisions that the AT
    command cannot.

    Regards,

    Mark D. MacLachlan


      My System SpecsSystem Spec

  7. #7


    Larry Serflaten Guest

    Re: Scripting the creation of complex Scheduled Tasks


    "Mark D. MacLachlan" <markdmac@xxxxxx> wrote

    > A suggested alternative would be to use AT to schedule the running of a
    > VBScript that could then make the inteligent decisions that the AT
    > command cannot.
    Bingo




      My System SpecsSystem Spec

  8. #8


    Dr J R Stockton Guest

    Re: Scripting the creation of complex Scheduled Tasks

    In microsoft.public.scripting.vbscript message <OCtRm4ZBKHA.1488@xxxxxx
    NGP03.phx.gbl>, Wed, 15 Jul 2009 15:46:52, Mark D. MacLachlan
    <markdmac@xxxxxx> posted:

    >
    >One could grab the last day of the month in the following fashion:
    >
    >For M = 2 To 13
    > If M < 13 Then
    > FirstOfMonth = M & "/1/" & Year(Date)
    > LastOfMonth = CDate(FirstOfMonth) -1
    > WScript.Echo LastOfMonth
    > Else
    > LastOfMonth = "12/31/" & Year(Date)
    > WScript.Echo LastOfMonth
    > End IF
    >Next
    >
    >There might be a more elegant way of doing that but that is what comes
    >to my mind at the moment.
    There is. Perhaps you missed my microsoft.public.scripting.vbscript
    message <W57ScIODO3UKFwCX@xxxxxx> of Tue, 7
    Jul 2009 17:10:43. Consider it, sig, and :-

    S = "<pre>"
    for J = 1 to 16
    S = S & J & " " & Day(DateSerial(2008, J+1, 0)) & VBCRLF
    next
    document.write S & "</pre>"
    ' It is convenient for me to write for a web page;
    ' changes for WSH should be easy enough. For full dates, omit Date().

    Also, your way uses a vile FFF date string, and gives me the final date
    in an alien format (my WinXP is set for ISO dates). That difference can
    be avoided by changing one of your lines to
    LastOfMonth = CDate("12/31/" & Year(Date))

    --
    (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm estrdate.htm vb-dates.htm pas-time.htm critdate.htm etc.

      My System SpecsSystem Spec

  9. #9


    Mike Short Guest

    Re: Scripting the creation of complex Scheduled Tasks

    OK, I think the way forward here is to schedule a task that is invoked
    every hour and then use the logic in the task to see whether or not it
    should do anything at that point.

    Thanks for all your input, it's been interesting and, hey, for once I
    didn't walk away feeling dumb for having missed something really
    obvious.

      My System SpecsSystem Spec

Scripting the creation of complex Scheduled Tasks problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Where are kept Scheduled tasks? Jack Vista General 8 17 Jan 2008
Scheduled Tasks Ulisses Righi Vista General 0 26 Jan 2007
Re: scheduled tasks Jimmy Brush Vista account administration 0 30 Sep 2006
scheduled tasks =?Utf-8?B?aWdsaW1wc2U=?= Vista performance & maintenance 1 19 Jul 2006
scheduled tasks =?Utf-8?B?aWdsaW1wc2U=?= Vista performance & maintenance 0 18 Jul 2006