15

How can I start a process on a remote computer in c#, say computer name = "someComputer", using System.Diagnostics.Process class?

I created a small console app on that remote computer that just writes "Hello world" to a txt file, and I would like to call it remotely.

Console app path: c:\MyAppFolder\MyApp.exe

Currently I have this:

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

I keep getting "Network path was not found".

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
DJPB
  • 5,429
  • 8
  • 30
  • 44

5 Answers5

17

Can can use PsExec from http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

Or WMI:

object theProcessToRun() = { "YourFileHere" };

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

theClass.InvokeMethod("Create", theProcessToRun);
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Ivan G.
  • 5,027
  • 2
  • 37
  • 65
  • 1
    I posted a CW answer quoting MSDN on the Process class that you should incorporate into your answer to cover the question on using Process. – Austin Salonen Feb 26 '10 at 18:12
  • tks for your answer? By the way, do you know if SysInternals have full official support by microsoft? – DJPB Mar 04 '10 at 14:55
  • 5
    Btw, "YourFileHere" needs to be the path local to the server, and you can append parameters to it, e.g. "C:\\Whatever\\Whatever.exe -param1 -param2" and "server" in that string needs to be the actual server name or ip, e.g. "\\localhost\root\cimv2:Win32_Process" – Mike Trusov Mar 21 '13 at 05:55
  • Some WMI authentication tips in this answer: http://stackoverflow.com/a/10414095/12484 – Jon Schneider Sep 01 '15 at 15:28
  • Am I missing something with this one? When I try executing the first line `object theProcessToRun() = { "YourFileHere" };` with a path to the file, there are just loads of errors (cannot specify constructor arguments in declaration...) Have you already initialised something with `theProcessToRun()`?.. – Bassie Jun 21 '16 at 16:05
  • @Bassie Yeah - saw that, too - added an edit that hopefully will get accepted. It should be `object theProcessToRun = new[] { "YourFileHere" };`, instead. – vapcguy Apr 14 '17 at 22:06
  • @ live2 Making code changes to answers is generally frowned upon, especially in the case of an 8 year old answer. A better approach would be to post a new answer that expands on this one by updating the code to current environments - and of course, offering your additional solution. – Mogsdad Jan 04 '18 at 16:21
  • I have tried WMI for launching remote process. They work perfectly fine for the local system. However, when then processes are launch on a remote system, they run only in the background and NOT interactively. I am searching for an alternate solution. Please share if you know. – Akhilesh Pandey Sep 30 '19 at 13:47
15

Use one of the following:

Or if you feel like it, inject your own service or COM component. That would be very close to what PsExec does.

Of all these methods, I prefer task scheduler. The cleanest API of them all, I think. Connect to the remote task scheduler, create a new task for the executable, run it. Note: the executable name should be local to that machine. Not \servername\path\file.exe, but c:\path\file.exe. Delete the task if you feel like it.

All those methods require that you have administrative access to the target machine.

ProcessStartInfo is not capable of launching remote processes.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • 1
    Do Task Scheduler for preference. See this answer for an illustration: http://stackoverflow.com/questions/6178437/start-remote-process-within-the-context/8818377#8818377 – Ben Jan 11 '12 at 11:33
  • Another option that can be useful if the target machine is a SQL server, is xp_CMDShell – Robin Bennett Sep 25 '13 at 14:47
7

According to MSDN, a Process object only allows access to remote processes not the ability to start or stop remote processes. So to answer your question with respect to using this class, you can't.

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
4

An example with WMI and other credentials as the current process, on default it used the same user as the process runs.

var hostname = "server"; //hostname or a IpAddress

var connection = new ConnectionOptions();
//The '.\' is for a local user on the remote machine
//Or 'mydomain\user' for a domain user
connection.Username = @".\Administrator";
connection.Password = "passwordOfAdministrator";

object[] theProcessToRun = { "YourFileHere" }; //for example notepad.exe

var wmiScope = new ManagementScope($@"\\{hostname}\root\cimv2", connection);
wmiScope.Connect();
using (var managementClass = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions()))
{
    managementClass.InvokeMethod("Create", theProcessToRun);
}
Lucius
  • 2,794
  • 4
  • 20
  • 42
live2
  • 3,771
  • 2
  • 37
  • 46
-5

I don't believe you can start a process through a UNC path directly; that is, if System.Process uses the windows comspec to launch the application... how about you test this theory by mapping a drive to "\someComputer\somePath", then changing your creation of the ProcessStartInfo to that? If it works that way, then you may want to consider temporarily mapping a drive programmatically, launch your app, then remove the mapping (much like pushd/popd works from a command window).

F3.
  • 19
  • 3