I am trying to create a scheduled task from a Windows service.
The service runs as nt authority\system. If I open a command prompt as nt authority\system, I can create the scheduled task with:
C:\\Windows\\System32\\schtasks.exe /RU "NT AUTHORITY\SYSTEM" /Create /SC ONCE /TN MyTaskNT1339 /TR C:\\Users\\Administrator\\AppData\\Local\\my.exe /ST 13:39
With this, once 1339 comes around, my.exe runs properly, so, I believe that all permissions are fine.
The service I am running is this Go block:
user, err := user.Current()
username := user.Username
cmd := exec.Command("C:\\Windows\\System32\\schtasks.exe",
"/RU",
username,//I have also tried "NT AUTHORITY\\SYSTEM", "\"NT AUTHORITY\\SYSTEM\"", etc
"/Create",
"/SC",
"ONCE",
"/TN",
"MyTaskSched1320",
"/TR",
"C:\\Users\\Administrator\\AppData\\Local\\my.exe",
"/ST",
"13:20",
)
if err := cmd.Run(); err != nil {
fmt.Println("run error")
fmt.Println(err.Error()) // this outputs "exit status 1"
}
fmt.Println("sched test service end")
}
Which is started as a service via nssm.
In addition to the "exit status 1" in the log, when trying to start the service with the above scheduled task block I get the error popup: "Windows could not start the service_name service on Local Computer. The service did not return an error. This could be an internal Windows error or an internal service error. If the problem persists, contact your administrator."
If I change the cmd block in the service to simply:
cmd := exec.Command("C:\\Users\\Administrator\\AppData\\Local\\my.exe",
the service will run my.exe as well, but not as a scheduled task.
It appears that you need to specify /RU "NT AUTHORITY\SYSTEM" for a service to be created from a nt authority\system user.
I am using the EC2 Windows_Server-2022-English-Full-Base-2023.08.10 AMI.