5

Sample run.bat file

echo "Test"
echo %1
echo %2
set /p DUMMY=Hit ENTER to continue...

So this bat file will print the first two arguments that are given to them. The arguments may or may not contain spaces. For that I have escaped them with Double quotes.

run.bat -test "arg2 d"

It worked perfectly fine,when called via command line.

I wanted to schedule that bat via Task Scheduler. But the task scheduler opens a cmd window and closes immediately.

Not Working

enter image description here

Working

Task Image

Note the quote in arguments. That is causing the issue. So how can I escape the argument with spaces.

Also if the Program/Script file location is entered without quotes then the arguments is working. but for that the script has to be in a folder without spaces.

Error in Action

enter image description here

So how to create a task with space in file path and arguments with spaces as well.

Its happening only in Windows 10. It's working fine in windows 7 btw.

Madhan
  • 5,750
  • 4
  • 28
  • 61
  • 3
    I attached a debugger to the svchost.exe process of the task scheduler service, with a breakpoint set on `CreateProcessAsUserW`. It turns out that it rewrites the command line as `"C:\Windows\SYSTEM32\cmd.exe /c """PATH\TO\BAT"" -test "arg2 d"""`. This is not only wrong (note the double set of quotes in `""PATH\TO\BAT""`) but weird since `CreateProcess` already knows how to run a .bat or .cmd script directly via `%ComSpec% /c`. – Eryk Sun Aug 10 '17 at 12:47
  • 4
    As a workaround, set the program to run as `cmd.exe`, and set the arguments as `/c ""D:\Test Space\run.bat" -test "arg2 d""`. – Eryk Sun Aug 10 '17 at 12:50
  • @eryksun Thanks. – Madhan Aug 11 '17 at 08:02
  • wow, is there any other work around to this? – majidarif Aug 18 '18 at 03:37
  • Similar issue. I had my windows 2008 scheduler running a .vbs file. After I exported/imported the task to a Windows 2016 server, I had to remove the quotes. Otherwise, it would just get stuck at "running" status. Once I removed the quotes, it worked fine. – NL3294 Aug 27 '19 at 19:31
  • I would do as Eryk Sun recommended - the actual executable you are running is cmd.exe. Cut out the middleman and run it directly. – Bill_Stewart Jun 23 '21 at 17:53

2 Answers2

1

I had a similar issue. I was running a scheduled task on Server 2012 R2. I had to change the dropdown of the task to Windows 2012 rather than 2008 and it seemed to work. Maybe Win10 needs 2012 selected in the task as well.

1

Short answer: Take the whole command line as you would type in command prompt, and put quotes around it. Don't escape any internal quotes, just put an extra quote on the beginning and the end of the command line.

Then set the scheduled program to cmd.exe and the arguments to /c (quoted command line)

Long answer: According to cmd.exe's documentation (found in 'cmd /?')

If /C or /K is specified, then the remainder of the command line after the switch is processed as a command line, where the following logic is used to process quote (") characters:

  1. If all of the following conditions are met, then quote characters on the command line are preserved:
  • no /S switch
  • exactly two quote characters
  • no special characters between the two quote characters, where special is one of: &<>()@^|
  • there are one or more whitespace characters between the two quote characters
  • the string between the two quote characters is the name of an executable file.
  1. Otherwise, old behavior is to see if the first character is a quote character and if so, strip the leading character and remove the last quote character on the command line, preserving any text after the last quote character.
Leandro
  • 71
  • 2
  • 3