Powershell and schtask with task that has a space

南楼画角 提交于 2019-12-05 05:20:48
Jon

I was having problems with this as well.. anyone else with this issue the answer is to include single quotes

/TR "'C:\Program Files (x86)\etc\test.exe'"

I had the same problem on Windows Server 2008 R2. While Microsoft support indicates you should be able to surround the command with \" I never got that to work, but single quotes work. Moreover, arguments with spaces can also be surrounded in single quotes:

schtasks /create /f /tn "Test" /tr "'c:\program files\test.bat' arg1 'arg 2 with spaces' arg3" /sc Daily /st 00:00

or when building up a string to execute (and using your $folder variable):

$createTask = "schtasks /create /f /tn `"Demon Purge Job`" /tr `"'$folder\Demon.DatabasePurge.exe' arg1 'arg 2 with spaces' arg3`"" /sc daily /st 00:00
Invoke-Expression $createTask

The first example produces the following task action screen:

Valdes

To work around this problem, enclose the path portion of the task (not including arguments or switches) between backslash \ and quotation marks " character combinations, for example \". Enclose the complete path of the task (including arguments or switches) between quotation marks as usual when you create a path or command that contains spaces.

Code:

schtasks /create /f /tn "Test" /tr "'c:\program files\test.bat' arg1 'arg 2 with spaces' arg3" /sc Daily /st 00:00

Replace:

schtasks /create /f /tn "Test" /tr "\"c:\program files\test.bat\" arg1 'arg 2 with spaces' arg3" /sc Daily /st 00:00
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!