将控制台输出重定向到日志文件

问题描述:

我正在使用 C# 的 WMI API 以连接到远程服务器并执行一些命令.我已成功建立连接.我现在需要做的就是将远程 CMD 的输出重定向到本地机器的日志文件中.

I am working with the WMI API for c# in order to connect to a remote server and execute some commands. I have successfully established a connection. All I need now is to redirect the output of the remote CMD into a log file in my local machine.

这是我的代码:

ConnectionOptions options = new ConnectionOptions();
options.Username = "login";
options.Password = "password";
ManagementScope scope = new ManagementScope("\\\\myserver\\root\\cimv2", options);
scope.Options.EnablePrivileges = true;
scope.Options.Impersonation = System.Management.ImpersonationLevel.Impersonate;
try
{
    scope.Connect();
    System.Management.ManagementClass local_ClassInstance = new System.Management.ManagementClass(scope, new System.Management.ManagementPath("Win32_Process"), null);
    Console.WriteLine("SUCCESS");
    //execute the command
    System.Management.ManagementBaseObject local_InParams = local_ClassInstance.GetMethodParameters("Create");
    local_InParams["CommandLine"] = @"cmd.exe /C myCommand";
    local_InParams["CurrentDirectory"] = @"mypath";
    System.Management.ManagementBaseObject local_ManagementBaseObject = local_ClassInstance.InvokeMethod("Create", local_InParams, null);
}
catch (Exception e)
{
    Console.WriteLine("FAILURE"+e.ToString());
}

编辑

我试图通过使用>"原语来实现这一点:

I tried to accomplish this by using the '>' primitive :

local_InParams["CommandLine"] = "command > log.txt";

但我创建的输出文件不包含任何内容.

But the output file that I created doesn't contain anything.

我也尝试使用进程来做到这一点

I tried also to do this using a process

Process myProcess = new Process();
myProcess.StartInfo.FileName = "ipconfig";
myProcess.StartInfo.Arguments = "/all";
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.Start();
myProcess.WaitForExit();
string myResult = myProcess.StandardOutput.ReadToEnd();
Console.WriteLine(myResult);
myProcess.Close();

但是进程没有返回我想要的信息,因为我想要远程机器的cmd的输出(因为我想要运行命令时服务器行为的日志).

But the process does not return the information that I want because I want the output of cmd of the remote machine (Because I want the log of the behaviour of the server while running the command).

有什么帮助吗?

我也遇到了捕获问题,并发现了另一个与您所拥有的类似的重定向...

I too had an issue with capturing, and found ANOTHER redirect that works similar to what you have...

myProcess.StartInfo.RedirectStandardError = true;
// trap normal data received AND any ERROR data received too
myProcess.OutputDataReceived += DOSOutputResultsHandler;
myProcess.ErrorDataReceived += DOSOutputErrorsHandler;

我还有两个字符串生成器属性,用于捕获执行 DOS 调用过程的类上的输出响应

I also have two string builder properties for capturing the output responses on my class that does the DOS Call process

StringBuilder DOSOutputResults = new StringBuilder();
StringBuilder DOSOutputErrors = new StringBuilder();


protected void DOSOutputResultsHandler(object sendingProcess,
   System.Diagnostics.DataReceivedEventArgs outLine)
{
   if (!string.IsNullOrEmpty(outLine.Data))
      // track data into the NORMAL output string builder
      DOSOutputResults.Append(Environment.NewLine + outLine.Data);
}


protected void DOSOutputErrorsHandler(object sendingProcess,
   System.Diagnostics.DataReceivedEventArgs outLine)
{
   if (!String.IsNullOrEmpty(outLine.Data))
      // track data into the ERROR output string builder
      DOSOutputErrors.Append(Environment.NewLine + outLine.Data);
}

此外,为了使用原语 ">" 重定向,还有一个 "2>" 重定向来处理未重定向到正常输出的错误.我在处理对 Java 的 DOS 调用时发现了这一点.

Additionally, for using the primitive ">" redirection, there is also a "2>" redirection that handles errors not redirected to normal output. I found this out when dealing with DOS calls to Java.