简体   繁体   English

从命令提示符使用 PowerShell 命令运行 schtasks.exe

[英]Run schtasks.exe with PowerShell command from command prompt

I want to run the schtasks.exe as administrator using PowerShell -Command with Start-Process to create a task.我想使用PowerShell -CommandStart-Process以管理员身份运行schtasks.exe来创建任务。 This is the code of my .bat file I've got so far.这是我目前得到的.bat文件的代码。

 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 ):经过一些研究,我能够确定路径在任务调度程序中但没有引号( /TR 之后的参数):

任务调度器 1

The next thing I did was adding of quotes to a string and put it as the argument after /TR我做的下一件事是在字符串中添加引号并将其作为参数放在 /TR 之后

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

which results in:这导致: 任务调度器 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!谢谢!

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.只要有匹配的引号集,cmd 解析器就不会抱怨。

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:这对我有用,并且在任务计划程序中正确显示: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM