How can I get the exit code from a remote WMI process

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

Is there any way to know when the process will end? Thanks!

+4
source share
2 answers

Faced with the same problem and wrote a simple VMI shell:

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

Summary for Run :

 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 properly, the code is inspired by this one . Just:

  • Implemented things in a static class
  • Added additional control of remote access settings
  • Redesigned event observer to suppress unattractive CheckProcess test
+3
source

Here is an example created at 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 
0
source

All Articles