重定向 C# 进程输出

发布于 2024-11-09 15:37:43 字数 618 浏览 5 评论 0原文

我正在开发一个项目,该项目将查询在其他系统上运行的计划任务。我编写了一个控制台应用程序,它运行 schtasks 并将输出重定向到文本文件。但是,我想知道是否可以将此输出重定向到 SQL 数据库?

我正在考虑编写一个存储过程来处理所有插入,但我不知道如何处理控制台应用程序内的数据。我已经有一个存储过程,它将 XML 和文本数据重定向到我过去创建的数据库,但我试图不让文件遍布各处。

任何输入都会很棒。这是我的代码,以防有人想看它:

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

下面只是使用文本编写器重定向到文本文件。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黯然#的苍凉 2024-11-16 15:37:43

是的,通过利用 用于sqlcmd 变量

for /F "skip=1 delims=, tokens=1,2,3*" %i in ('schtasks.exe /query /FO CSV /s')执行 sqlcmd -E -S; -d; -Q“插入<表>(任务名称,下一步运行,状态)值('$(任务名称)','$(下一步运行)','$(状态)');” /v 任务名称=%i /v NextRun=%j /v 状态=%k

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

薄暮涼年 2024-11-16 15:37:43

我会编写一个网络服务,这个小程序调用它来发布它的信息。让该 Web 服务将结果写入数据库并公开该服务的方法以检索信息。使用一组大多数程序员熟悉的技术使其更易于维护。
唯一的问题是,您的控制台应用程序将如何执行?
将控制台应用程序转换为 Windows 服务是否值得?让它监听传入的请求来完成它的工作。 WCF 对于处理此类任务非常方便。
你怎么认为?

WCF 登录页面

如何:在托管 Windows 服务中托管 WCF 服务

您需要与您的网络人员交谈,以确保您的 Web 服务器可以打开与安装 wcf 服务的目标计算机的连接。

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.

甜柠檬 2024-11-16 15:37:43

如果程序很小,将在受信任的机器上运行,只需使用 SqlConnection(或相当于 rdmbs 的连接)创建命令,并通过 procOutput 作为参数执行它)。像这样的东西:

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

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();
蹲墙角沉默 2024-11-16 15:37:43

当然。执行此操作的懒惰方法是:

  • 像现在一样重定向它。
  • 从标准输出和标准错误读取,而不是直接写入文本文件,请使用 log4net。
  • 使用 SQL 附加程序配置 log4net 以捕获您编写的日志消息。
  • 如果您愿意,还可以配置滚动文件附加程序,以便日志消息也写入日志文件。
  • 您可以添加一个事件日志附加程序,以便将标准错误写入事件日志,以便操作人员可以连接警报。

我喜欢以这种方式使用 log4net,因为它使批量生产变得简单。我可以获得正常的控制台输出,同时我可以在文本文件、sql 数据库或 Windows 事件日志中捕获它。让操作人员感到高兴。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文