我正在使用用於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