Pregunta

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.

¿Fue útil?

Solución

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

Otros consejos

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.

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();

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).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top