The Task Scheduler consists
of multiple elements. The Task Scheduler application is actually a
Windows service. Because Task Scheduler is a service, it's always
present in the background so it can run applications automatically for
you.
As a safety precaution,
it's a good idea to set the service to manual and manually start it as
needed. To set the Task Scheduler to start manually, type SC Config Schedule Start= Demand and press Enter. Notice the space between the equals sign (=)
and the value. Of course, if you're running tasks every day, the
automatic start option makes more sense. In this case, you type SC Config Schedule Start= Auto and press Enter. When you want to determine the Task Scheduler status, type SC QueryEx Schedule and press Enter. Use QueryEx, rather than Query,
so that you obtain the Process Identifier (PID) as part of the
output—many command line utilities require the PID as input. To start
the Task Scheduler, type SC Start Schedule and press Enter.
The Task Scheduler logs on
using the Local System account. Normally, this account provides more
than sufficient rights for local activities. However, when you
automatically perform tasks on remote machines, you might need to change
the account to handle the increased security requirements. Always make
sure you have the Task Scheduler service running when you need to
execute applications automatically. To change the password to another
account, type SC Config Schedule Obj= ".\AccountName" Password= "AccountPassword"
and press Enter. Notice that the account name includes both the domain
and the username. The use of a period indicates the location machine. To
change the service account back to Local System, type SC Config Schedule Obj= ".\LocalSystem" Password= ""
and press Enter. It's important to note that Server Core tightens
security considerably, so you need to consider the effect of the new
security measures on any tasks you want to perform. In many cases,
you'll need to change the security settings of any jobs that you want to
move from other servers to Server Core to ensure they'll run correctly.
The second part of Task Scheduler is a graphical utility called Scheduled Tasks that you'll normally find in the Start => Programs => Accessories =>
Systems Tools folder on a GUI system. This utility displays all of the
tasks you've scheduled. You can also use it to create new tasks, either
directly or by using the supplied wizard. This second part of the Task
Scheduler doesn't exist in Server Core.
Standard Server Core
tasks aren't visible to previous versions of Windows (they are visible
to Vista users). To create a scheduled task in Server Core that's
visible to previous versions of Windows, you must use the SchTasks
utility with the /V1 command line switch . You can also modify the Configure
For field found on the General tab of any standard task you create to
support other Windows versions. The Configure For field only contains
Vista as an option when you create a basic task.
|
|
The third part of the
Task Scheduler is a number of related utilities that are discussed in
the sections that follow. These utilities make it considerably easier to
schedule multiple tasks or to schedule tasks for multiple users. You
can use these utilities from a remote location to ensure user machines
perform required maintenance tasks automatically at a time that's
convenient for the user. Because operation is automatic, you don't have
to worry about users performing the task incorrectly, inconsistently, or
not at all.
Scripts can have
payoffs other than increased personal productivity. For example,
Microsoft regularly offers incentives for submitting scripts to the
Community-Submitted Scripts Center at http://www.microsoft.com/technet/scriptcenter/csc/default.mspx. At the time of this writing, you can obtain a free e-learning course.
|
|
1. Managing Tasks with the SCHTasks Command
The Schedule Tasks
(SchTasks) Command helps you schedule tasks at the command line. The
basic command line requires that you provide a parameter (an action or
command) and any arguments that the parameter requires. The list of
recognized parameters includes /Create, /Delete, /Query, /Change, /Run, and /End. The following sections describe each of these parameters.
/Create
The /Create parameter helps you create new tasks. Never use this parameter to change an existing task; use the /Change parameter instead. This parameter uses the following syntax:
SCHTASKS /Create [/S system [/U username [/P password]]] [/RU username
[/RP password]] /SC schedule [/MO modifier] [/D day] [/I idletime]
/TN taskname /TR taskrun [/ST starttime] [/M months] [/SD startdate]
[/ED enddate] [/RI interval] [ {/ET endtime | /DU duration} [/K]
[/XML xmlfile] /V1]] [/IT | /NP] [/Z] [/F]
The following list describes each of the standard command line arguments.
/S
systemSpecifies the remote system that you want to check. In most cases, you'll also need to supply the /U and the /P command line switches when using this switch.
/U [domain]userSpecifies the
username on the remote system. This name may not match the username on
the local system. You'll need to supply a domain name when working with a
domain controller.
/P [password]Specifies the
password for the given user. You can provide the command line switch
without specifying the password on the command line in cleartext. The
system prompts you for the password. Using this feature can help you
maintain the security of passwords used on your system.
/RU
usernameSpecifies the user account or user context that you want to use to run the task. Use "", "NT AUTHORITY\SYSTEM", or "SYSTEM" to use the system account to run the task.
/RP
passwordSpecifies the
password of the user account or user context you want to use to run the
task. Use an asterisk (*) or a blank (nothing after the command line
switch) in place of the password when you want the system to prompt you
for the password. This command line switch has no effect when you use
the system account to run the task.
/SC schedule Defines the
frequency at which the system runs the task. The valid frequencies
include MINUTE, HOURLY, DAILY, WEEKLY, MONTHLY, ONCE, ONSTART, ONLOGON,
and ONIDLE.
/MO
modifierRefines the schedule
type to provide finer control over the schedule frequency. The modifier
you use depends on the scheduling frequency. The following list
describes each of the modifiers.
- MINUTE Every 1–1,439 minutes
- HOURLY Every 1–23 hours
- DAILY Every 1–365 days
- WEEKLY Every 1–52 weeks
- ONCE No modifiers
- ONSTART No modifiers
- ONLOGON No modifiers
- ONIDLE No modifiers
- MONTHLY Every 1 to 12 months, or FIRST, SECOND, THIRD, FOURTH, LAST, or LASTDAY
/D
daysSpecifies the day
of the week to run the task. Acceptable values include MON, TUE, WED,
THU, FRI, SAT, and SUN. When working with a monthly schedule, you can
specify date numbers between 1 and 31.
/M
monthsSpecifies the month
of the year to run a task. The default setting is the current month.
Acceptable values include JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP,
OCT, NOV, and DEC.
/I
idletimeSpecifies the
amount of time to wait before running a scheduled ONIDLE task. Idle time
is when no other application on the system is running (the system idle
process is using all of the computer resources). The acceptable range of
values is 1 to 999 minutes.
/TN
tasknameSpecifies the name of the task. The utility requires a unique name for the purpose of identification.
/TR
taskrunSpecifies the full path and filename of the task that you want to run. Never assume that the task will run based on a PATH
environment variable because you can't guarantee the user will have the
environment variable defined. You can also specify any command line
arguments that the task requires. Enclose tasks with spaces in quotes.
/ST
starttimeSpecifies the time
that you want the task to run. The time format is HH:MM:SS (24-hour
time). Don't use a 12-hour time format that includes AM and PM. The
default setting is the current time.
/SD
startdateSpecifies the
first date on which to run the task. The date format is mm/dd/yyyy.
Always include a 4-digit year. The default setting is the current day.
/ED
enddateSpecifies the last
date that you want to run the task. The date format is mm/dd/yyyy.
Always include a 4-digit year. The default is not to use an end time, so
the task continues to run indefinitely.
/RI
intervalSpecifies the
repetition interval of the task in minutes. You can't use this feature
with schedule types of MINUTE, HOURLY, ONSTART, ONLOGON, ONIDLE, and
ONEVENT. The valid range for this argument is 1 to 599,940 minutes. If
you specify either the /ET or /DE command line arguments, then the default interval is 10 minutes.
/ET
endtimeSpecifies the end
time of the task. The time format is HH:MM:SS (24-hour time). Don't use a
12-hour time format that includes AM and PM. You can't use this feature
with schedule types of ONSTART, ONLOGON, ONIDLE, and ONEVENT. It also
isn't possible to use this feature with the /DU option.
/DU
durationSpecifies the duration
of the task. The time format is HH:MM (a time interval in hours and
minutes, not a specific time). You can't use this feature with schedule
types of ONSTART, ONLOGON, ONIDLE, and ONEVENT. It also isn't possible
to use this feature with the /ET option. When you specify the /RI argument with a /V1 task, the default duration is 1 hour.
/K Automatically
terminates the task at the end time or after the duration interval
expires. You can't use this feature with schedule types of ONSTART,
ONLOGON, ONIDLE, and ONEVENT. This argument is only applicable when you
specify either the /ET or /DU command line arguments.
/XML
xmlfileCreates a task based on the task XML in the specified file. Combine this command line switch with the /RU and /RP
command line switches to specify a complete entry (including security).
The task XML can also contain the principal, which means that you only
supply the /RU command line switch.
/V1 Creates a task that's
compatible with pre-Vista versions of Windows. If you don't use this
command line switch on a Vista system, then other versions of Windows
can't see the task you create. You can't use this command line switch
with /XML.
/IT Forces the task
to run interactively only. The job runs when the user is logged on, but
Vista and above ignores it when the user is logged out.
/NP Disables password
storage. The job runs noninteractively using the credentials of the
specified user. The job can only access local resources. Using this
command line switch improves security by not storing the password and by
reducing task access to external resources.
/Z Marks the task
for deletion after its final run (as specified in the schedule you
provide). The job isn't actually deleted—Vista only marks it for
deletion.
/F Forces task creation
even if a task by that name already exists on the system. Vista and
above doesn't display any warning messages, it simply overwrites the
existing job.
/Delete
The /Delete parameter deletes tasks that you no longer want to run. This parameter uses the following syntax:
SCHTASKS /Delete [/S system [/U username [/P password]]] /TN taskname [/F]
The following list describes each of the command line arguments.
/S
systemSpecifies the remote system that you want to check. In most cases, you'll also need to supply the /U and the /P command line switches when using this switch.
/U [domain\]userSpecifies the
username on the remote system. This name may not match the username on
the local system. You'll need to supply a domain name when working with a
domain controller.
/P [password]Specifies the
password for the given user. You can provide the command line switch
without specifying the password on the command line in cleartext. The
system prompts you for the password. Using this feature can help you
maintain the security of passwords used on your system.
/TN
tasknameSpecifies the name of the task you want to delete. You can delete all tasks using the asterisk (*) wildcard.
/F Forces the utility
to delete the task and suppress any warnings when the task is currently
running. Use this command line switch only when the task is frozen or
unresponsive. Using this command line switch can result in data loss or
other unexpected system behavior.
/Query
The /Query parameter requests information about any tasks you've created. This parameter uses the following syntax:
SCHTASKS /Query [/S system [/U username [/P password]]]
[/FO format | /XML] [/NH] [/V] [/?] [/TN taskname]
The following list describes each of the command line arguments.
/S
systemSpecifies the
remote system that you want to check. In most cases, you'll also need to
supply the /U and the /P command line switches when using this switch.
/U [domain\]userSpecifies the
username on the remote system. This name may not match the username on
the local system. You'll need to supply a domain name when working with a
domain controller.
/P [password]Specifies the
password for the given user. You can provide the command line switch
without specifying the password on the command line in cleartext. The
system prompts you for the password. Using this feature can help you
maintain the security of passwords used on your system.
/FO {TABLE | LIST | CSV} Defines the
output provided by the utility. The table format is normally the easiest
to view on screen. The table columns define the values for output,
while each row contains one driver entry. The CSV output provides the
best method for preparing the data for entry in a database. Use
redirection to output the CSV data to a file and then import it to
your database. The list format provides one data element per line. Each
group of data elements defines one driver. The utility separates each
driver by one blank line. Some people find the list format more readable
when working in verbose mode since the table format requires multiple
lines for each entry (the lines wrap).
/XML Displays the
output in XML format. This command line switch is useful when you want
to learn more about creating jobs using XML files.
/NH Forces the
utility to display the data without a column header. You can only use
this command line switch with the table and CSV formats. Omitting the
header makes it easier to incorporate the data in a report or import it
into a database.
/V Displays detailed
data about each of the defined tasks. The standard display shows only
TaskName, Next Run Time, and Status. In addition to this basic
information, the verbose display shows HostName, Last Run Time, Last
Result, Creator, Schedule, Task To Run, Start In, Comment, Scheduled
Task State, Scheduled Type, Start Time, Start Date, End Date, Days,
Months, Run As User, Delete Task If Not Rescheduled, Stop Task If Runs X
Hours and X Mins, Repeat: Every, Repeat: Until: Time, Repeat: Until:
Duration, Repeat: Stop If Still Running, Idle Time, and Power
Management.
/TN
tasknameSpecifies the name of
the task you want to query. Vista and above displays all of the tasks
when you don't supply this parameter.
/Change
The /Change
parameter changes an existing task. You can't use this parameter to
create a new task. However, this parameter does work well to indicate a
change in task location. This parameter uses the following syntax:
SCHTASKS /Change [/S system [/U username [/P [password]]]] [/RU
runasuser] [/RP runaspassword] [/TR taskrun] /TN taskname [/ST
starttime] [/RI interval] [ {/ET endtime | /DU duration} [/K] ]
[/SD startdate] [/ED enddate] [/ENABLE | /DISABLE] [/IT] [/Z] }
The following list describes each of the command line arguments.
/S
systemSpecifies the remote system that you want to check. In most cases, you'll also need to supply the /U and the /P command line switches when using this switch.
/U [domain\]userSpecifies the
username on the remote system. This name may not match the username on
the local system. You'll need to supply a domain name when working with a
domain controller.
/P [password]Specifies the
password for the given user. You can provide the command line switch
without specifying the password on the command line in cleartext. The
system prompts you for the password. Using this feature can help you
maintain the security of passwords used on your system.
/RU
usernameSpecifies the user account or user context that you want to use to run the task. Use "", "NT AUTHORITY\SYSTEM", or "SYSTEM" to use the system account to run the task.
/RP
passwordSpecifies the
password of the user account or user context you want to use to run the
task. Use an asterisk (*) or a blank (nothing after the command line
switch) in place of the password when you want the system to prompt you
for the password. This command line switch has no effect when you use
the system account to run the task.
/TR
taskrunSpecifies the full
path and filename of the new task that you want to run. The new task
need not be a new execution; it may simply be a new version or a version
of the same executable in a different location. Never assume that the
task will run based on a PATH environment
variable because you can't guarantee the user will have the environment
variable defined. You can also specify any command line arguments that
the task requires. Enclose tasks with spaces in quotes.
/TN
tasknameSpecifies the name of the task to change.
/ST
starttimeSpecifies the time
that you want the task to run. The time format is HH:MM:SS (24-hour
time). Don't use a 12-hour time format that includes AM and PM. The
default setting is the current time.
/RI
intervalSpecifies the
repetition interval of the task in minutes. You can't use this feature
with schedule types of MINUTE, HOURLY, ONSTART, ONLOGON, ONIDLE, and
ONEVENT. The valid range for this argument is 1 to 599,940 minutes. If
you specify either the /ET or /DE command line arguments, then the default interval is 10 minutes.
/ET
endtimeSpecifies the end
time of the task. The time format is HH:MM:SS (24-hour time). Don't use a
12-hour time format that includes AM and PM. You can't use this feature
with schedule types of ONSTART, ONLOGON, ONIDLE, and ONEVENT. It also
isn't possible to use this feature with the /DU option.
/DU
durationSpecifies the
duration of the task. The time format is HH:MM (a time interval in hours
and minutes, not a specific time). You can't use this feature with
schedule types of ONSTART, ONLOGON, ONIDLE, and ONEVENT. It also isn't
possible to use this feature with the /ET option. When you specify the /RI argument with a /V1 task, the default duration is 1 hour.
/K Automatically
terminates the task at the end time or after the duration interval
expires. You can't use this feature with schedule types of ONSTART,
ONLOGON, ONIDLE, and ONEVENT. This argument is only applicable when you
specify either the /ET or /DU command line arguments.
/SD
startdateSpecifies the first
date on which to run the task. The date format is mm/dd/yyyy. Always
include a 4-digit year. The default setting is the current day.
/ED
enddateSpecifies the last
date that you want to run the task. The date format is mm/dd/yyyy.
Always include a 4-digit year. The default is not to use an end time, so
the task continues to run indefinitely.
/ENABLE Enables the scheduled task (so it can run).
/DISABLE Disables the schedule task (it can't run, even when scheduled).
/IT Forces the task to
run interactively only. The job runs when the user is logged on, but
Vista and above ignores it when the user is logged out.
/Z Marks the task for
deletion after its final run (as specified in the schedule you
provide). The job isn't actually deleted—Vista and above only marks it
for deletion.
/Run
The /Run
parameter runs an existing task, even if the task isn't scheduled to
run. Running a task using this method doesn't change the task schedule;
the task runs using the schedule you set for it. You can use this
feature to test your tasks to ensure they work as anticipated. Check the
\Windows\SchedLgU.TXT file for the results of any task you run, especially on remote machines. This parameter uses the following syntax:
SCHTASKS /Run [/S system [/U username [/P password]]] /TN taskname
The following list describes each of the command line arguments.
/S
systemSpecifies the remote system that you want to check. In most cases, you'll also need to supply the /U and the /P command line switches when using this switch.
/U [domain\]userSpecifies the
username on the remote system. This name may not match the username on
the local system. You'll need to supply a domain name when working with a
domain controller.
/P [password]Specifies the
password for the given user. You can provide the command line switch
without specifying the password on the command line in cleartext. The
system prompts you for the password. Using this feature can help you
maintain the security of passwords used on your system.
/TN
tasknameSpecifies the name
of the task you want to run. Running a task at the command line is
different from having the Task Scheduler run it for you. Using this
option forces the utility to use your credentials, rather than the Task
Scheduler credentials, which means that an executable that normally
works could fail when used in this way.
/End
The /End
parameter forces a task to stop, even if the task is in the middle of
processing data. Consequently, you should use this parameter with care
to avoid data loss or odd system behavior. You can only use this
parameter to end programs started with the Task Scheduler. This parameter uses the following syntax:
SCHTASKS /End [/S system [/U username [/P password]]] /TN taskname
The following list describes each of the command line arguments.
/S
systemSpecifies the remote system that you want to check. In most cases, you'll also need to supply the /U and the /P command line switches when using this switch.
/U [domain\]userSpecifies the
username on the remote system. This name may not match the username on
the local system. You'll need to supply a domain name when working with a
domain controller.
/P [password]Specifies the
password for the given user. You can provide the command line switch
without specifying the password on the command line in cleartext. The
system prompts you for the password. Using this feature can help you
maintain the security of passwords used on your system.
/TN
tasknameSpecifies the name of the task you want to end.
|