使用 schtasks.exe 指定计划任务的运行目录
我有一个由计划任务调用的应用程序。 它从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
只是想添加适用于 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:
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.
我最近遇到了同样的问题。 我解决这个问题的方法是将 /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.
您可以使用以下命令设置起始目录。
关键是 /tr 开关中的 \。
You can set the start in directory using the following command
The key is the \ in the /tr switch.
请参阅 我对类似问题的回答,即如何设置“唤醒计算机以运行此任务...”选项,该选项只能从任务计划程序 UI(并通过 XML)获得,而不能从
schtasks.exe /create
命令行。它的具体细节是:
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:
schtasks.exe /create /tn MyTask ...
schtasks.exe /query /xml /tn MyTask > MyTask.xml
schtasks.exe /create /tn MyTask /xml MyTask.xml /f
Details are here.
我希望人们能够看到 XML 方法的这个答案(坦率地说,我认为这是一种更简洁的方法,并且还有一些更好的文档介绍了您可以设置哪些参数来配置任务中的特定功能)。
第 1 步:创建设置所有任务设置的 XML 文件,可在多个位置获取有关 XML 元素的详细信息:
步骤 2:特定于任务将从“何处”执行(如在起始目录中,脚本将在命令行中开始,这与OP的问题直接相关......您需要配置像这样的参数...
请注意上面,WorkingDirectory参数中没有任何引号 - 我之前犯了这个错误。
第 3 步:由于您将使用 schtasks.exe 来通过 XML 创建此新任务,因此请在此处查看更多信息:https://msdn.microsoft.com/en-us/library/bb736357.aspx
第 4 步:在在 Windows 命令行中,您将执行类似这样的操作(一旦您的 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...
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)
Hope this helps out a bit -- have fun!
尝试 cd /d "" & schtasks
更改工作目录,然后运行 schtasks。
Try
cd /d "<WorkingDirectory>" & schtasks <SchtasksParams>
Change working directory and then run schtasks.
在应用程序中使用 My.Application.Info.DirectoryPath 无需 XML 设置。
Use My.Application.Info.DirectoryPath in App without need a XML Setting.