英文:
Create a schedule by the Schtask.exe
问题
我尝试创建一个计划表,但是当我运行以下代码时,总是出现错误(exit status 0x80004005
):
package main
import (
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("schtasks.exe", "/Create",
"/SC ONLOGON",
"/TN MyTask",
)
if err := cmd.Run(); err != nil {
fmt.Println(err.Error())
}
}
即使我以管理员权限运行它,我仍然得到相同的结果。
我该如何解决这个问题,或者是否有其他方法?
英文:
I try to create a schedule, but I always get the error (exit status 0x80004005
) when I run the
package main
import (
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("schtasks.exe", "/Create",
"/SC ONLOGON",
"/TN MyTask",
)
if err := cmd.Run(); err != nil {
fmt.Println(err.Error())
}
}
Even if I run it with administrator
privileges, I still get the same result.
How do I solve it, or is there any other way?
答案1
得分: 1
首先,你必须将每个参数放入单独的数组项中,并且必须添加/ TR参数来指定应该运行哪个程序:
cmd := exec.Command("schtasks.exe", "/Create",
"/SC",
"ONLOGON",
"/TN",
"MyTask",
"/TR",
"calc.exe",
)
if err := cmd.Run(); err != nil {
fmt.Println(err.Error())
}
英文:
First of all, you must put every parameter into separate array items, and you must add the /TR paramteter to specify which program it should run:
cmd := exec.Command("schtasks.exe", "/Create",
"/SC",
"ONLOGON",
"/TN",
"MyTask",
"/TR",
"calc.exe",
)
if err := cmd.Run(); err != nil {
fmt.Println(err.Error())
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论