windows 使用 schtasks.exe 指定计划任务的运行目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/447774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Specifying the running directory for Scheduled Tasks using schtasks.exe
提问by Eugene Katz
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.
我有一个由计划任务调用的应用程序。它从 Windows Server 2003 迁移到 Windows Server 2008。在 2003 年,该应用程序在可执行文件所在的目录中运行。On 2008 Environment.CurrentDirectory
(C#) 报告说它在 C:\Windows\System32 中运行。如何设置运行目录?我正在使用 schtasks.exe 进行命令行部署。
UPD: Through the interface, it seems to be the "Start in (optional)" field on the action edit screen.
UPD:通过界面,似乎是动作编辑屏幕上的“开始于(可选)”字段。
UPD: Looks like using the XML file may help, but I'm looking to do without it.
UPD:看起来使用 XML 文件可能会有所帮助,但我希望不用它。
采纳答案by matt.franklin
I recently came across the same issue. The way I resolved it was to add the /V1 switch to the schtasks command.
我最近遇到了同样的问题。我解决它的方法是将 /V1 开关添加到 schtasks 命令。
/V1 creates a pre-vista compatible scheduled task and automatically populates the Start In directory.
/V1 创建一个 pre-vista 兼容的计划任务并自动填充 Start In 目录。
回答by Angel Naydenov
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:
只是想添加对 Windows Server 2008 和 2012 有效的详细信息。由于许多人可以更好地理解屏幕截图,这里是一个屏幕截图:
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.
把它们加起来。当您为计划任务创建操作时,您可以选择设置“开始于(可选)”字段(在屏幕截图中以红色四舍五入)。这将是您的流程被触发的目录。
Hope this helps!
希望这可以帮助!
回答by matt.franklin
You can set the start in directory using the following command
您可以使用以下命令在目录中设置启动
The key is the \ in the /tr switch.
关键是 /tr 开关中的 \。
SCHTASKS /Create /u username /p pswd /ru "NT AUTHORITY\SYSTEM"
/rp /sc ONSTART /tn task-name /tr "\"D:\name-of-file-to-run\" "
回答by NicJ
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 .
请参阅我对类似问题的回答,了解如何设置“唤醒计算机以运行此任务...”选项,该选项仅可从任务计划程序 UI(并通过 XML)获得,而不能schtasks.exe /create
从命令行获得。
The nuts and bolts of it are:
它的螺母和螺栓是:
- Create your task via
schtasks.exe /create /tn MyTask ...
- Export your task to XML via
schtasks.exe /query /xml /tn MyTask > MyTask.xml
- Update this XML via XSLT or a search/replace
- Re-import (overwriting the old task) via
schtasks.exe /create /tn MyTask /xml MyTask.xml /f
- 通过创建您的任务
schtasks.exe /create /tn MyTask ...
- 通过以下方式将您的任务导出到 XML
schtasks.exe /query /xml /tn MyTask > MyTask.xml
- 通过 XSLT 或搜索/替换更新此 XML
- 通过重新导入(覆盖旧任务)
schtasks.exe /create /tn MyTask /xml MyTask.xml /f
回答by Yuh-Rong Leu
Try cd /d "<WorkingDirectory>" & schtasks <SchtasksParams>
尝试 cd /d "<WorkingDirectory>" & schtasks <SchtasksParams>
Change working directory and then run schtasks.
更改工作目录,然后运行schtasks。
回答by Doug E Fresh
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).
我希望人们会看到 XML 方法的这个答案(坦率地说,我认为这是一种更清晰的方法,并且有一些关于您可以设置哪些参数来配置任务中的特定功能的更好的文档)。
Step 1: create XML file that sets all task settings, several places for more info on XML elements:
第 1 步:创建设置所有任务设置的 XML 文件,有几个地方可以提供有关 XML 元素的更多信息:
- https://msdn.microsoft.com/en-us/library/windows/desktop/aa383609(v=vs.85).aspx
- https://msdn.microsoft.com/en-us/library/windows/desktop/aa446863(v=vs.85).aspx
- https://msdn.microsoft.com/en-us/library/windows/desktop/aa383611(v=vs.85).aspx
- PowerShell script doesn't work correctly from Windows Task Scheduler
- Task Scheduler from command line
- https://msdn.microsoft.com/en-us/library/windows/desktop/aa383609(v=vs.85).aspx
- https://msdn.microsoft.com/en-us/library/windows/desktop/aa446863(v=vs.85).aspx
- https://msdn.microsoft.com/en-us/library/windows/desktop/aa383611(v=vs.85).aspx
- Windows 任务计划程序中的 PowerShell 脚本无法正常工作
- 来自命令行的任务计划程序
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...
第 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>
Please note abovethat there aren't any quotation marks in the WorkingDirectoryparamater -- I made that mistake earlier.
请注意上面的WorkingDirectory参数中没有任何引号——我之前犯了这个错误。
Step 3: Since you'll be using schtasks.exein 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
第 3 步:由于您将使用 schtasks.exe通过 XML 创建此新任务,请查看此处了解更多信息: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)
第 4 步:在 windows 命令行中,您将执行如下操作(一旦您的 XML 准备就绪)
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!
希望这会有所帮助 - 玩得开心!
回答by Owen
Use My.Application.Info.DirectoryPath in App without need a XML Setting.
在应用程序中使用 My.Application.Info.DirectoryPath 无需 XML 设置。