Announcement

Collapse
No announcement yet.

Scripting the creation of complex Scheduled Tasks

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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.

    I know I could create multiple tasks running the same process, each with it's own schedule.

    Anyone care to enlighten me?

  • #2
    Re: Scripting the creation of complex Scheduled Tasks

    Originally posted by Candy View Post
    I want to script the creation of a scheduled task with multiple schedules enabled, to run on XP and W2K3.
    I'd love scripting tasks too, but it is hard. WMI is the way, but sadly the Windows Task Scheduler is quite... limited.

    Quote from the article: "Task Scheduler can deal with tasks created by WMI, but WMI cannot deal with tasks created by the Task Scheduler. Even worse, suppose you use Task Scheduler to modify the task you originally created with WMI. That task will be successfully modified, but you’ll no longer be able to access it using WMI".

    How depressing.

    -vP

    Comment


    • #3
      Re: Scripting the creation of complex Scheduled Tasks

      You can try something like this:


      Code:
      Set wshShell = CreateObject("WScript.Shell")
      
      ' to schedule a task for the 15th days of May and June
      strCommand = "schtasks /create /tn ""My App"" /tr c:\apps\myapp.exe /sc monthly /d 15 /m MAY,JUN /st 15:00:00"
       
      retval = WshShell.Run(strCommand, 0, True)
      
        If retval = 0 Then
      	MsgBox "The task was successfully created"
        Else
      	strMessage ="There were problems creating the task. " & Err.Description
      	MsgBox strMessage, vbCritical + vbOKOnly, "Fatal Error"
        End If
      
      WScript.Quit(0)
      Also look here for more examples of how to use SCHTASKS


      I think what you may have to do is create 3 different tasks...

      Here is a better example for your first task:

      schtasks /create /tn ""My App"" /tr c:\apps\myapp.exe /sc weekly /d ""MON WED FRI"" /m MAY,JUN /st 06:00:00

      /SC WEEKLY ' Weekly
      /MO 1 ' Every week
      /D "MON WED FRI" ' run on Monday, Wed, Friday
      /ST 06:00:00 ' start at 6:00 AM
      Last edited by ekrengel; 13th July 2009, 20:03.

      Comment


      • #4
        Re: Scripting the creation of complex Scheduled Tasks

        Thanks for the responses.

        I have discovered a multitude of ways to create scheduled tasks but not one for multiple schedules in a single task. The example I gave was a simplified one and I would have to create dozens of tasks.

        I am thinking sendkeys or autoit may be my only solution. And that, in turn, means the install must be interactive at the very least.

        Comment


        • #5
          Re: Scripting the creation of complex Scheduled Tasks

          Do you have to use the show multiple schedules way? Or you just don't want to fill up scheduled tasks with 12 or more tasks for each machine you are doing this for? And want to consolidate it to look nice with 1?

          Because you can create more than 1 task by scripting.

          Example reading from a list of machines in a txt file:

          Code:
          @ECHO OFF
          
          :: 8pm Mon-Thu
          FOR /F %%M in (Machines.TXT) DO SchTasks /Create /S %%M
          	/SC WEEKLY /D "MON TUE WED THUR" /TN "MON-THUR JOB"
          	/TR "D:\MyBatch.BAT" /ST 20:00:00
          
          :: 10pm Fri
          FOR /F %%M in (Machines.TXT) DO SchTasks /Create /S %%M
          	/SC WEEKLY /D "FRI" /TN "FRI JOB"
          	/TR "D:\MyBatch.BAT" /ST 22:00:00
          
          :: 8am last day of every month
          FOR /F %%M in (Machines.TXT) DO SchTasks /Create /S %%M
          	/SC MONTHLY /MO LASTDAY /M * /TN "LAST MONTHLY JOB"
          	/TR "D:\MyBatch.BAT" /ST 08:00:00
          Last edited by ekrengel; 15th July 2009, 16:21. Reason: added example

          Comment


          • #6
            Re: Scripting the creation of complex Scheduled Tasks

            Originally posted by ekrengel View Post
            Do you have to use the show multiple schedules way? Or you just don't want to fill up scheduled tasks with 12 or more tasks for each machine you are doing this for? And want to consolidate it to look nice with 1?
            I have to do this for hundreds of servers across the globe. Multiple tasks become "interesting" when you need to suspend operation. If I have 12 tasks, I have to disable them all. The re-enable them afterwards. That means another script to manage task activation...

            Had some advice from elsewhere that may present a solution: Set up a task that fires every hour, 24/7. In the initial part of the script determine whether it is supposed to run now and continue/abort as required. That is to say, use Task Scheduler as an initiator and create my own fire or forget mechanism...

            Comment


            • #7
              Re: Scripting the creation of complex Scheduled Tasks

              Originally posted by Candy View Post
              I have to do this for hundreds of servers across the globe. Multiple tasks become "interesting" when you need to suspend operation. If I have 12 tasks, I have to disable them all. The re-enable them afterwards. That means another script to manage task activation...

              Had some advice from elsewhere that may present a solution: Set up a task that fires every hour, 24/7. In the initial part of the script determine whether it is supposed to run now and continue/abort as required. That is to say, use Task Scheduler as an initiator and create my own fire or forget mechanism...
              Well for the multiple machines, you can read from a txt file, so that solves that. For the re-enabling part, you can create arguments in the script perform different tasks...combined into 1 script. I'm not saying I know how to do this, I'm sure someone does (REMS ), but it can be done.

              By arguments I mean a second command when you run the script. So if the script is called "schedule.cmd", you could have a section of the script that executes a certain part when you use an argument. Example:

              schedule.cmd create
              schedule.cmd disablejobs
              schedule.cmd enablejobs

              EDIT**

              I just hacked this together...but I think it would go something like this (I'm not very good at batch scripting):


              Code:
              @ECHO OFF
              SETLOCAL
              
              SET HELP=Usage: schedule.bat createtasks | disabletasks | enabletasks
              
              IF "%1" == "" GOTO HELP
              IF "%1" == "createtasks" GOTO CREATETASKS
              IF "%1" == "disabletasks" GOTO DISABLETASKS
              IF "%1" == "enabletasks" GOTO ENABLETASKS
              
              
              :CREATETASKS
              REM 8pm Mon-Thu
              FOR /F %%M in (Machines.TXT) DO SchTasks /Create /S %%M
              	/SC WEEKLY /D "MON TUE WED THUR" /TN "MON-THUR JOB"
              	/TR "D:\MyBatch.BAT" /ST 20:00:00
              
              REM 10pm Fri
              FOR /F %%M in (Machines.TXT) DO SchTasks /Create /S %%M
              	/SC WEEKLY /D "FRI" /TN "FRI JOB"
              	/TR "D:\MyBatch.BAT" /ST 22:00:00
              
              REM 8am last day of every month
              FOR /F %%M in (Machines.TXT) DO SchTasks /Create /S %%M
              	/SC MONTHLY /MO LASTDAY /M * /TN "LAST MONTHLY JOB"
              	/TR "D:\MyBatch.BAT" /ST 08:00:00
              
              
              :: I think the below "/DISABLE" and "/ENABLE" are only for vista/server 2008, unless you
              :: know of another way...
              
              :DISABLETASKS
              FOR /F %%M in (Machines.TXT) DO SchTasks /DISABLE
              	/TN ("MON-THUR","FRI JOB","LAST MONTHLY JOB")
              
              :ENABLETASKS
              FOR /F %%M in (Machines.TXT) DO SchTasks /ENABLE
              	/TN ("MON-THUR","FRI JOB","LAST MONTHLY JOB")
              
              :HELP
              ECHO %HELP%
              
              :end
              Last edited by ekrengel; 15th July 2009, 18:41.

              Comment


              • #8
                Re: Scripting the creation of complex Scheduled Tasks

                I don't know of a SCHTASKS.exe /DISABLE switch, it is not documented.

                AFAIK.. If you like to Disable a job programmatically, you can use the dos command MOVE to move the "%windir%\tasks\dispayname.job" file to an other folder within the same volume.
                And move it back to the tasks folder to Enable it again.

                To set multiple schedules for the same task programmatically, it is common to use one script that will launch the jobs on the right time. Create a script that is scheduled to run every 30 minutes, the script get the current time and create a period starting 15 minutes before and ending 15 after the current time. it will test the hard coded date/times for the job to execute if it is in that period.
                You will have to create some functions in the script to translate the "last-day-of-month-TO-date" or the "##_strDay-of-Month-TO-date". Not very hard to script but can be tricky though.

                You can modify NTFS permissions on the "dispayname.job" file to 'hide' in the SCHEDULED TASKS pane from the logged user.


                Just my 2 cents,
                I can think of a way to distribute jobs scheduled with multiple date/times, although I have never seen someone using it nor have I tested it.
                It might be possible to create a task on a reference computer and distrubute the job-file to other computers. Run the Task as the buildin System.
                Most chance for success you'll get if the machienes all have the same OS and SP level in the same language and configured the same locales.
                Copy the file by keeping the ACL.


                \Rems

                This posting is provided "AS IS" with no warranties, and confers no rights.

                __________________

                ** Remember to give credit where credit's due **
                and leave Reputation Points for meaningful posts

                Comment


                • #9
                  Re: Scripting the creation of complex Scheduled Tasks

                  Yes they will most likely have to use that method because the disable/enable switch seems like it is only for Vista/Server 2008. Or just delete all the jobs, and have them re-created again, but I don't know how effective that is...

                  The disable/enable switch is documented HERE on MSDN. Scroll down towards the bottom of the page. "Windows XP and Windows Server 2003: This option is not available."

                  Is what I put together somewhat right?

                  Comment


                  • #10
                    Re: Scripting the creation of complex Scheduled Tasks

                    Originally posted by Rems View Post
                    I can think of a way to distribute jobs scheduled with multiple date/times, although I have never seen someone using it nor have I tested it.
                    It might be possible to create a task on a reference computer and distrubute the job-file to other computers. Run the Task as the buildin System.
                    Most chance for success you'll get if the machienes all have the same OS and SP level in the same language and configured the same locales.
                    Copy the file by keeping the ACL.
                    I had a similar idea but there are problems with this approach. The first is that the security context for the copied job becomes the credentials with which you copied it. That means it won't be running as NT Authority/SYSTEM any more unless you change it back.

                    More serious is that the times are stored as UTC which means they would all be wrong as soon as you push this to a server in another TZ.

                    After some reflection, I am going to go forwards with a task that is invoked every hour and let the script decide whether or not it needs to run at that point or not. Because of what the script is doing, I can store the run schedule centrally which means I can manage the schedules centrally with ease.

                    Thanks for all the suggestions, folks.

                    Comment

                    Working...
                    X