C# 在远程计算机上运行程序

C# 在远程计算机上运行程序,c#,wmi,remote-server,C#,Wmi,Remote Server,我有以下代码: ConnectionOptions connOptions = new ConnectionOptions(); connOptions.Impersonation = ImpersonationLevel.Impersonate; connOptions.EnablePrivileges = true; ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", sHost)

我有以下代码:

ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", sHost), connOptions);
manScope.Connect();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_Process");
ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions);
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
inParams["CommandLine"] = "notepad.exe";
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
当我将sHost设置为我的本地IP地址时,它会工作,记事本就会打开。 但是,当我将其设置为远程服务器时,什么也没有发生

(我可以连接到远程计算机并从我的应用程序复制文件-这只是为了尝试远程运行东西。还为WMI配置了防火墙)

有点没有更多的想法


谢谢。

也许,你应该使用PsTools(从你的应用程序运行)?非常适合远程部署。

我想您已经检查过记事本没有在远程机器的后台运行(在任务管理器中查找)?您没有连接到登录用户的桌面会话,因此它可能不会显示在屏幕上。谢谢您的回复。是的,我检查过了,但是没有任何东西可以运行它不能在后台运行。我还尝试将用户名和密码设置为“connOptions”,但仍然没有任何结果…你能检查outParams[“returnValue”]和outParams[“processId”]的值吗?哦,现在它可以工作了…为了进入域,我必须将权限添加到“connOptions”。现在它运行正常!现在的问题是Rhumboll首先提到的——流程在后台运行(可以在任务管理器中显示)。有什么办法吗?另外,“记事本”就是一个例子,实际上我正在运行一个批处理脚本。脚本完成后,“cmd”在后台运行。有什么办法克服它吗?谢谢PS工具比WMI路由快得多相信我。