Specifying the running directory for Scheduled Tasks using schtasks.exe

后端 未结 7 1197
野的像风
野的像风 2020-12-01 07:27

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 execut

相关标签:
7条回答
  • 2020-12-01 07:54

    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:

    • 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

    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!

    0 讨论(0)
  • 2020-12-01 08:00

    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.

    0 讨论(0)
  • 2020-12-01 08:05

    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.

    0 讨论(0)
  • 2020-12-01 08:08

    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\" "
    
    0 讨论(0)
  • 2020-12-01 08:08

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

    Change working directory and then run schtasks.

    0 讨论(0)
  • 2020-12-01 08:10

    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.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题