1

I am working on a project that will query scheduled tasks running on other systems. I have written a console application that runs schtasks and redirects the output to a text file. However, I was wondering if it is possible to redirect this output to a SQL database?

I was thinking of writing a stored procedure which will handle all of the inserts but I don't know what to do with the data inside the console app. I already have a stored proc that redirects XML and text data to a database that I created in the past but I am trying to not have files laying around every where.

Any input would be great. Here is my code in case anyone wants to see it:

Process process = new Process();
process.StartInfo.FileName = "C:\\Windows\\System32\\schtasks.exe";
process.StartInfo.Arguments = "/query /s 192.168.0.124";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();

string procOutput = process.StandardOutput.ReadToEnd();

Below here is just the redirection to a text file using a text writer.

GSerg
  • 76,472
  • 17
  • 159
  • 346
Matt
  • 1,220
  • 3
  • 21
  • 36

4 Answers4

1

I would write a webservice that this little program calls to post it's information to. Have that webservice write results to a database and expose methods from that service to retrieve the information back. Using a set of technologies that most programmers are familar with makes it easier to maintain. The only question is, how will your console app be executed? Is it worth the effort to convert the console app into a windows service? Have it listen for incoming requests to do its stuff. WCF is very handy for handling these kinds of tasks. What do you think?

WCF Landing Page

How to: Host a WCF Service in a Managed Windows Service

You'll need to talk with your network guys to make sure your web server can open a connection to the target machine where the wcf service is installed.

Jay
  • 6,224
  • 4
  • 20
  • 23
  • My plan (note I have gone down the paths of web and windows form but they wouldn't do what I needed) is to have a web front end that will retrieve the data from the database. I can then add more functionality using schtasks to allow them to start and stop tasks based on permissions stored in another table. I had thought about WCF but didn't know if it created more of a security risk since it ran as a service and we are accessing the OS through schtasks. I have never written a service so maybe you can fill me in on that. – Matt May 24 '11 at 20:09
  • What your describing sounds like a windows service would be perfect for. Your service should run under the 'network_service' user context. It should be able to execute schtasks, but i'm not 100% on this. Have you looked into WMI? It may be able to provide you with the information you need without executing an external program. Added some resources up top to get you started. – Jay May 24 '11 at 20:34
  • I had only looked at the COM object up to this point but did not care for the output options. Then again, I never played with it either. I just quickly looked at the WMI class and, from what I have seen, has several useful properties. I will check this out and see how it looks. Thank you for the input. – Matt May 24 '11 at 20:47
  • Just wanted to add WMI can be access by remote machines, so you may be able to hook up several machines from a single deployment. – Jay May 24 '11 at 20:51
1

yes, it is possible in a single line by leveraging for and sqlcmd variables:

for /F "skip=1 delims=, tokens=1,2,3*" %i in ('schtasks.exe /query /FO CSV /s <server>') do sqlcmd -E -S <dbserver> -d <db> -Q "insert into <table> (TaskName, NextRun, Status) values ('$(TaskName)', '$(NextRun)', '$(Status)');" /v TaskName=%i /v NextRun=%j /v Status=%k

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • Nice. I guess I can just run that as a batch file? – Matt May 24 '11 at 21:22
  • Yes, just be aware that inside a batch (.cmd, .bat) you need to use `%%i`, `%%j` and `%%k`. – Remus Rusanu May 24 '11 at 21:24
  • Although if you already have an app, perhaps you should cut out the middleman and just enumerate the tasks yourself. Use the TaskScheduler COM class, see http://msdn.microsoft.com/en-us/library/aa446881%28v=vs.85%29.aspx – Remus Rusanu May 24 '11 at 21:28
  • I looked at the COM class but I need to play around with it more. The output was not what I was looking for. I am sure I can get the same type of output as schtasks but I just need to spend some time with it. – Matt May 24 '11 at 21:30
0

If the program is small, will run in a trusted machine, just use a SqlConnection (or the connection equivalent to your rdmbs) to create a command, and execute it passing procOutput as a parameter). Something like this:

var connection = new System.Data.SqlClient.SqlConnection(connection);
            var command = connection.CreateCommand();
            command.CommandText = "procedureName";
            command.Parameters.Add(new System.Data.SqlClient.SqlParameter("paramName", "output"));
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.ExecuteNonQuery();
  • It will run in a trusted machine. Will the redirected output work as a parameter? Meaning will I need to have multiple parameters for each piece of output (i.e. TaskName, Status, etc.)? – Matt May 24 '11 at 20:03
0

Sure. The lazy way to do this would be to:

  • redirect it as you do now.
  • where you read from standard out and standard error, rather than directly writing to a text file, use log4net.
  • configure log4net with a SQL appender to catch the log messages you write.
  • if you like, also configure a rolling file appender so that the log messages are also written to the log file.
  • You can get fancy and add an event log appender so that standard error gets written to the event log, so the operations guys can wire up an alert.

I like to use log4net this way because it makes doing production batch stuff simple. I can get normal console output, while at the same time I catch it in a text file, sql database or the windows event log. Makes the operations guys happy(ier).

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • I don't know much about that. Will it handle things like commands being written to the console or will I still need to write the data to some medium? – Matt May 24 '11 at 20:35
  • See http://logging.apache.org/log4net/index.html for details on log4.net. Here's a config file that logs to a log file and to email: http://weblogs.asp.net/tgraham/archive/2007/03/15/a-realistic-log4net-config.aspx. Examples of log4net appenders can be found at http://logging.apache.org/log4net/release/config-examples.html. log4net takes anything written via an ILog instance. – Nicholas Carey May 25 '11 at 16:51
  • If you wanted to trap anything written to the Console, you'd need to replace the console's TextWriters for standard output and standard error with custom TextWriters that would run the output to log4net. Then you'd need to be careful about the log4net configuration (if you've got a ConsoleAppender configured, you're likely to pipe your output to log4net that sends it to the console that sends it to log4net ... you get the picture. – Nicholas Carey May 25 '11 at 16:52