开发者

Changing the 'Run As:' field for a scheduled task via C#

开发者 https://www.devze.com 2023-01-21 17:26 出处:网络
I need to write a C# program to change the \'Run As\' field of scheduled tasks on remote systems. What are some of the libraries I should be looking at to achi开发者_开发百科eve this?You can spawn a

I need to write a C# program to change the 'Run As' field of scheduled tasks on remote systems.

What are some of the libraries I should be looking at to achi开发者_开发百科eve this?


You can spawn a process to call SCHTASKS.exe

Here is some code that I used to do just that:

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} /RU {2}\\{3} /RP {4}",
    MachineName,
    ScheduledTaskName,
    activeDirectoryDomainName,
    userName,
    password
);

p.Start();
// Read the error stream first and then wait.
string error = p.StandardError.ReadToEnd();
p.WaitForExit();

if (!String.IsNullOrWhiteSpace(error))
{
    throw new Exception(error);
}


maybe : http://bartdesmet.net/blogs/bart/archive/2008/02/23/calling-the-task-scheduler-in-windows-vista-and-windows-server-2008-from-managed-code.aspx ?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号