tags:

views:

42

answers:

2

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 achieve this?

+1  A: 

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);
}
Gabriel McAdams
what do you mean by activeDirectoryDomainName?
xbonez
I am first trying to achieve the same on my local machine, so I ommitted MachineName from the list of arguments, so it defualts to local system
xbonez
activeDirectoryDomainName is just the windows domain for the login. If you are part of the domain, then your login looks something like this: `domain\username`. Otherwise, it looks like this: `username`
Gabriel McAdams
gotcha. thanks.
xbonez