2010-10-27 96 views
5

我正在使用以下代码更改远程主机上计划任务的'Run As:'用户名和密码。检查计划任务是否存在并检查状态

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

我有几个问题:

  1. 我如何检查是否指定的服务存在的话,那么,如果它不存在,我可以退出程序。
  2. 如何检查计划任务是否正在运行或停用?
  3. 如果计划任务处于禁用状态,我仍可以更改凭据吗?还是像Windows服务一样,如果密码被禁用,则凭据无法更改?

回答

6

看看我给你的链接my last answer。链接为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"); 
+0

你的返回语句给出了这个错误:未处理的异常:System.IndexOutOfRangeException:索引超出了数组的缺省值 ds。 – xbonez 2010-10-27 18:10:20

+0

当我运行它时,返回了2行。第一行看起来像这样:'文件夹:\'也许你的不同?当你运行上面的代码时,你的代码是 – 2010-10-27 18:19:11

+0

?我无法运行错误 – xbonez 2010-10-27 18:26:32

5

我知道这有点晚了,但是,我真的想发布这个。 (这是因为我喜欢简单的代码):d的使用

实施例:

MessageBox.Show("The scheduled task's 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."; 
      } 
     } 
    } 
} 
+1

使用'Arguments =“/ query/TN \”“+ taskname +”\“”;'以防万一“taskname”包含空格。 – Manish 2017-09-26 06:52:56

+0

@manish啊是的,好点 – 2017-09-27 00:57:41

-1

只需拨打schtask /查询。/TN选项不起作用只返回一个错误。但是,如果您只是使用/ Query,您仍然可以搜索任务的输出。