Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute process on remote machine, in C#

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

like image 305
DJPB Avatar asked Feb 26 '10 17:02

DJPB


4 Answers

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);
like image 146
Ivan G. Avatar answered Nov 11 '22 12:11

Ivan G.


Use one of the following:

  • (EDIT) Remote Powershell
  • WMI (see Ivan G's answer)
  • Task Scheduler API (http://msdn.microsoft.com/en-us/library/windows/desktop/aa383606%28v=vs.85%29.aspx)
  • PsExec
  • WshRemote object with a dummy script. Chances are, it works via DCOM, activating some of scripting objects remotely.

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.

like image 28
Seva Alekseyev Avatar answered Nov 11 '22 11:11

Seva Alekseyev


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.

like image 7
Austin Salonen Avatar answered Nov 11 '22 10:11

Austin Salonen


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);
}
like image 4
live2 Avatar answered Nov 11 '22 10:11

live2