views:

494

answers:

4

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

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

Something linke 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!

+9  A: 

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.

Ian Jacobs
+1, PS Exec is the way to run an exe on a remote machine. Whoever gave the downvote please can you say why, is there something wrong with PS Exec? Downvotes with no comment are pointless.
Steve Haigh
+1  A: 

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

Alex
A: 

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

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.

Seva Alekseyev