Code to check the staus of scheduled task in remote machine and fire email

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi Folks,

I do have a requirement:

1.Need to get the status of all scheduled tasks schduled in a remote machine
2.Based on status need to send an alert email


This is my sample code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32.TaskScheduler;
using System.Threading;
using System.Diagnostics;
using System.Net;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Data;
using System.Devices;
//using System.Devices.RemoteDeviceManager;
//using System.Diagonistics.EventLog;


namespace TaskSchedulerRemote
{class Program
{
//TaskService 01_server = getTaskService("\\123.122.222.222", "administrator", "domain1", "pwd0", true);
//TaskService 02_server = getTaskService("\\123.122.222.222", "administrator", "domain2", "pwd1", true);
static void Main(string[] args)
{
using (TaskService task = new TaskService())
{
TaskDefinition td = task.NewTask();
td.RegistrationInfo.Description = "RF Report";
td.Actions.Add(new ExecAction("notepad.exe", "c:test.log", null));
while (true)
{
Task taskdDef = task.FindTask("RF Report");
//string registeredTask;
//string[] taskCollection = { "", "", "" };
//foreach (string str in taskCollection)
//{

//For Each registeredTask In taskCollection

switch (taskdDef.State)
{
case TaskState.Queued:
Console.WriteLine("Queued");
break;
case TaskState.Ready:
Console.WriteLine("Ready");
break;
case TaskState.Unknown:
Console.WriteLine("Unknown");
break;
case TaskState.Disabled:
Console.WriteLine("Disabled");
break;
case TaskState.Running:
Console.WriteLine("Running");
break;
}
Thread.Sleep(10000);
//MailMessage Msg = new MailMessage();
//MailAddress fromMail = new MailAddress("emailid");
//// Sender e-mail address.
//Msg.From = fromMail;
//// Recipient e-mail address.
//Msg.To.Add(new MailAddress("emailid"));
//Msg.Priority = MailPriority.High;
//// Subject of e-mail

//Msg.Subject = "Scheduled Task Status Report:" + System.DateTime.Now;




//Msg.Body += "<html><body><Table><tr><td>Hi All,</td></tr><tr><td>Please find below the status report of schduled task :</td></tr></Table></body></html><html><body>";
//Msg.Body += "<html><body><Table><tr><td>Regards,</td></tr><tr><td></td></tr><tr><td></td></tr>" +
//"<tr><td><b>NOTE: This is an automated mail. Please, do not reply.</b> </td></tr></table></body></html>";
//Msg.IsBodyHtml = true;
//string sSmtpServer = "";
//sSmtpServer = "mailservername";
//SmtpClient a = new SmtpClient();
//a.Host = sSmtpServer;
//a.EnableSsl = false;
//try
//{
// Console.WriteLine("Sending mail...");
// a.Send(Msg);
// Console.WriteLine("Mail was sent successfully!");
//}
//catch (Exception ep)
//{
// Console.WriteLine("failed to send mail:");
// Console.WriteLine(ep.Message);
//}



//}
}

string sSource = "MyCustom Group";
string sLog = "Application";
string sEvent = "";


if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource, sLog);


EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Error, 234);



}
}
}}


Not sure how to connect to the remote server/machine and read and get the task status details and also need to amend that status details in email.

Could anyone please look into my code and suggest.

Many Thanks,
Sisir

View the full article
 
Back
Top