|
The Task Scheduler
The Task Scheduler is one of those tools that no one talks about because everyone assumes you already know about it. Just in case you missed it, the Task Scheduler is the “better” AT command to schedule programs, batch files, and other processes. The AT command-line program that comes with NT has few options and a poor interface. The Task Scheduler has a “wizard” style GUI as well as a scriptable COM interface. Microsoft Site Server provides the COM interface that opens up a world of opportunities. For example, you can write a VBScript (*.vbs) file that backs up your metabase every 10 minutes, every hour, every day, or anytime.
Installing the Task Scheduler
In order to use the Task Scheduler, you need to download it from http://www.microsoft.com/ie/download and choose Internet Explorer Add-ons. This will take you through a couple of screens where you will need to know your operating system and which add-ons you want.
Installing Site Server
The Task Scheduler from the IE Add-ons does not come with the COM interface. You need to download Site Server in order to get TaskScheduler.dll that provides the COM interfaces used in this article.
What is a Task?
A task is any program, batch file, command file, or file you want to run. Each task has a unique name. This name is also the filename with a "job" extension such as "calendar.job". Tasks are kept in the Tasks directory. The location of this directory is stored in the registry at:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\SchedulingAgent\TasksFolder
The Task Scheduler log file is usually called "SchedLog.Txt" and is kept in the Windows directory.
What is a Trigger?
A trigger is a set of rules that will cause the task to be executed. There are two types of triggers: time-based and event-based. Time-based triggers cause a task to be executed when the time/date of the trigger has been reached. Event-based triggers cause a task to be executed when certain system events occur such as start up, log on, or idle.
Opening the Wizard
To create a task with the Task Scheduler, you need to know a few things about the task. The following figures illustrate how to create a simple task. To start up the wizard, go to
Start | Programs | Administrative Tools | Scheduled Tasks. Figure 1 is the Task Scheduler with a couple of tasks currently scheduled. The wizard is the first item in the list, Add Scheduled Task.
Figure 1 - The Task Scheduler Interface Once you click on Add Scheduled Task, Figure 2 is the first screen of the window. Click on Next.
Figure 2 - The First Screen of the Task Scheduler
The next thing you need to do is pick a file to schedule. Figure 3 shows all the standard applications you can choose from. You can also browse for a file to schedule.
Figure 3 - Choose the File to Schedule
Once you have chosen the file, you need to choose the name of the task and frequency that the task is run. Figure 4 shows the most common occurrences. The advanced properties of each scheduled task can be much more granular than the options listed on this screen.
Figure 4 - Schedule for the Task
The next thing you need to do is schedule when the task starts. Figure 5 illustrates your choices.
Figure 5 - When to start the Task
This next screen is the most important in terms of running your applications unattended. Figure 6 allows you to enter a user context for the task. You must have a user context in which to run the task. However, the user context can be the Administrator account for NT. When your network is dependent on domain names and group permissions, it is important that each task runs within the security context of what it is being asked to do.
As an example of a security context, assume that the schedule task runs a VBScript file (*.vbs) to get information out of a database and generate a series of web pages each hour. The security context of this task must have adequate permissions to make the requests from the database. As a second example, assume that the task reads from a file that is stored on another machine’s NTFS drive. That file has permissions set in such a way that few people have access to it. The security context of the task must be on that permissions list in order for the task to complete successfully. Figure 6 is requesting the security context.
Figure 6 - Setting the Security Context
Figure 7 shows the choices you have made in the wizard and gives you the option to open the advanced properties. At this point a simple task has been scheduled and will show up in the list from Figure 1.
Figure 7 - Verify Task
Now that the task has been scheduled, you should know how to alter the task information or handle the advanced properties. The following images will take you through the windows that will appear if you choose either advanced options or double-click on the task from the list of tasks. Figure 8 shows you the file that you have scheduled as well as the comment describing the task and the security context. The check box at the box "Enabled" is set by default. If you uncheck this box, the task is still there but disabled, so it will not run for any trigger.
Figure 8 - Task Tab
The Schedule Tab shows the simple schedule properties. If you want more than 1 trigger for a task, the check box at the bottom of Figure 9 needs to be checked. This will allow you to
Figure 9 - Schedule Tab
The Schedule Tab button labeled Advanced… in Figure 10 gives you more granular control of the timing of the task. You can set the start date to some date in the future such as the first day of the month. The time can be set per range of minutes, hours, days, or months. You can also stop the task if it is still running the next time the task is supposed to start again.
Figure 10 - Advanced Schedule Options
The Settings Tab in figure 11 allows to you stop or delete the task.
Figure 11 - Settings Tab
That is all there is the Task Scheduler. Once you set the task you can go back to the list of tasks to verify the information. If you want to run the task right then (instead of waiting for the scheduled time), right click and choose Run.
Listing Tasks
In order to understand the properties of the Task, Sample 1 will show how
to list the current Tasks and their properties. The Task has three types of properties.
The first type of property is to define the task. This includes the JobName, Comment,
Creator (who created the task), Flags, Status, TriggerCount. The second type of properties
defines what is file or application is to run. These properties include ApplicationName, Parameters (such as command line parameters), WorkingDirectory (where the file or application is), AccountName (user context). The last type of property for the Task tracks the timing of the task. These properties are IdleWaitDeadLineMinutes, MaxRunTime, MostRecentRunTime, NextRunTime. These last two properties are determined by the Task Scheduler. The property Flags is a bitmask where its numeric value can represent more than a single value. Each single value sets a particular bit of the bitmask.
Listing the Properties of the Tasks
<HTML>
<BODY>
<%
Set m_objScheduler = Server.CreateObject(""Scheduler.SchedulingAgent.1"")
i=1
For Each objTask in m_objScheduler.Tasks
%>
<table>
<tr><td>(<%= i %>)</td>
<td>Name</td><td><% = objTask.JobName %></td></tr>
<tr><td>(<%= i %>)</td>
<td>Comment</td><td><% = objTask.Comment %></td></tr>
<tr><td>(<%= i %>)</td>
<td>Creator</td><td><% = objTask.Creator %></td></tr>
<tr><td>(<%= i %>)</td>
<td>ApplicationName</td><td>
<% = objTask.ApplicationName %></td></tr>
<tr><td>(<%= i %>)</td>
<td>Parameters</td><td><% = objTask.Parameters %></td></tr>
<tr><td>(<%= i %>)</td>
<td>WorkingDirectory</td><td>
<% = objTask.WorkingDirectory %></td></tr>
<tr><td>(<%= i %>)</td>
<td>AccountName</td><td><% = objTask.AccountName %></td></tr>
<tr><td>(<%= i %>)</td>
<td>IdleWaitDeadLineMinutes</td>
<td><% = objTask.IdleWaitDeadLineMinutes %></td></tr>
<tr><td>(<%= i %>)</td>
<td>MaxRunTime</td><td><% = objTask.MaxRunTime %></td></tr>
<tr><td>(<%= i %>)</td>
<td>MostRecentRunTime</td><td>
<% = objTask.MostRecentRunTime %></td></tr>
<tr><td>(<%= i %>)</td>
<td>NextRunTime</td><td><% = objTask.NextRunTime %></td></tr>
<%
sMessage = """"
sFlags = 0
sFlags = objTask.Flags
if sFlags And &H1 then
sMessage = sMessage & "" TASK_FLAG_INTERACTIVE<br>""
end if
if sFlags And &H2 then
sMessage = sMessage & "" TASK_FLAG_DELETE_WHEN_DONE<br>""
end if
if sFlags And &H4 then
sMessage = sMessage & "" TASK_FLAG_DISABLED<br>""
end if
if sFlags And &H10 then
sMessage = sMessage & "" TASK_FLAG_START_ONLY_IF_IDLE<br>""
end if
if sFlags And &H20 then
sMessage = sMessage & "" TASK_FLAG_KILL_ON_IDLE_END<br>""
end if
if sFlags And &H40 then
sMessage = sMessage & "" TASK_FLAG_DONT_START_IF_ON_BATTERIES<br>""
end if
if sFlags And &H80 then
sMessage = sMessage & "" TASK_FLAG_KILL_IF_GOING_ON_BATTERIES<br>""
end if
sMessage = sMessage & "" TASK_FLAG_RUN_ONLY_IF_DOCKED<br>""
end if
if sFlags And &H200 then
sMessage = sMessage & "" TASK_FLAG_HIDDEN<br>""
end if
if sFlags And &H400 then
sMessage = sMessage & "" TASK_FLAG_RUN_IF_CONNECTED_TO_INTERNET<br>""
end if
if sFlags And &H800 then
sMessage = sMessage & "" TASK_FLAG_RESTART_ON_IDLE_RESUME<br>""
end if
%>
<tr><td>(<%= i %>)</td><td>Flags</td>
<td><% = sMessage %>(<%= sFlags %>)</td></tr>
<%
sMessage = """"
sStatus = """"
sStatus = objTask.Status
Select Case sStatus
Case ""1""
sMessage = ""SCHED_S_TASK_READY""
Case ""2""
sMessage = ""SCHED_S_TASK_RUNNING""
Case ""3""
sMessage = ""SCHED_S_TASK_NOT_SCHEDULED""
End Select
%>
<tr><td>(<%= i %>)</td><td>Status</td><td><% = sMessage %>
</td></tr>
<tr><td>(<%= i %>)</td><td>Trigger Count</td><td>
<% = objTask.Triggers.Count %></td></tr>
</table>
<hr>
<%
i = i + 1
Next
Set m_objScheduler = Nothing
%>
</BODY>
</HTML>
Listing Triggers
For a better grasp of the properties of the Trigger, Sample 2 will show how to list the current Triggers for each Task. The properties of the Trigger are set so that the Task will run at certain times. If the Trigger is a daily trigger, the weeks and months properties would not need to be set. The trigger string is built from the other properties. The DaysOfTheWeek and Months properties both use bitmasks to represent more than a single value.
Listing the Properties of the Triggers
<HTML>
<BODY>
<%
on error resume next
Set m_objScheduler = Server.CreateObject("Scheduler.SchedulingAgent.1")
i=1
For Each objTask in m_objScheduler.Tasks
%>
<table border=1>
<tr><td>(<%= i %>)</td><td>Name</td><td><% = objTask.JobName %>
</td></tr>
<tr><td>(<%= i %>)</td><td>Trigger Count</td><td>
<% = objTask.Triggers.Count %></td></tr>
<%
j = 1
nTriggersCount = objTask.Triggers.Count
If (nTriggersCount <> 0) Then
For Each trigger in objTask.Triggers
%>
<tr><td>(<%= i %>)</td><td>Trigger String</td><td>
<% = trigger.TriggerString %></td></tr>
<%
sMessage = ""
sStatus = ""
sStatus = trigger.TriggerType
Select Case sStatus
Case "0"
sMessage = "TASK_TIME_TRIGGER_ONCE"
Case "1"
sMessage = "TASK_TIME_TRIGGER_DAILY"
Case "2"
sMessage = "TASK_TIME_TRIGGER_WEEKLY"
Case "3"
sMessage = "TASK_TIME_TRIGGER_MONTHLYDATE"
Case "4"
sMessage = "TASK_TIME_TRIGGER_MONTHLYDOW"
Case "5"
sMessage = "TASK_TIME_TRIGGER_ON_IDLE"
Case "6"
sMessage = "TASK_TIME_TRIGGER_AT_SYSTEMSTART"
Case "7"
sMessage = "TASK_TIME_TRIGGER_AT_LOGON"
End Select
%>
<tr><td>(<%= i %>)</td><td>Trigger Type</td><td>
<% = sMessage %></td></tr>
<tr><td>(<%= i %>)</td><td>Begin Time</td><td>
<% = trigger.BeginTime %></td></tr>
<tr><td>(<%= i %>)</td><td>End Time</td><td>
<% = trigger.EndTime %></td></tr>
<tr><td>(<%= i %>)</td><td>Duration</td><td>
<% = trigger.Duration %></td></tr>
<tr><td>(<%= i %>)</td><td>Days Interval</td><td>
<% = trigger.DaysInterval %></td></tr>
<tr><td>(<%= i %>)</td><td>Interval<br>
(happens every X minutes)</td><td><% = trigger.Interval %></td>
</tr>
<%
sFlags = 0
sMessage = ""
sFlags = trigger.DaysOfTheWeek
if sFlags And &H1 then
sMessage = sMessage & " Sunday<br>"
end if
if sFlags And &H2 then
sMessage = sMessage & " Monday<br>"
end if
if sFlags And &H4 then
sMessage = sMessage & " Tuesday<br>"
end if
if sFlags And &H8 then
sMessage = sMessage & " Wednesday<br>"
end if
if sFlags And &H10 then
sMessage = sMessage & " Thursday<br>"
end if
if sFlags And &H20 then
sMessage = sMessage & " Friday<br>"
end if
if sFlags And &H40 then
sMessage = sMessage & " Saturday<br>"
end if
%>
<tr><td>(<%= i %>)</td><td>Days of the Week</td>
<td><% = sMessage %>(<%= sFlags %>)</td></tr>
<%
sFlags = 0
sMessage = ""
sFlags = trigger.Months
if sFlags And &H1 then
sMessage = sMessage & " January<br>"
end if
if sFlags And &H2 then
sMessage = sMessage & " February<br>"
end if
if sFlags And &H4 then
sMessage = sMessage & " March<br>"
end if
if sFlags And &H8 then
sMessage = sMessage & " April<br>"
end if
if sFlags And &H10 then
sMessage = sMessage & " May<br>"
end if
if sFlags And &H20 then
sMessage = sMessage & " June<br>"
end if
if sFlags And &H40 then
sMessage = sMessage & " July<br>"
end if
if sFlags And &H80 then
sMessage = sMessage & " August<br>"
end if
if sFlags And &H100 then
sMessage = sMessage & " September<br>"
end if
if sFlags And &H200 then
sMessage = sMessage & " October<br>"
end if
if sFlags And &H400 then
sMessage = sMessage & " November<br>"
end if
if sFlags And &H800 then
sMessage = sMessage & " December<br>"
end if
%>
<tr><td>(<%= i %>)</td><td>Months</td><td>
<% = sMessage %>(<%= sFlags %>)</td></tr>
<tr><td>(<%= i %>)</td><td>WeeksInterval<br>
(happens every X weeks)</td><td><% = trigger.WeeksInterval %>
</td></tr>
<tr><td>(<%= i %>)</td><td>WhichWeek</td><td>
<% = trigger.WhichWeek %></td></tr>
<%
j = j + 1
Next
End if
%>
</table>
<hr>
<%
i = i + 1
Next
Set m_objScheduler = Nothing
%>
</BODY>
</HTML>
Creating a Task
In order to create a task, you don’t need to set all the properties (in fact, some are read-only). Listing 3 creates a task named “mytest.job” and associates it with the notepad application. If the Creator property was not set and the code is run from an ASP page, the Creator would be the user context for that page. For an anonymous site, that would be the IUSR_machinename account. For an NTLM site, the Creator would be the requesting user’s account.
Creating a Task
<%
Set oScheduler = Server.CreateObject("Scheduler.SchedulingAgent.1")
Set objTask = oScheduler.Tasks.Add("mytest.job")
objTask.ApplicationName = "D:\WINNT\system32\notepad.exe"
objTask.Comment = "this is my test task"
objTask.Creator = "ASP Page"
objTask.WorkingDirectory = "D:\WINNT\system32"
objTask.SetAccountInformation "domain\user","~Thursday"
Set oScheduler = Nothing
Set objTask = Nothing
%>
Creating a Trigger
Creating a Task doesn’t do much without a Trigger. You can set as many Triggers on a Task as you want. The code below creates a Trigger for the Task created above. This sample code is hard-coded to be a daily task but could easily be changed to a weekly or monthly task.
Creating a Trigger
<%
Set oScheduler = Server.CreateObject("Scheduler.SchedulingAgent.1")
strJobName = "mytest.job"
'Find Task so we can add trigger
Set objTask = objScheduler.Tasks.Item(strJobName)
Set objTrigger = objTask.Triggers.Add()
objTrigger.BeginTime = Now()
objTrigger.Flags = 0
objTrigger.Duration = 0
sTriggerType = "daily"
Select Case sTriggerType
Case "daily"
objTrigger.TriggerType = 1
objTrigger.DaysInterval = 1 'Every day
Case "weekly"
objTrigger.TriggerType = 2
objTrigger.WeeksInterval = 1 'Every week
objTrigger.DaysOfTheWeek = 2 'Monday
Case "monthly"
objTrigger.TriggerType = 3
objTrigger.Months = 4095 'Every month
Case Else
objTrigger.TriggerType = 0
End Select
Set objTrigger = Nothing
Set objTask = Nothing
%>
Running the Task
Once the Task and its Triggers have been created, you may want to run the task. The code below shows how to run a specific task.
Running the Task
<%
Set oScheduler = Server.CreateObject("Scheduler.SchedulingAgent.1")
'Find Task so we can add trigger
Set objTask = objScheduler.Tasks.Item("mytest.job")
objTask.Run
Set objTask = Nothing
Set oScheduler = Nothing
%>
Deleting the Task
The code below shows how to delete the Task.
Deleteing the Task
<%
Set oScheduler = Server.CreateObject("Scheduler.SchedulingAgent.1")
Set objTask = oScheduler.Tasks.Item("mytest.job")
oScheduler.Tasks.Delete(objTask.JobName)
Set oScheduler = Nothing
Set objTask = Nothing
%>
Summary
The Task Scheduler is a powerful tool to help manage or automate tasks on your machines. You can schedule any kind of task. Just imagine a tool that backs up your machine configuration for a web farm. This would include NT Users and Groups, NT Shares, NT Web Servers and virtual roots. Once the backup tool is built, you can run the tool every night and have the information stored on tape backup. Now when you want to add another server to your web farm, you just need to apply the configuration to the machine.
Further Reading
The NT Task Scheduler interfaces are documented for C++ development on MSDN and the Internet Client SDK has more information - http://www.microsoft.com/msdn/sdk/inetsdk/asetup/default.htm
Download
If you wish to download the samples provided you can do so from our FTP server at:
http://15seconds.com/files/980513.zip .
|