How can you run a process on another computer in .

2019-08-04 20:09发布

问题:

Let's say I have a windows service called "MyService" and an executable called "MyEXE" located on several computers on my network.

Is it possible (from within "MyService") to start several instances of "MyEXE" on a different/same computer, have it do some task and return a true/false result to a callback method in "MyService"?

Something like this

class MyService : ServiceBase
{
    delegate void UpdateCallBack(int id, bool updated)
    void CheckForUpdates()
    {
        bool updatesExist = someService.GetCurrentVersion() != currentVersion;
        if(updatesExist)
        {
            UpdatePC("C:\Program Files\MyApp.exe", 1, UpdateComplete);
            UpdatePC("\\somepc\Program Files\MyApp.exe", 1, UpdateComplete);
            UpdatePC("\\mypc\Program Files\MyApp.exe", 1, UpdateComplete);
        }
    }

    void UpdatePC(string filePath, int id, UpdateCallBack callback)
    {
       //This is where I am kind of lost 
       SomeEXERunner runner = new SomeEXERunner();
       runner.Run(filePath,"-u",id,callback);
    }

    void UpdateComplete(int id, bool updated)
    {
       //do something
       if(!updated)
          EmailService.NotifyAdmin("PC Not updated", id);
    }
}

Maybe I'm getting the whole architecture wrong!

回答1:

You could use PSexec as Ian stated, but I think WMI is a better way try this for an example http://www.codeproject.com/KB/cs/EverythingInWmi02.aspx



回答2:

One thing you can do is run the PsExec tool (http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) from Sysinternals in a separate process, pipe the result to a file, then parse it to figure out success/failure.



回答3:

One solution is to use psexec, the other is to... do what psexec does:

  • embed a service exe as a resource into your app
  • copy the resource into \\remotesystem\admin$
  • use SCM to start a this exe as a service on \\remotesystem
  • connect ask the service to do the work
  • uninstall the service from SCM


回答4:

Also see Task Manager API. You schedule a task to run once now+1 sec, etc. It's COM-based, so quite .NET-friendly.

Unlike psexec, this does not install anything on the target machine.