【问题标题】:How do I stop/start a scheduled task on a remote computer programmatically?如何以编程方式停止/启动远程计算机上的计划任务?
【发布时间】:2010-09-22 09:04:19
【问题描述】:

我想编写一个脚本来停止远程计算机上的计划任务,做一些事情,然后重新启动计划任务。

我该怎么做?

【问题讨论】:

    标签: windows scheduled-tasks


    【解决方案1】:

    注意: "schtasks"(参见另一个已接受的回复)已替换 "at"。但是,如果情况需要与没有 schtask 的旧版本 Windows 兼容,则“at”可能有用。

    “at”的命令行帮助:

    C:\>在 /? AT 命令调度命令和程序在计算机上运行 指定的时间和日期。计划服务必须正在运行才能使用 AT 命令。 AT [\\计算机名] [[id] [/DELETE] | /删除[/是]] 在 [\\computername] 时间 [/INTERACTIVE] [ /EVERY:日期[,...] | /NEXT:date[,...]] "命令" \\computername 指定远程计算机。命令安排在 如果省略此参数,则为本地计算机。 id 是分配给计划的标识号 命令。 /delete 取消预定的命令。如果省略 id,则所有 计算机上的预定命令被取消。 /yes 与取消所有作业命令一起使用 需要确认。 time 指定命令运行的时间。 /interactive 允许作业与用户的桌面交互 作业运行时登录的人员。 /every:date[,...] 在一周中的每个指定日期运行命令或 月。如果省略日期,则为月份的当前日期 假设。 /next:date[,...] 在下次出现 天(例如,下周四)。如果省略日期,则 假定为当前日期。 “command” 是要运行的 Windows NT 命令或批处理程序。

    【讨论】:

    • at 命令创建一个计划任务,但我不相信它可以启动或停止一个现有任务。
    • 嗯。 “At”确实具有“/delete”的能力,它“取消预定的命令”,但我希望处理任何已经运行的进程都属于“做一些事情”类别——因为知道如何停止进程完全取决于被停止的进程。
    【解决方案2】:

    这是我找到的。

    停止:

    schtasks /end /s <machine name> /tn <task name>
    

    开始:

    schtasks /run /s <machine name> /tn <task name>
    
    
    C:\>schtasks /?
    
    SCHTASKS /parameter [arguments]
    
    Description:
        Enables an administrator to create, delete, query, change, run and
        end scheduled tasks on a local or remote system. Replaces AT.exe.
    
    Parameter List:
        /Create         Creates a new scheduled task.
    
        /Delete         Deletes the scheduled task(s).
    
        /Query          Displays all scheduled tasks.
    
        /Change         Changes the properties of scheduled task.
    
        /Run            Runs the scheduled task immediately.
    
        /End            Stops the currently running scheduled task.
    
        /?              Displays this help message.
    
    Examples:
        SCHTASKS
        SCHTASKS /?
        SCHTASKS /Run /?
        SCHTASKS /End /?
        SCHTASKS /Create /?
        SCHTASKS /Delete /?
        SCHTASKS /Query  /?
        SCHTASKS /Change /?
    

    【讨论】:

    • 机器名可选:schtasks /end /tn &lt;task name&gt;
    • 这就是我要找的。有没有办法从运行的进程中获取退出代码?
    【解决方案3】:

    /disable/enable 切换 /change 命令怎么样?

    schtasks.exe /change /s <machine name> /tn <task name> /disable
    schtasks.exe /change /s <machine name> /tn <task name> /enable
    

    【讨论】:

      【解决方案4】:
      schtasks /change /disable /tn "Name Of Task" /s REMOTEMACHINENAME /u mydomain\administrator /p adminpassword 
      

      【讨论】:

      • 这没有提供答案。
      【解决方案5】:

      试试这个:

      schtasks /change /ENABLE /tn "Auto Restart" /s mycomutername /u mycomputername\username/p mypassowrd
      

      【讨论】:

        猜你喜欢
        • 2013-10-15
        • 2010-10-02
        • 2019-03-29
        • 1970-01-01
        • 1970-01-01
        • 2012-01-23
        • 2023-03-25
        • 2011-07-22
        • 1970-01-01
        相关资源
        最近更新 更多