2

I'm trying to create a scheduled task through schtasks comand and I'm struggling on how to escape double quotes (and other possible special characters) for the password on /RP parameter.

SCHTASKS /create /tn "Task name" /tr "powershell D:\path\to\powershell\script\powershellScript.ps1" /ru userName /rp pass"word /rl HIGHEST /f /sc MONTHLY /st 02:00

Some examples of what I've tried so far (after a lot of search), nothing working (and all combinations I could think of):

  • Enclosing the password with double quotes: /rp "pass"word"
  • Enclosing the password with single quotes: /rp 'pass"word'
  • Escaping special characters with ^: /rp "pass^"word"
  • Escaping special characters with ": /rp "pass""word"
  • Escaping special characters with : /rp "pass\"word"

Every time, the error message I got is always the same (or very similar), suggesting that the double quotes causes the interpretation of the command to break:

Invalid syntax. Mandatory option 'sc' is missing. Type "CREATE /?" for usage.

Is it even possible?

Wavish
  • 31
  • 6

2 Answers2

0

As the task creation is executed from a PowerShell script, in this particular case, I ended up by searching on the password text and analyzing whether there are double quotes present.

If that's the case, then schtasks will prompt to re-enter the password, and that way it doesn't crash.

if($password.contains('"')){ #if the password contains double quotes, prompt them, to avoid the task creation to crash
    SCHTASKS /create /tn "Task name" /tr "powershell D:\path\to\powershell\script\powershellScript.ps1" /ru userName /rp * /rl HIGHEST /f /sc MONTHLY /st 02:00
}else{
    SCHTASKS /create /tn "Task name" /tr "powershell D:\path\to\powershell\script\powershellScript.ps1" /ru userName /rp "$password" /rl HIGHEST /f /sc MONTHLY /st 02:00
}
Wavish
  • 31
  • 6
0

The problem is that you are not escaping your backslashes that are part of the command. I encountered the same problem when trying to schedule syncthing to start and also passing parameters telling it where to look; the troublesome parameters are -generate="" and -home="C:\SyncThing\config". For me, the command I needed was:

schtasks /create /sc onstart /TN SyncThing /TR "C:\\SyncThing\\syncthing.exe -home=\"C:\\SyncThing\\config\" -generate=\"\""

So, put the command along with its parameters inside double quotes, and within that, precede double quotes and backslashes with a backslash. In your case, that would render:

SCHTASKS /create /tn "Task name" /tr "powershell D:\\path\\to\\powershell\\script\\powershellScript.ps1" /ru userName /rp pass\"word /rl HIGHEST /f /sc MONTHLY /st 02:00
TamaMcGlinn
  • 2,840
  • 23
  • 34