使用 schtasks.exe 指定计划任务的运行目录

发布于 2024-07-12 02:00:22 字数 351 浏览 11 评论 0原文

我有一个由计划任务调用的应用程序。 它从 Windows Server 2003 迁移到 Windows Server 2008。在 2003 年,该应用程序在可执行文件所在的目录中运行。 2008 年,Environment.CurrentDirectory (C#) 报告它在 C:\Windows\System32 中运行。 如何设置运行目录? 我使用 schtasks.exe 进行命令行部署。

UPD:通过界面,它似乎是操作编辑屏幕上的“开始于(可选)”字段。

UPD:看起来使用 XML 文件可能会有所帮助,但我希望不使用它。

I have an application which gets called by a scheduled task. It moved from Windows Server 2003 to Windows Server 2008. On 2003, the app ran in the directory where the executable was located. On 2008 Environment.CurrentDirectory (C#) reports that it's running in C:\Windows\System32. How do I set the running directory? I'm using schtasks.exe for command-line deployment.

UPD: Through the interface, it seems to be the "Start in (optional)" field on the action edit screen.

UPD: Looks like using the XML file may help, but I'm looking to do without it.

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

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

发布评论

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

评论(7

靖瑶 2024-07-19 02:00:22

只是想添加适用于 Windows Server 2008 和 2012 的详细信息。由于许多人可以更好地理解屏幕截图,因此这里有一个屏幕截图:
在此处输入图像描述

总结一下。 当您为计划任务创建操作时,您可以选择设置“开始于(可选)”字段(在屏幕截图中以红色圆角显示)。 这将是触发流程的目录。

Just wanted to add details that are valid for Windows Server 2008 and 2012. As many people can understand screen shots better here is a screen shot:
enter image description here

To sum it up. When you create the action for your scheduled task you have the option to set the "Start in (optional)" field (rounded in red on the screen shot). This will be the directory from where your process is triggered.

南笙 2024-07-19 02:00:22

我最近遇到了同样的问题。 我解决这个问题的方法是将 /V1 开关添加到 schtasks 命令中。

/V1 创建一个 pre-vista 兼容的计划任务并自动填充“开始目录”。

I recently came across the same issue. The way I resolved it was to add the /V1 switch to the schtasks command.

/V1 creates a pre-vista compatible scheduled task and automatically populates the Start In directory.

岛徒 2024-07-19 02:00:22

您可以使用以下命令设置起始目录。

关键是 /tr 开关中的 \。

SCHTASKS /Create /u username /p pswd /ru "NT AUTHORITY\SYSTEM"
  /rp /sc ONSTART /tn task-name /tr "\"D:\name-of-file-to-run\" "

You can set the start in directory using the following command

The key is the \ in the /tr switch.

SCHTASKS /Create /u username /p pswd /ru "NT AUTHORITY\SYSTEM"
  /rp /sc ONSTART /tn task-name /tr "\"D:\name-of-file-to-run\" "
这样的小城市 2024-07-19 02:00:22

请参阅 我对类似问题的回答,即如何设置“唤醒计算机以运行此任务...”选项,该选项只能从任务计划程序 UI(并通过 XML)获得,而不能从 schtasks.exe /create 命令行。

它的具体细节是:

  1. 通过 schtasks.exe /create /tn MyTask ... 创建任务
  2. 通过 schtasks.exe /query /xml /tn MyTask > 将任务导出为 XML ; MyTask.xml
  3. 通过 XSLT 或搜索/替换更新此 XML
  4. 通过 schtasks.exe /create /tn MyTask /xml MyTask.xml /f 重新导入(覆盖旧任务)

< a href="https://stackoverflow.com/questions/497945/using-schtasks-from-the-command-line-what-parameter-will-wake-the-computer-from/506362#506362">详细信息在这里。

Please see my answer to a similar question, of how to set the "Wake the computer to run this task..." option that is only available from the Task Scheduler UI (and via the XML), and not the schtasks.exe /create command line .

The nuts and bolts of it are:

  1. Create your task via schtasks.exe /create /tn MyTask ...
  2. Export your task to XML via schtasks.exe /query /xml /tn MyTask > MyTask.xml
  3. Update this XML via XSLT or a search/replace
  4. Re-import (overwriting the old task) via schtasks.exe /create /tn MyTask /xml MyTask.xml /f

Details are here.

兰花执着 2024-07-19 02:00:22

我希望人们能够看到 XML 方法的这个答案(坦率地说,我认为这是一种更简洁的方法,并且还有一些更好的文档介绍了您可以设置哪些参数来配置任务中的特定功能)。

第 1 步:创建设置所有任务设置的 XML 文件,可在多个位置获取有关 XML 元素的详细信息:

步骤 2:特定于任务将从“何处”执行(如在起始目录中,脚本将在命令行中开始,这与OP的问题直接相关......您需要配置像这样的参数...

<?xml version="1.0" encoding="UTF-16"?> 
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> 
  <RegistrationInfo> 
    <Date>2012-08-19T16:49:14.6182</Date> 
    <Author>YOUR-COMPUTER-DOMAIN\YOUR-USERNAME</Author> 
  </RegistrationInfo>

    ... a bunch of other stuff in between here ....

  <Actions Context="Author"> 
    <Exec> 
      <Command>C:\PythonEXE\mini_program_test.exe</Command> 
      <Arguments></Arguments> 
      <WorkingDirectory>C:\Some\where\here\</WorkingDirectory> 
    </Exec>
  </Actions> 
</Task> 

请注意上面WorkingDirectory参数中没有任何引号 - 我之前犯了这个错误。

第 3 步:由于您将使用 schtasks.exe 来通过 XML 创建此新任务,因此请在此处查看更多信息:https://msdn.microsoft.com/en-us/library/bb736357.aspx

第 4 步:在在 Windows 命令行中,您将执行类似这样的操作(一旦您的 XML 准备就绪)

C:\>schtasks /CREATE /TN "TASK-NAME-HERE" /RU "YOUR-USERNAME" /RP "YOUR-PASSWORD" /XML C:\YOUR-XML-FILE-LOCATION\ready.xml

希望这对您有所帮助 - 玩得开心!

I hope people will see this answer for the XML approach (frankly I think that it's a cleaner method and there's some better documentation around what parameters you can set to configure specific features within the task too).

Step 1: create XML file that sets all task settings, several places for more info on XML elements:

Step 2: Specific to "where" the task will execute from (as in the starting directory the script will commence in the command line, this is directly related to the OP's question....You'll need to configure the parameter like so...

<?xml version="1.0" encoding="UTF-16"?> 
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> 
  <RegistrationInfo> 
    <Date>2012-08-19T16:49:14.6182</Date> 
    <Author>YOUR-COMPUTER-DOMAIN\YOUR-USERNAME</Author> 
  </RegistrationInfo>

    ... a bunch of other stuff in between here ....

  <Actions Context="Author"> 
    <Exec> 
      <Command>C:\PythonEXE\mini_program_test.exe</Command> 
      <Arguments></Arguments> 
      <WorkingDirectory>C:\Some\where\here\</WorkingDirectory> 
    </Exec>
  </Actions> 
</Task> 

Please note above that there aren't any quotation marks in the WorkingDirectory paramater -- I made that mistake earlier.

Step 3: Since you'll be using schtasks.exe in order to CREATE this new task via the XML, take a look here for more info: https://msdn.microsoft.com/en-us/library/bb736357.aspx

Step 4: In the windows command line, you'll execute something like this (once your XML is ready)

C:\>schtasks /CREATE /TN "TASK-NAME-HERE" /RU "YOUR-USERNAME" /RP "YOUR-PASSWORD" /XML C:\YOUR-XML-FILE-LOCATION\ready.xml

Hope this helps out a bit -- have fun!

假装爱人 2024-07-19 02:00:22

尝试 cd /d "" & schtasks

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

Try cd /d "<WorkingDirectory>" & schtasks <SchtasksParams>

Change working directory and then run schtasks.

神魇的王 2024-07-19 02:00:22

在应用程序中使用 My.Application.Info.DirectoryPath 无需 XML 设置。

Use My.Application.Info.DirectoryPath in App without need a XML Setting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文