如何在Windows中使用Schtasks从命令行计划任务?

Recurring tasks are generally schedules with scheduled task manager. Scheduled task manager have GUI for management but creating a task in 20 different servers is very hard and trivial job. Windows operating systems provide tool called schtasks which is used to create, modify, delete scheduled tasks.

周期性任务通常是使用计划任务管理器进行的计划。 预定的任务管理器具有用于管理的GUI,但是在20个不同的服务器中创建任务非常困难且琐碎。 Windows操作系统提供了称为schtasks工具,该工具用于创建,修改,删除计划的任务。

帮帮我 (Help)

Help  about scheduling tasks command schtasks can be printed with /? option.

有关计划任务的帮助,命令schtasks可以使用/?打印/? 选项。

$ schtasks /?
Help
Help
帮帮我

句法(Syntax)

While using command we will use following syntax.

在使用命令时,我们将使用以下语法。

SCHTASKS /parameter [arguments]

创建计划任务/作业 (Create A Scheduled Task/Job)

In order to create a scheduled task we need to provide following parameters at least.

为了创建计划任务,我们至少需要提供以下参数。

  • /SC specifies

    /SC指定

  • /TN specifies task name which will identify task from other tasks

    /TN指定任务名称,该名称将标识其他任务中的任务

  • /TR specifies the tasks executable file or command.

    /TR指定任务可执行文件或命令。

In this example we will create a task named defrag which will run c:\windows\system32\defrag.exe at weekly periods.

在此示例中,我们将创建一个名为defrag的任务,该任务将每周运行c:\windows\system32\defrag.exe

$ Schtasks /create  /SC weekly  /TN defrag /TR c:\windows\system32\defrag.exe
Create A Scheduled Task/Job
Create A Scheduled Task/Job
创建计划的任务/工作

Schtasks属性(Schtasks Attributes)

While creating scheduled jobs we can providing following attributes.

在创建计划的作业时,我们可以提供以下属性。

  • /S system Specifies the remote system to connect to.

    / S系统指定要连接的远程系统。
  • /U username Specifies the user context under which schtasks.exe

    / U用户名指定schtasks.exe所在的用户上下文

    should execute.

    应该执行。

  • /P [password] Specifies the password for the given user context.

    / P [密码]指定给定用户上下文的密码。

    Prompts for input if omitted.

    如果省略,则提示输入。

  • /TN taskname Specifies which scheduled task to change.

    / TN任务名称指定要更改的计划任务。
  • /RU username Changes the user name (user context) under which the

    / RU username更改用户名(用户上下文),

    scheduled task has to run. For the system account,

    预定任务必须运行。 对于系统帐户,

    valid values are “”, “NT AUTHORITY\SYSTEM” or “SYSTEM”.

    有效值为“”,“ NT AUTHORITY \ SYSTEM”或“ SYSTEM”。

    For v2 tasks, “NT AUTHORITY\LOCALSERVICE” and

    对于v2任务,“ NT AUTHORITY \ LOCALSERVICE”和

    “NT AUTHORITY\NETWORKSERVICE” are also available as well

    也可以使用“ NT AUTHORITY \ NETWORKSERVICE”

    as the well known SIDs for all three.

    作为这三个的众所周知的SID。

    /RP password Specifies a new password for the existing user

    / RP密码为现有用户指定新密码

    context or the password for a new user account.

    上下文或新用户帐户的密码。

    This password is ignored for the system account.

    系统帐户将忽略此密码。

  • /TR taskrun Specifies the new program that the

    / TR taskrun指定新程序

    scheduled task will run.

    计划的任务将运行。

  • /ST starttime Specifies the start time to run the task. The time

    / ST starttime指定运行任务的开始时间。 时间

    format is HH:mm (24 hour time) for example, 14:30

    格式为HH:mm(24小时制),例如14:30

    for 2:30 PM.

    下午2:30。

  • /RI interval Specifies the repetition interval in

    / RI interval指定重复间隔

    minutes. Valid range: 1 – 599940 minutes.

    分钟。 有效范围:1 – 599940分钟。

  • /ET endtime Specifies the end time to run the task. The time

    / ET结束时间指定运行任务的结束时间。 时间

    format is HH:mm (24 hour time) for example, 14:50

    格式为HH:mm(24小时制),例如14:50

    for 2:50 PM.

    下午2:50。

  • /DU duration Specifies the duration to run the task. The time

    / DU持续时间指定运行任务的持续时间。 时间

    format is HH:mm. This is not applicable with /ET.

    格式为HH:mm。 这不适用于/ ET。

  • /K Terminates the task at the endtime or duration time.

    / K在结束时间或持续时间终止任务。
  • /SD startdate Specifies the first date on which the task runs.

    / SD开始日期指定任务运行的第一个日期。

    The format is mm/dd/yyyy.

    格式是mm / dd / yyyy。

  • /ED enddate Specifies the last date when the task should run.

    / ED enddate指定任务应运行的最后日期。

    The format is mm/dd/yyyy.

    格式是mm / dd / yyyy。

  • /IT Enables the task to run interactively only if the

    / IT仅在以下情况下启用任务以交互方式运行:

    /RU user is currently logged on at the time the job

    / RU用户当前在作业时登录

    runs. This task runs only if the user is logged in.

    运行。 仅当用户登录时,此任务才运行。

  • /RL level Sets the Run Level for the job. Valid values are

    / RL级别设置作业的运行级别。 有效值为

    LIMITED and HIGHEST. The default is to not change it.

    限量和最高。 默认为不更改。

  • /ENABLE Enables the scheduled task.

    / ENABLE启用计划的任务。
  • /DISABLE Disables the scheduled task.

    / DISABLE禁用计划的任务。
  • /Z Marks the task for deletion after its final run.

    / Z将任务标记为要在最终运行后删除。
  • /DELAY delaytime Specifies the wait time to delay the running of the

    / DELAY delaytime指定等待时间以延迟运行

    task after the trigger is fired. The time format is

    触发器触发后执行任务。 时间格式为

    mmmm:ss. This option is only valid for schedule types

    mmmm:ss。 此选项仅对计划类型有效

    ONSTART, ONLOGON, ONEVENT.

    ONSTART,ONLOGON和ONEVENT。

LEARN MORE  How To Set Job With Different User In Crontab?
了解更多信息如何在Crontab中与其他用户一起设置作业?

创建具有更多选项的计划任务/作业 (Create A Scheduled Tasks/Job With More Options)

In previous example we have used to very little options. We can provide more needed attributes. In this example we will provide

在前面的示例中,我们很少使用选项。 我们可以提供更多所需的属性。 在此示例中,我们将提供

  • /RU for the username

    /RU作为用户名

  • /RP for password

    /RP输入密码

  • /SC for period

    /SC期间

  • /D‘ for date

    /D '日期

  • /TN for task name

    /TN为任务名称

  • /TR for executable file or command

    /TR表示可执行文件或命令

  • /ST for time

    /ST时间

$ Schtasks /create /RU jack /RP 123456 /SC weekly /D SAT /TN defrag /TR c:\windows\system32\defrag.exe /ST 10:00:00
Create A Scheduled Tasks/Job With More Options
Create A Scheduled Tasks/Job With More Options
创建具有更多选项的计划任务/作业

列出所有准备就绪的计划任务/作业(List All Ready Created Scheduled Tasks/Jobs)

In a IT environment there will be a lot of existing scheduled jobs. We can list these existing tasks just issuing schtasks like below.

在IT环境中,将有许多现有的预定作业。 我们可以像下面那样发布schtasks来列出这些现有任务。

$ schtasks
List All Ready Created Scheduled Tasks/Jobs
List All Ready Created Scheduled Tasks/Jobs
列出所有准备就绪的计划任务/作业

For each task following information is printed.

对于每个任务,将打印以下信息。

  • Folder shows namespace based location.

    Folder显示基于名称空间的位置。

  • Taskname shows given name for the task to identify

    Taskname显示给定名称为任务,以确定

  • Next Run Time shows when will the task will run for the next time

    Next Run Time显示任务何时下次运行

  • Status shows whether the task is ready to run or running or there is a problem.

    Status显示任务是准备运行还是正在运行或存在问题。

删除计划的任务/工作 (Delete Scheduled Task/Job)

All ready scheduled jobs can be deleted if we do not need it. We can delete task with /delete option and specifying the task name. In this example we will delete task named degfrag which is specified with /TN option.

如果我们不需要所有准备好的预定作业,则可以将其删除。 我们可以使用/delete选项删除任务并指定任务名称。 在此示例中,我们将删除名为degfrag任务,该任务由/TN选项指定。

$ schtasks /delete /TN defrag
Delete Scheduled Task/Job
Delete Scheduled Task/Job
删除计划的任务/工作

While deleting we need to confirm the delete operation. If the task is deleted properly we will get a SUCCESS message.

删除时,我们需要确认删除操作。 如果正确删除了任务,我们将收到一条SUCCESS消息。

删除所有计划的任务/作业 (Delete All Scheduled Task/Jobs)

If we do not need all of the scheduled jobs and we want to delete them in a single shot we can use asterisks * sign to specify task name.

如果我们不需要所有计划的作业,并且想一次删除它们,则可以使用星号*号指定任务名称。

$ schtasks /delete /TN *

禁用计划的任务/作业 (Disable Scheduled Task/Job)

Some time we may need to disable scheduledjob for a short period. We can use /change option with /disable option in order to disable a scheduled job. In this example we will disable the job named defrag .

有时我们可能需要在短时间内禁用Scheduledjob。 我们可以将/change选项与/disable选项一起使用,以禁用计划的作业。 在此示例中,我们将禁用名为defrag的作业。

$ schtasks /change /tn defrag /disable
Disable Scheduled Task/Job
Disable Scheduled Task/Job
禁用计划的任务/作业

启用计划任务/作业(Enable Scheduled Task/Job)

We can enable scheduled job similar to disable operation where we use /enable .

我们可以启用计划的作业,类似于在使用/enable禁用操作。

$ schtasks /change /tn defrag /enable
Enable Scheduled Task/Job
Enable Scheduled Task/Job
启用计划任务/作业

修改计划任务/作业(Modify Scheduled Task/Job)

A scheduled task can be changed with /change option. We can specify the attribute we want to change. In this example we will change the username and password of the user which will run task with /RU and /RP .

可以使用/change选项更改预定任务。 我们可以指定要更改的属性。 在此示例中,我们将更改将使用/RU/RP运行任务的用户的用户名和密码。

$ schtasks /change /tn defrag /ru jack /rp 123456
Modify Scheduled Task/Job
Modify Scheduled Task/Job
修改计划任务/作业

翻译自: https://www.poftut.com/schedule-tasks-command-line-windows-schtasks/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值