I use the following code to change the Run As: username and password for a scheduled task on a remote host.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "SCHTASKS.exe";
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.Arguments = String.Format(
"/Change /S {0} /TN {1} /TR {2} /RU {3}\\{4} /RP {5}",
MachineName, ScheduledTaskName, taskPath, activeDirectoryDomainName, userName, password);
p.Start();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
I have a couple of questions:
- How to check if the specified service exists at all, so if it does not exist, I can just exit the program.
- How can I check if a scheduled task is running or disabled?
- If the scheduled task is disabled, can I change the credentials or is it like a Windows service where the credentials cannot be changed if they are disabled?
source
share