使用Schtask.exe创建一个日程安排。

huangapple go评论80阅读模式
英文:

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())
}

huangapple
  • 本文由 发表于 2022年5月4日 15:25:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/72109291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定