Powershell and schtask with a task that has space

I am using the schtask command with powershell. The emerging issue is that when it is C: \ Program Files \, it considers the path to be only C: \ Program, and the rest of the path is an argument. I tried to escape from it using "pre" and "post of the field", but made a difference. How can i do this? I cannot recode the path because it can be changed when the user installs it.

I created this on Windows 7 x64. It creates a task that the script returns. However, when I look at it in Task Schedular, the properties of the task, then the actions and delete the changes. It shows the program as C: \ Program, and then the rest as an argument.

enter image description here

Script:

$folder = Split-Path $MyInvocation.MyCommand.Path -Parent $app = "\Demon.DatabasePurge.exe" $exe = $app.Insert(0, $folder) schtasks /create /tn "Demon Purge Job" /sc daily /st 00:00:00 /tr $exe 

Here is what I tried:

 $folder = Split-Path $MyInvocation.MyCommand.Path -Parent $app = "\Demon.DatabasePurge.exe`"" $exe = $app.Insert(0, $folder) $exe2 = $exe.Insert(0, "`"") schtasks /create /tn "Demon Purge Job" /sc daily /st 00:00:00 /tr $exe2 
+4
source share
2 answers

I also had problems with this. Anyone else with this problem answer should include single quotes

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

I had the same issue on Windows Server 2008 R2. While Microsoft support indicates that you should be able to surround the \ "command, I never got this to work, but single quotes work. In addition, arguments with spaces can also be surrounded by 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 creating a string to execute (and using the $ 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 shows the following task action screen: enter image description here

+4
source

Source: https://habr.com/ru/post/1411872/


All Articles