Check progress and check scheduled tasks

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 /TN {0} /RU {1} /RP {2}",ScheduledTaskName,userName,password);
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();
// Read the error stream first and then wait.
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?
+5
source share
3 answers

, . SCHTASKS.exe.

Querying for Task Information.

, . , .

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "SCHTASKS.exe";
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

p.StartInfo.Arguments = String.Format("/Query /S {0} /TN {1} /FO TABLE /NH", MachineName, ScheduledTaskName);

p.Start();
// Read the error stream
string error = p.StandardError.ReadToEnd();

//Read the output string
p.StandardOutput.ReadLine();
string tbl = p.StandardOutput.ReadToEnd();

//Then wait for it to finish
p.WaitForExit();

//Check for an error
if (!String.IsNullOrWhiteSpace(error))
{
    throw new Exception(error);
}

//Parse output
return tbl.Split(new String[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries)[1].Trim().EndsWith("Running");
+6

, , . ( , ): D

:

MessageBox.Show("The scheduled task existance is " + taskexistance("TASKNAMEHERE").ToString());

:

private string taskexistance(string taskname)
{
    ProcessStartInfo start = new ProcessStartInfo();
    start.FileName = "schtasks.exe"; // Specify exe name.
    start.UseShellExecute = false;
    start.CreateNoWindow = true;
    start.WindowStyle = ProcessWindowStyle.Hidden;
    start.Arguments = "/query /TN " + taskname;
    start.RedirectStandardOutput = true;
    // Start the process.
    using (Process process = Process.Start(start))
    {
        // Read in all the text from the process with the StreamReader.
        using (StreamReader reader = process.StandardOutput)
        {
            string stdout = reader.ReadToEnd();
            if (stdout.Contains(taskname)) {
                return "true.";
            }
            else
            {
                return "false.";
            }
        }
    }
}
+6

schtask/Query. /TN , ERROR. /Query, .

-1

All Articles