views:

736

answers:

5

Hi there

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"

+4  A: 

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

aloneguid
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
tks for your answer? By the way, do you know if SysInternals have full official support by microsoft?
DJPB
A: 

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
MMmmmmmmm no )))
aloneguid
+3  A: 

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
A: 

Try replacing C: with C$ in the filename (c$\MyAppFolder\MyApp.exe)

Jim Violette
Ever try this yourself? ;)
Mike Atlas
+3  A: 

Use one of the following:

  • WMI
  • Task Scheduler API
  • PsExec

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.

Seva Alekseyev