我正在使用用于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的输出过程不返回我想要的信息(因为我想的日志做服务器在运行命令时的行为)。
请帮忙吗?
对于如何将命令提示符命令的输出重定向到文件,您做了哪些研究?这是非常容易发现的信息。 – Servy
我看到我可以在我的命令后用'>'或'>>'来做到这一点,但它不起作用。我还看到了使用System.Management.ManagementBaseObject执行此操作的另一种方式,但它不重定向控制台的内容,而是重定向进程日志的内容。 –
“它不起作用”不会告诉我们任何事情。向我们展示你的尝试,并详细解释你所做的每一次尝试都会发生什么。 – Servy