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

问题描述

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

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";

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)