使用 WMI 在远程服务器上执行命令并将输出保存在本地机器中

时间:2021-01-20 17:09:58

标签: c# wmi remote-server remote-connection

我需要在远程服务器上执行命令并获取结果并将其保存在我的开发服务器中。

ConnectionOptions options = new ConnectionOptions();
options.Username = "QA_Username"; 
options.Password = "QA_Password";

ManagementScope scope = new ManagementScope(@"\\My_QA_Server\root\cimv2", options);          
scope.Connect();

ManagementClass classInstance = new ManagementClass(scope, new ManagementPath("Win32_Process"), null);
ManagementBaseObject inParams = classInstance.GetMethodParameters("Create");

inParams["CommandLine"] = @"cmd.exe /S /C ipconfig > \\My_Dev_Server\d$\Status\Output.txt";

ManagementBaseObject outParams = classInstance.InvokeMethod("Create", inParams, null);

如果我像下面这样更改代码,将结果保存在远程服务器的 Output.txt 中可以正常工作

inParams["CommandLine"] = @"cmd.exe /S /C ipconfig > \\My_QA_Server\d$\Status\Output.txt";
inParams["CommandLine"] = @"cmd.exe /S /C ipconfig > d:\Status\Output.txt";

但是如果我提供运行代码的开发服务器路径,它不会保存 Output.txt

inParams["CommandLine"] = @"cmd.exe /S /C ipconfig > \\My_Dev_Server\d$\Status\Output.txt";

如何将结果保存在开发服务器路径中?

0 个答案:

没有答案
相关问题