4

for my application I want to be monitoring the "Status" tab of specific scheduled task on a remote server and perform actions based on that status. ie, run a task if it is not already running, etc.

As of right now, I am launching cmd.exe as a process in C#, psexec'ing that machine's schtasks into a text file, reading that text file, and then I perform the corresponding action.

Are there better ways to do this process (get a specific status of a schtask) that do not involve 3rd party opens-source libs?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Kevin Zhou
  • 1,273
  • 4
  • 17
  • 25

2 Answers2

1

You could try using the native TaskService COM API:

//add reference to the 'Task Scheduler 1.1 Type Library'

using TaskScheduler;

//...

TaskSchedulerClass ts = new TaskSchedulerClass();
ts.Connect(remotehost, user, domain, password);
IRunningTaskCollection tasks = ts.GetRunningTasks(1);
foreach (IRunningTask task in tasks)
{
    Console.WriteLine(task.Name);
}

For some task on security and permissions see http://msdn.microsoft.com/en-us/library/windows/desktop/aa382140(v=vs.85).aspx

Alternatively, if powershell is an option that might be easier depending on the version.

Kyle
  • 1,366
  • 2
  • 16
  • 28