重定向 C# 进程输出
我正在开发一个项目,该项目将查询在其他系统上运行的计划任务。我编写了一个控制台应用程序,它运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,通过利用
用于
和 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
我会编写一个网络服务,这个小程序调用它来发布它的信息。让该 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.
如果程序很小,将在受信任的机器上运行,只需使用 SqlConnection(或相当于 rdmbs 的连接)创建命令,并通过 procOutput 作为参数执行它)。像这样的东西:
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:
当然。执行此操作的懒惰方法是:
我喜欢以这种方式使用 log4net,因为它使批量生产变得简单。我可以获得正常的控制台输出,同时我可以在文本文件、sql 数据库或 Windows 事件日志中捕获它。让操作人员感到高兴。
Sure. The lazy way to do this would be to:
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).