Carson
Carson

Reputation: 7948

Create a schedule by the Schtask.exe

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?

Upvotes: 1

Views: 509

Answers (1)

Fenistil
Fenistil

Reputation: 3801

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

Upvotes: 1

Related Questions