2014-02-17 146 views
0

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

这里是我的代码:

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

编辑

我试图通过使用来完成这次 '>' 原始:

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

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

我想也是这个使用过程

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的输出过程不返回我想要的信息(因为我想的日志做服务器在运行命令时的行为)。

请帮忙吗?

+0

对于如何将命令提示符命令的输出重定向到文件,您做了哪些研究?这是非常容易发现的信息。 – Servy

+0

我看到我可以在我的命令后用'>'或'>>'来做到这一点,但它不起作用。我还看到了使用System.Management.ManagementBaseObject执行此操作的另一种方式,但它不重定向控制台的内容,而是重定向进程日志的内容。 –

+1

“它不起作用”不会告诉我们任何事情。向我们展示你的尝试,并详细解释你所做的每一次尝试都会发生什么。 – Servy

回答

1

我也有一个问题与捕获,并发现了另一个重定向的工作原理类似,你有什么...

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

我也有两个字符串生成器属性捕捉我的类,它的输出响应的DOS调用过程

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>”重定向。处理DOS对DOS的调用时,我发现了这一点。

+0

谢谢你的帮助。但日志没有任何回报给我。同样的问题。 –

+0

是否应该开始处理以获得输出? –

+0

因为要开始这个过程,我应该给它一个文件来执行。 –

相关问题