指定“开始” Windows 中 schtasks 命令中的目录

发布于 2024-07-25 05:15:52 字数 508 浏览 6 评论 0原文

我意识到这个问题在以下线程中得到了“回答”: Specifying the running directory for Scheduled Tasks using schtasks.exe

但是,我我仍然无法理解答案并准确地了解我的情况的结果会是什么样子。

我的 schtasks 命令如下所示:

Schtasks /Create /TR "C:\Program Files\Java\jre6\bin\javaw.exe main.MoveFile input.txt" /SC WEEKLY /TN mytask

我想要指定在“C:\My Library”目录中开始。 在 tr 部分之前放置 "\" 会填充启动目录“C:\Program Files\Java\jre6\bin”。

我已经搞砸了很多次,但我似乎无法让它发挥作用。

I realize that this question is "answered" at the following thread: Specifying the running directory for Scheduled Tasks using schtasks.exe

However, I'm still having trouble understanding the answer and seeing exactly what the result would look like for my situation.

My schtasks command looks like this:

Schtasks /Create /TR "C:\Program Files\Java\jre6\bin\javaw.exe main.MoveFile input.txt" /SC WEEKLY /TN mytask

I want to specify the start in directory of "C:\My Library". Putting a "\" before the tr section fills in a start-in directory of "C:\Program Files\Java\jre6\bin".

I've messed around with it a lot, but I just can't seem to make it work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

不羁少年 2024-08-01 05:15:52

如果所有其他方法都失败,您可以重定向到一个批处理文件,该文件设置其自己的 CD,然后调用您的程序。
例如:

Schtasks /Create /TR "C:\example\batch.bat" /SC WEEKLY /TN mytask

作为 schtask,以及

cd "%temp%\"
"C:\Program Files\Java\jre6\bin\javaw.exe main.MoveFile input.txt"

“C:\example\batch.bat”。 这应该将当前目录保留为您在批处理文件中将其更改为的目录,并保留与其相关的所有引用。

If all else fails, you can redirect to a batch file that sets it's own CD, then calls your program.
for example:

Schtasks /Create /TR "C:\example\batch.bat" /SC WEEKLY /TN mytask

As the schtask, and

cd "%temp%\"
"C:\Program Files\Java\jre6\bin\javaw.exe main.MoveFile input.txt"

as "C:\example\batch.bat". That should keep the current directory as whatever you change it to in the batch file and keep all references relative to that.

蓝戈者 2024-08-01 05:15:52

更新:请注意,从 Powershell v3 开始(但仅在 Windows 2012 及更高版本下!),我发现新的 API 更有吸引力:

$taskPath = "\MyTasksFolder\"
$name = 'MyTask'
$runAt = '5:00 AM'
$exe = 'my.exe'
$params = 'command line arguments'
$location = "C:\Path\To\MyTask"

Unregister-ScheduledTask -TaskName $name -TaskPath $taskPath -Confirm:$false -ErrorAction:SilentlyContinue  

$action = New-ScheduledTaskAction –Execute "$location\$exe" -Argument "$params" -WorkingDirectory $location
$trigger = New-ScheduledTaskTrigger -Daily -At $runAt
Register-ScheduledTask –TaskName $name -TaskPath $taskPath -Action $action –Trigger $trigger –User 'someuser' -Password 'somepassword' | Out-Null

Amal 的带有 /v1 开关的解决方案很棒但不允许在自定义文件夹中创建任务(即您无法创建“MyCompany\MyTask”并且所有内容最终都在根文件夹中),因此我最终得到了如下所述的 PowerShell 脚本。

用法:

CreateScheduledTask -computer:"hostname-or-ip" `
                    -taskName:"MyFolder\MyTask" `
                    -command:"foo.exe" `
                    -arguments:"/some:args /here" `
                    -workingFolder:"C:\path\to\the\folder" `
                    -startTime:"21:00" `
                    -enable:"false" `
                    -runAs:"DOMAIN\user" `
                    -runAsPassword:"p@$w0rd"

(注意,enable 必须小写 - 对于布尔值,您需要 $value.ToString().ToLower()

<强>实现:

该函数使用XML任务定义和“Schedule.Service”COM对象。

#####################################################
#
#  Creates a Windows scheduled task triggered DAILY.
#  Assumes TODAY start date, puts "run-as" user as task author.
#
#####################################################
function CreateScheduledTask($computer, $taskName, $command, $arguments, $workingFolder, $startTime, $enable, $runAs, $runAsPassword)
{    
    $xmlTemplate = "<?xml version='1.0' encoding='UTF-16'?>
        <Task version='1.2' xmlns='http://schemas.microsoft.com/windows/2004/02/mit/task'>
          <RegistrationInfo>
            <Date>{0}</Date>
            <Author>{1}</Author>
          </RegistrationInfo>
          <Triggers>
            <CalendarTrigger>
              <StartBoundary>{2}</StartBoundary>
              <Enabled>true</Enabled>
              <ScheduleByDay>
                <DaysInterval>1</DaysInterval>
              </ScheduleByDay>
            </CalendarTrigger>
          </Triggers>
          <Principals>
            <Principal id='Author'>
              <UserId>{1}</UserId>
              <LogonType>Password</LogonType>
              <RunLevel>LeastPrivilege</RunLevel>
            </Principal>
          </Principals>
          <Settings>
            <IdleSettings>
              <Duration>PT10M</Duration>
              <WaitTimeout>PT1H</WaitTimeout>
              <StopOnIdleEnd>true</StopOnIdleEnd>
              <RestartOnIdle>false</RestartOnIdle>
            </IdleSettings>
            <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
            <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
            <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
            <AllowHardTerminate>true</AllowHardTerminate>
            <StartWhenAvailable>false</StartWhenAvailable>
            <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
            <AllowStartOnDemand>true</AllowStartOnDemand>
            <Enabled>{3}</Enabled>
            <Hidden>false</Hidden>
            <RunOnlyIfIdle>false</RunOnlyIfIdle>
            <WakeToRun>false</WakeToRun>
            <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
            <Priority>7</Priority>
          </Settings>
          <Actions Context='Author'>
            <Exec>
              <Command>{4}</Command>
              <Arguments>{5}</Arguments>
              <WorkingDirectory>{6}</WorkingDirectory>
            </Exec>
          </Actions>
        </Task>"
    $registrationDateTime = [DateTime]::Now.ToString("yyyy-MM-dd") + "T" + [DateTime]::Now.ToString("HH:mm:ss")
    $startDateTime = [DateTime]::Now.ToString("yyyy-MM-dd") + "T" + $startTime + ":00"
    $xml = $xmlTemplate -f $registrationDateTime, $runAs, $startDateTime, $enable, $command, $arguments, $workingFolder

    $sch = new-object -ComObject("Schedule.Service")
    $sch.Connect($computer)
    $task = $sch.NewTask($null)
    $task.XmlText = $xml

    $createOrUpdateFlag = 6
    $sch.GetFolder("\").RegisterTaskDefinition($taskName, $task, $createOrUpdateFlag, $runAs, $runAsPassword, $null, $null) | out-null 
}

UPDATE: Note that starting from Powershell v3 (but only under Windows 2012 and higher!) there's new API which I find much more attractive:

$taskPath = "\MyTasksFolder\"
$name = 'MyTask'
$runAt = '5:00 AM'
$exe = 'my.exe'
$params = 'command line arguments'
$location = "C:\Path\To\MyTask"

Unregister-ScheduledTask -TaskName $name -TaskPath $taskPath -Confirm:$false -ErrorAction:SilentlyContinue  

$action = New-ScheduledTaskAction –Execute "$location\$exe" -Argument "$params" -WorkingDirectory $location
$trigger = New-ScheduledTaskTrigger -Daily -At $runAt
Register-ScheduledTask –TaskName $name -TaskPath $taskPath -Action $action –Trigger $trigger –User 'someuser' -Password 'somepassword' | Out-Null

Amal's solution with /v1 switch is great but doesn't allow to create tasks in custom folders (ie you can't create "MyCompany\MyTask" and everything ends up in the root folder), so I finally ended up with a PowerShell script described below.

Usage:

CreateScheduledTask -computer:"hostname-or-ip" `
                    -taskName:"MyFolder\MyTask" `
                    -command:"foo.exe" `
                    -arguments:"/some:args /here" `
                    -workingFolder:"C:\path\to\the\folder" `
                    -startTime:"21:00" `
                    -enable:"false" `
                    -runAs:"DOMAIN\user" `
                    -runAsPassword:"p@$w0rd"

(Note, enable must be lowercase - for a boolean you'd need $value.ToString().ToLower())

Implementation:

The function uses XML task definition and "Schedule.Service" COM object.

#####################################################
#
#  Creates a Windows scheduled task triggered DAILY.
#  Assumes TODAY start date, puts "run-as" user as task author.
#
#####################################################
function CreateScheduledTask($computer, $taskName, $command, $arguments, $workingFolder, $startTime, $enable, $runAs, $runAsPassword)
{    
    $xmlTemplate = "<?xml version='1.0' encoding='UTF-16'?>
        <Task version='1.2' xmlns='http://schemas.microsoft.com/windows/2004/02/mit/task'>
          <RegistrationInfo>
            <Date>{0}</Date>
            <Author>{1}</Author>
          </RegistrationInfo>
          <Triggers>
            <CalendarTrigger>
              <StartBoundary>{2}</StartBoundary>
              <Enabled>true</Enabled>
              <ScheduleByDay>
                <DaysInterval>1</DaysInterval>
              </ScheduleByDay>
            </CalendarTrigger>
          </Triggers>
          <Principals>
            <Principal id='Author'>
              <UserId>{1}</UserId>
              <LogonType>Password</LogonType>
              <RunLevel>LeastPrivilege</RunLevel>
            </Principal>
          </Principals>
          <Settings>
            <IdleSettings>
              <Duration>PT10M</Duration>
              <WaitTimeout>PT1H</WaitTimeout>
              <StopOnIdleEnd>true</StopOnIdleEnd>
              <RestartOnIdle>false</RestartOnIdle>
            </IdleSettings>
            <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
            <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
            <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
            <AllowHardTerminate>true</AllowHardTerminate>
            <StartWhenAvailable>false</StartWhenAvailable>
            <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
            <AllowStartOnDemand>true</AllowStartOnDemand>
            <Enabled>{3}</Enabled>
            <Hidden>false</Hidden>
            <RunOnlyIfIdle>false</RunOnlyIfIdle>
            <WakeToRun>false</WakeToRun>
            <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
            <Priority>7</Priority>
          </Settings>
          <Actions Context='Author'>
            <Exec>
              <Command>{4}</Command>
              <Arguments>{5}</Arguments>
              <WorkingDirectory>{6}</WorkingDirectory>
            </Exec>
          </Actions>
        </Task>"
    $registrationDateTime = [DateTime]::Now.ToString("yyyy-MM-dd") + "T" + [DateTime]::Now.ToString("HH:mm:ss")
    $startDateTime = [DateTime]::Now.ToString("yyyy-MM-dd") + "T" + $startTime + ":00"
    $xml = $xmlTemplate -f $registrationDateTime, $runAs, $startDateTime, $enable, $command, $arguments, $workingFolder

    $sch = new-object -ComObject("Schedule.Service")
    $sch.Connect($computer)
    $task = $sch.NewTask($null)
    $task.XmlText = $xml

    $createOrUpdateFlag = 6
    $sch.GetFolder("\").RegisterTaskDefinition($taskName, $task, $createOrUpdateFlag, $runAs, $runAsPassword, $null, $null) | out-null 
}
屋顶上的小猫咪 2024-08-01 05:15:52

不确定您使用的是哪个版本的 Windows,但从阅读其他问题来看,Vista / Server 2008 上的 schtasks 似乎没有提供允许您直接指定“启动”目录的命令选项。 人们提供的解决方法是:

  1. 使用 /v1 标志创建 XP / 2003 兼容任务,在这种情况下,会自动设置“start-in”目录。 不确定它的设置是什么,但我怀疑它可能与您的任务可执行文件位于同一目录,这对您不起作用。
  2. 从 XML 文件创建任务(使用 /XML 选项),该文件允许您指定“起始”目录。 抱歉,我不知道此 XML 文件的语法/结构。
  3. 使用任务计划程序 UI 创建任务。

Not sure what version of Windows you are on, but from reading the other question it looks like schtasks on Vista / Server 2008 does not provide a command option that would allow you to specify a "start-in" directory directly. The workarounds people provided were:

  1. Use the /v1 flag to create a XP / 2003 compatible task, in which case the "start-in" directory is automatically set. Not sure what it is set to but I suspect it may be the same directory as your task executable, which won't work for you.
  2. Create your task from an XML file (using the /XML option) which does allow you to specify a "start-in" directory. Sorry I don't know the syntax / structure for this XML file.
  3. Create your task using the Task Scheduler UI instead.
玩世 2024-08-01 05:15:52

正如您所注意到的,在 /TR 参数中使用额外引号的技巧仅允许您使用与可执行文件所在的目录相同的目录。 如果要指定不同的工作目录,则应使用 /XML 选项并指定列出工作目录的 XML 文件。 该命令将如下所示:

SchTasks /Create /TN "Foo" /XML task.xml

XML 文件将如下所示:

<?xml version="1.0" ?>
<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
    <RegistrationInfo>
        <Date>2006-05-02T13:21:17</Date>
        <Author>AuthorName</Author>
        <Version>1.0.0</Version>
        <Description>Call MoveFile</Description>
    </RegistrationInfo>
    <Triggers>
        <CalendarTrigger>
            <StartBoundary>2011-11-02T00:00:00</StartBoundary>
            <ScheduleByDay>
                <DaysInterval>1</DaysInterval>
            </ScheduleByDay>
        </CalendarTrigger>
    </Triggers>
    <Principals>
        <Principal>
            <UserId>Administrator</UserId>
            <LogonType>InteractiveToken</LogonType>
        </Principal>
    </Principals>
    <Settings>
        <Enabled>true</Enabled>
        <AllowStartOnDemand>true</AllowStartOnDemand>
        <AllowHardTerminate>true</AllowHardTerminate>
    </Settings>
    <Actions>
        <Exec>
            <Command>C:\Program Files\Java\jre6\bin\javaw.exe</Command>
            <Arguments>main.MoveFile input.txt</Arguments>
            <WorkingDirectory>C:\My Library</WorkingDirectory>
        </Exec>
    </Actions>
</Task>

这里有有关 XML 架构的更多信息: http://msdn.microsoft.com/en-us/library/windows/desktop/aa383609(v=VS.85).aspx

As you note, the trick of using the extra quotes in the /TR parameter only allows you to use the same directory as where the executable resides. If you want to specify a different working directory, you should use the /XML option and specify an XML file that lists the working directory. The command would be something like this:

SchTasks /Create /TN "Foo" /XML task.xml

The XML file would look something like this:

<?xml version="1.0" ?>
<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
    <RegistrationInfo>
        <Date>2006-05-02T13:21:17</Date>
        <Author>AuthorName</Author>
        <Version>1.0.0</Version>
        <Description>Call MoveFile</Description>
    </RegistrationInfo>
    <Triggers>
        <CalendarTrigger>
            <StartBoundary>2011-11-02T00:00:00</StartBoundary>
            <ScheduleByDay>
                <DaysInterval>1</DaysInterval>
            </ScheduleByDay>
        </CalendarTrigger>
    </Triggers>
    <Principals>
        <Principal>
            <UserId>Administrator</UserId>
            <LogonType>InteractiveToken</LogonType>
        </Principal>
    </Principals>
    <Settings>
        <Enabled>true</Enabled>
        <AllowStartOnDemand>true</AllowStartOnDemand>
        <AllowHardTerminate>true</AllowHardTerminate>
    </Settings>
    <Actions>
        <Exec>
            <Command>C:\Program Files\Java\jre6\bin\javaw.exe</Command>
            <Arguments>main.MoveFile input.txt</Arguments>
            <WorkingDirectory>C:\My Library</WorkingDirectory>
        </Exec>
    </Actions>
</Task>

There's more information about the XML schema here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa383609(v=VS.85).aspx

好倦 2024-08-01 05:15:52

尝试

cd /d "C:\Program Files\Java\jre6\bin" & schtasks /create /tr "C:\Program Files\Java\jre6\bin\javaw.exe main.MoveFile input.txt" /sc WEEKLY /tn mytask

更改工作目录,然后运行 ​​schtasks。

Try

cd /d "C:\Program Files\Java\jre6\bin" & schtasks /create /tr "C:\Program Files\Java\jre6\bin\javaw.exe main.MoveFile input.txt" /sc WEEKLY /tn mytask

Change working directory and then run schtasks.

差↓一点笑了 2024-08-01 05:15:52

需要

安排一个从特定目录运行的任务。如果我们在执行pwd命令时调用此目录c:\x它将打印c:\x。 从那里开始的每个路径都是相对于该目录的。

另一个例子:运行 java -jar myapp.jar 将执行位于 c:\x\myapp.jar 中的 JAR 文件

问题

是无法使用命令 schtasks /create 设置该特定参数

解决方案

有点步骤(就像步骤太多):

  1. 使用 schtasks 尽可能具体地创建任务/创建命令。 尝试将每个参数设置为所需的值:开始时间、重复次数、持续时间等...

    在这种情况下,/tn 参数是强制性的,因此设置它:\tn mytask

  2. 使用 schtasks /query /tn mytask /xml > 将新创建的任务导出到 XML > mytask.xml

  3. 在您喜欢的编辑器中打开mytasks.xml。 您应该看到类似这样的内容(我隐藏了不有趣的部分):

<?xml version="1.0" encoding="UTF-16"?>

<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">

 <RegistrationInfo>...
 </RegistrationInfo>

 <Triggers>...
 </Triggers>

 <Settings>...
 </Settings>

 <Actions Context="Author">

   <Exec>

     <Command>java</Command>

     <Arguments>-jar c:\x\myapp.jar</Arguments>

   </Exec>

 </Actions>

 <Principals>...
 </Principals>

</Task>
  1. 部分内,在 字段下方引入一个新的字段:
<WorkingDirectory>c:\x\</WorkingDirectory>

并保存文件。此字段在此处的 xml 架构定义中定义 https://learn.microsoft.com/en-us/windows/desktop/taskschd/task-scheduler-schema

  1. 删除之前创建的任务 (使用修改后的 xml 重新创建它):schtasks /delete /tn mytask

  2. 使用 /xml 参数再次创建任务:schtasks \tn mytask \xml mytask.xml

只是为了看看它是否有效

使用 schtasks /run /tn mytask 执行它

The need

To schedule a task that will run from an specific directory. If we call this directory c:\x whenever the pwd command is executed it will print c:\x. Every path from there is relative to that directory.

Another example: running java -jar myapp.jar will execute the JAR file located in c:\x\myapp.jar

The problem

It is impossible to set that specific parameter using the command schtasks /create

The solution

Is a bit steppy (like in too many steps):

  1. Create the task as specific as you can using the schtasks /create command. Try to set every parameter to the desired value: start time, repetition, duration, etc...

    In this case the /tn argument is mandatory, so set it: \tn mytask

  2. Export the newly created task to XML using schtasks /query /tn mytask /xml > mytask.xml

  3. Open mytasks.xml in your favorite editor. You should see something like this (I've hidden the not interesting parts):

<?xml version="1.0" encoding="UTF-16"?>

<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">

 <RegistrationInfo>...
 </RegistrationInfo>

 <Triggers>...
 </Triggers>

 <Settings>...
 </Settings>

 <Actions Context="Author">

   <Exec>

     <Command>java</Command>

     <Arguments>-jar c:\x\myapp.jar</Arguments>

   </Exec>

 </Actions>

 <Principals>...
 </Principals>

</Task>
  1. Inside the <Exec> section, below to the <Argument> field introduce a new field:
<WorkingDirectory>c:\x\</WorkingDirectory>

And save the file. This field is defined in the xml schema definition here https://learn.microsoft.com/en-us/windows/desktop/taskschd/task-scheduler-schema

  1. Delete the previously created task (to recreate it using the modified xml): schtasks /delete /tn mytask

  2. Create the task again using /xml argument: schtasks \tn mytask \xml mytask.xml

Just to see if it works

Execute it using schtasks /run /tn mytask.

闻呓 2024-08-01 05:15:52

我发现,如果您在 SCHTASKS 命令行中使用 8.3 命名约定作为路径和文件名,“起始位置”字段将使用文件路径填充 -

例如“C:\Progra~1\NBVCl~1\nbv_up~ 1.exe”将导致“C:\Progra~1\NBVCl~1”出现在
“开始于”区域

I have found that if you use the 8.3 naming convention in the SCHTASKS command line for the path and file names the "Start In" field is polulated with the file path -

e.g. "C:\Progra~1\NBVCl~1\nbv_up~1.exe" will result in "C:\Progra~1\NBVCl~1" appearing in
the "start In" area

眸中客 2024-08-01 05:15:52

注意:这是我刚刚看到的问题..

注意:您必须有两行:

REM NOTE:You have to create the schedule first
SCHTASKS /S SERVER /CREATE /TN "SERVER_RESTART" /RU "" /TR "D:\WORK\scripts\__server_restart.bat 1" /SC MONTHLY /MO FIRST /D SUN /ST:02:10
REM The next line is run to set the run in folder as well as set the: run as: NT AUTHORITY\SYSTEM
SCHTASKS /S SERVER /CHANGE /TN "SERVER_RESTART" /RU "" /TR "D:\WORK\scripts\__server_restart.bat 1"

我在 Windows 2008 中注意到的一件事是,它比 2003 更好地处理批处理脚本。 我不认为“运行”文件夹那么重要,因为我刚刚从任务计划程序手动在测试机器上运行服务器重新启动,它运行得很好。

对于那些可能遇到转义字符等问题的人来说,考虑以下内容:

SCHTASKS /CHANGE /S SERVER /RU "" /TR "powershell -file "\"D:\WORK\ps\zip_up_files\zip_up_files.ps1"\"" /TN "PowerShell - New Archive"

或者,另一个例子:

SCHTASKS /CREATE /S SERVER /RU "" /TR "powershell -file "\"D:\WORK\ps\page_test.ps1"\"" /TN "PowerShell - Page Test" /SC MINUTE /MO 3 /ST:23:00

注意:额外的引号和额外的反斜杠。

希望这可以帮助!

Note: Here is the issue that I just saw with this..

Note: You have to have two lines:

REM NOTE:You have to create the schedule first
SCHTASKS /S SERVER /CREATE /TN "SERVER_RESTART" /RU "" /TR "D:\WORK\scripts\__server_restart.bat 1" /SC MONTHLY /MO FIRST /D SUN /ST:02:10
REM The next line is run to set the run in folder as well as set the: run as: NT AUTHORITY\SYSTEM
SCHTASKS /S SERVER /CHANGE /TN "SERVER_RESTART" /RU "" /TR "D:\WORK\scripts\__server_restart.bat 1"

One of the things that I have noticed with Windows 2008, is that it handles batch scripts much better than 2003, for example. I don't think the "run in" Folder is as important as I just ran the server restart on a test machine manually from Task Scheduler and it runs just fine..

For the folks that maybe running into issues with escaping characters and such, consider the following:

SCHTASKS /CHANGE /S SERVER /RU "" /TR "powershell -file "\"D:\WORK\ps\zip_up_files\zip_up_files.ps1"\"" /TN "PowerShell - New Archive"

or, another example:

SCHTASKS /CREATE /S SERVER /RU "" /TR "powershell -file "\"D:\WORK\ps\page_test.ps1"\"" /TN "PowerShell - Page Test" /SC MINUTE /MO 3 /ST:23:00

Note: The extra Quoting and the extra backslashes.

Hope this helps!

乙白 2024-08-01 05:15:52

唯一的方法是使用 XML 文件,使用以下命令: schtasks /Create /XML C:\file.xml /TN TaskName /RU 域\用户名 /RP 密码

每天 23:00 运行的示例 XML 文件,C:\task文件夹将设置运行目录:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2016-07-13T07:56:56</Date>
    <Author>Administrator</Author>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2016-07-13T23:00:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>domain\Administrator</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>C:\taskfolder\task.bat</Command>
      <WorkingDirectory>C:\taskfolder</WorkingDirectory>
    </Exec>
  </Actions>
</Task>

The only way would be to use an XML Flle, use the following command: schtasks /Create /XML C:\file.xml /TN TaskName /RU domain\username /RP password

Sample XML File that will run every day at 23:00, the C:\taskfolder will set the run in directory:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2016-07-13T07:56:56</Date>
    <Author>Administrator</Author>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2016-07-13T23:00:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>domain\Administrator</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>C:\taskfolder\task.bat</Command>
      <WorkingDirectory>C:\taskfolder</WorkingDirectory>
    </Exec>
  </Actions>
</Task>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文