如何在远程计算机中执行命令?

use*_*179 21 .net c# command process

我在服务器中有一个共享文件夹,我需要远程执行某些文件的命令.我怎么做?

需要在服务器上运行哪些服务才能使其工作?

一些细节:只能使用C#.服务器中无法安装任何内容.

Den*_*nis 32

另一种解决方案是使用WMI.NET或Windows Management Instrumentation.

使用.NET Framework命名空间System.Management,您可以使用Windows Management Instrumentation(WMI)自动执行管理任务.

代码示例

using System.Management;
...
var processToRun = new[] { "notepad.exe" };
var connection = new ConnectionOptions();
connection.Username = "username";
connection.Password = "password";
var wmiScope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", REMOTE_COMPUTER_NAME), connection);
var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
wmiProcess.InvokeMethod("Create", processToRun);
Run Code Online (Sandbox Code Playgroud)

如果您在身份验证方面遇到问题,请检查DCOM配置.

  1. 在目标计算机上,dcomcnfg从命令提示符运行.
  2. 扩大 Component Services\Computers\My Computer\DCOM Config
  3. 查找使用GUID标识的Windows管理指令8BC3F05E-D86B-11D0-A075-00C04FB68820(您可以在详细信息视图中查看).
  4. 编辑属性,然后在权限选项卡下添加您尝试登录的用户名.
  5. 您可能需要重新启动服务或整个计算机.

注意:用于远程进程的所有路径都必须是目标计算机的本地路径.

  • 这个解决方案可以运行一个应用程序,但没有交互(没有接口) (3认同)

Dir*_*mar 6

你可以使用SysInternal的PsExec.