如何在C#上远程机器上执行进程

DJP*_*JPB 13 c# system.diagnostics process

如何使用System.Diagnostics.Process类在c#中使用计算机名称="someComputer"在远程计算机上启动进程?

我在那台远程计算机上创建了一个小型控制台应用程序,它只是将"Hello world"写入txt文件,我想远程调用它.

控制台应用程序路径:c:\ MyAppFolder\MyApp.exe

目前我有这个:

ProcessStartInfo startInfo = new ProcessStartInfo(string.Format(@"\\{0}\{1}", someComputer, somePath);

            startInfo.UserName = "MyUserName";
            SecureString sec = new SecureString();
            string pwd = "MyPassword";
            foreach (char item in pwd)
            {
                sec.AppendChar(item);
            }
            sec.MakeReadOnly();
            startInfo.Password = sec;
            startInfo.UseShellExecute = false;

            Process.Start(startInfo);
Run Code Online (Sandbox Code Playgroud)

我一直在"找不到网络路径".

Iva*_* G. 16

可以使用来自http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx的 PsExec

或者WMI:

object theProcessToRun() = { "YourFileHere" };

ManagementClass theClass = new ManagementClass(@"\\server\root\cimv2:Win32_Process");

theClass.InvokeMethod("Create", theProcessToRun);
Run Code Online (Sandbox Code Playgroud)

  • 顺便说一句,"YourFileHere"需要是服务器本地的路径,你可以为它添加参数,例如"C:\\ Whatever \\ Whatever.exe -param1 -param2"和该字符串中的"server"需要实际的服务器名称或IP,例如"\\ localhost\root\cimv2:Win32_Process" (5认同)

Sev*_*yev 14

使用以下之一:

或者,如果您愿意,请注入您自己的服务或COM组件.这与PsExec的作用非常接近.

在所有这些方法中,我更喜欢任务调度程序.我认为,这些都是最干​​净的API.连接到远程任务调度程序,为可执行文件创建新任务,运行它.注意:可执行文件名应该是计算机的本地名称.不是\ servername\path\file.exe,而是c:\ path\file.exe.如果您愿意,请删除该任务.

所有这些方法都要求您具有对目标计算机的管理访问权限.

ProcessStartInfo 无法启动远程进程.


Aus*_*nen 7

根据MSDN,Process对象只允许访问远程进程而不能启动或停止远程进程.所以要回答你关于使用这个课程的问题,你不能.