5

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!

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

3 Comments

Note: You may have to modify WMI security settings to allow specifyed users to run the commands (see msdn.microsoft.com/en-us/library/windows/desktop/…)
Does this work on Windows XP/2003? It seems from msdn.microsoft.com/en-us/library/windows/desktop/… that ExitCode is not available on those OSs.
I'm querying for ExitStatus property in Win32_ProcessStopTrace, so, indeed, it's likely not on those OSs. Using psexec may be a reasonnable alternative depending on your needs (Actually that's what I finally used to avoid changing WMI security settings).
0

Here is an example create on the top of .NET objects but written in Powershell, it's easy to translate it to C#

Clear-Host # Authentication object $ConOptions = New-Object System.Management.ConnectionOptions $ConOptions.Username = "socite\administrateur" $ConOptions.Password = "adm" $ConOptions.EnablePrivileges = $true $ConOptions.Impersonation = "Impersonate" $ConOptions.Authentication = "Default" $scope = New-Object System.Management.ManagementScope("\\192.168.183.220\root\cimV2", $ConOptions) $ObjectGetOptions = New-Object System.Management.ObjectGetOptions($null, [System.TimeSpan]::MaxValue, $true) # Equivalent to local : # $proc = [wmiclass]"\\.\ROOT\CIMV2:Win32_Process" $proc = New-Object System.Management.ManagementClass($scope, "\\192.168.183.220\ROOT\CIMV2:Win32_Process", $ObjectGetOptions) # Now create the process remotly $res = $proc.Create("cmd.exe") # Now create an event to detect remote death $timespan = New-Object System.TimeSpan(0, 0, 1) $querryString = "SELECT * From WIN32_ProcessStopTrace WHERE ProcessID=$($res.ProcessID)" $query = New-Object System.Management.WQLEventQuery ($querryString) $watcher = New-Object System.Management.ManagementEventWatcher($scope, $query) $b = $watcher.WaitForNextEvent() $b 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.