Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get an exit code from WMI spun remote process

Tags:

c#

wmi

I'm executing a process remotely via WMI (Win32_Process Create) but am unable to figure out how I can determine when the process has completed executing. When I first issue the command, there is an exit code (0 for success) but that just tells me the process has been successfully spawned.

Is there a way I can know when the process ends? Thanks!

like image 623
coergo Avatar asked Oct 09 '22 22:10

coergo


1 Answers

Faced same issue and wrote a simple VMI wrapper:

var exitStatus = WmiOperations.Run("notepad.exe", wait:10);

Synopsis for Run is:

int Run(string command, // Required
        string commandline = null, // (default=none)
        string machine = null, // (default=local)
        string domain = null, // (default=current user domain)
        string username = null, // (default=current user login)
        string password = null, // (default=current user password)
        SecureString securePassword = null, // (default=current user password)
        double wait = double.PositiveInfinity); // (default=wait til command ends);

Source code can be downloaded from here.

Give caesar his due, code is inspired from this one. Simply:

  • Refactored things to static class
  • Added more control on remoting parameters
  • Redesigned event watcher to suppress the unappealing CheckProcess test
like image 194
CitizenInsane Avatar answered Oct 18 '22 20:10

CitizenInsane