Task Scheduler arguments with quotes not working

Viewed 19840

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.

2 Answers

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.

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.
Related