1

I want to run the schtasks.exe as administrator using PowerShell -Command with Start-Process to create a task. This is the code of my .bat file I've got so far.

 set mydir=%~dp0
 set mydir2=%mydir%prim.exe
 set mys='`"C:\workspace\prim.exe`"'
 Powershell -Command "&{Start-Process -FilePath schtasks -ArgumentList '/Create', '/SC ONLOGON', '/TN CoUpdater', '/IT', '/RL HIGHEST', '/TR', '\"%mydir2%\"' -verb RunAs}"

At first I set up my path to the program I want to run. The problem is that my path name contains some white spaces. After some research I was able to determine that the path is in the task scheduler but without quotes (argument after /TR ):

Task scheduler 1

The next thing I did was adding of quotes to a string and put it as the argument after /TR

set mys='`"C:\workspace\prim.exe`"'

which results in: Task scheduler 2

But now I don't know how I should add the quotes to my pathname. I hope anybody can help me with this problem. Thank you!

Community
  • 1
  • 1
Felix
  • 31
  • 1
  • 3

2 Answers2

0

Using a call operator is unnecessary, here's a way that should work:

SET "PRIM=""%~dp0prim.exe"""
powershell -Command "Start-Process -FilePath schtasks -ArgumentList '/Create','/SC','ONLOGON','/TN','CoUpdater','/IT','/RL','HIGHEST','/TR','%PRIM%' -Verb runas"

Note I embedded the quotes into the environment variable. As long as there are matching sets of quotes, the cmd parser shouldn't complain.

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • Thank you very much for responding that fast. Unfortunately it doesn't work. – Felix Jul 06 '18 at 18:17
  • I get following error: unvalid argument/option with the part of my pathname at wich the first white space occurs – Felix Jul 06 '18 at 18:20
  • @Felix Did you copy/paste it verbatim because it worked when I tested it? – Maximilian Burszley Jul 06 '18 at 18:48
  • @Felix [Probably because I embedded them incorrectly](https://stackoverflow.com/questions/562038/escaping-double-quotes-in-batch-script). Updated. – Maximilian Burszley Jul 06 '18 at 19:10
  • ok now it works, but I am still missing the quotation marks as the first space is interpreted as the argument of the program... How can I fix that? – Felix Jul 06 '18 at 22:06
  • What I need is that the path to the executing program is in quotes like this: "C:\Users\workspace 18\prim.exe" – Felix Jul 06 '18 at 22:13
0

By try and error I found a solution:

set mys='`\"C:\works pace\prim.exe`\"'
Powershell -Command "Start-Process -FilePath schtasks -ArgumentList '/Create', '/SC ONLOGON', '/TN CoUpdater', '/IT', '/RL HIGHEST', '/TR', \"%mys%\" -verb RunAs"

This worked for me and in the task scheduler it is displayed correctly: enter image description here

Felix
  • 31
  • 1
  • 3