Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查计划任务是否存在并检查状态_C# - Fatal编程技术网

C# 检查计划任务是否存在并检查状态

C# 检查计划任务是否存在并检查状态,c#,C#,我正在使用以下代码更改远程主机上计划任务的“运行方式:”用户名和密码 Process p = new Process(); p.StartInfo.UseShellExecute = false; p.StartInfo.FileName = "SCHTASKS.exe"; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.StartInfo.WindowStyle = ProcessWi

我正在使用以下代码更改远程主机上计划任务的“运行方式:”用户名和密码

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();
我有几个问题:

  • 我如何检查指定的服务是否存在,这样如果它不存在,我就可以退出程序
  • 如何检查计划任务是否正在运行或已禁用
  • 如果禁用了计划任务,我仍然可以更改凭据吗?或者它是否像Windows服务一样,如果禁用,则无法更改凭据

  • 看看我给你的链接。的链接

    查看名为“查询任务信息”的部分。

    这是我检查当前运行状态的代码。您可以根据需要修改输出

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

    我知道这有点晚了,但是,我真的很想发布这个。(因为我喜欢简单的代码):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.";
                }
            }
        }
    }
    
    功能:

    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.";
                }
            }
        }
    }
    

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

    您的return语句给出了以下错误:未处理的异常:System.IndexOutOfRangeException:Index在数组的边界之外。当我运行它时,返回了2行。第一行是这样的:`Folder:`也许你的不一样?在命令窗口中手动调用该进程
    SCHTASKS.exe/Query/TN YourTaskName/FO TABLE/NH
    查看结果。错误:无效参数/选项-'/TN'。键入“SCHTASKS/QUERY/?”了解用法。我把任务名改成了我的。我不能为你做任何事。键入
    SCHTASKS/QUERY/?
    并查看其内容。使用
    Arguments=“/QUERY/TN\”“+taskname+”\”如果“taskname”包含空格。@Kiquenet尝试在控制台中运行它,您可以看到输出。试着把它从那里分离出来:)解析任务检查状态的输出(字符串?@Kiquenet在我上面的例子中,stdout是一个字符串,它包含可以解析以获得状态的输出。