locked
WMI create remote process and get exit code from the completed process RRS feed

  • Question

  • Hi,

    I have been trying to find vbscript examples of the below to no avail. Hoping someone can help in pointing me in the right direction or even a link to an eample.

    I want to use WMI to create a process on a remote machine. No problem here. The problem is that I want to wait or loop until the remote process has completed and then retrieve the exit code from the process itself (Not the exit code from the create process - which most examples refer to)

    Any help will be much appreciated

    Thanks

    Mike

    Wednesday, September 1, 2010 11:13 PM

Answers

  • I've never seen code that retrieves an exit code from the started process. The method I use is to copy the program to be run to the remote computer, start it (create the process), then attempt to delete the program in a loop. This will fail until the program completes. However, no error information is retrieved. For example, the following describes and links a VBScript program to deploy an executable to remote computers:

    http://www.rlmueller.net/Deploy.htm

    which is based on this Microsoft example:

    http://support.microsoft.com/default.aspx?scid=kb;en-us;827227

    Richard Mueller


    MVP ADSI
    Thursday, September 2, 2010 12:12 AM

All replies

  • I've never seen code that retrieves an exit code from the started process. The method I use is to copy the program to be run to the remote computer, start it (create the process), then attempt to delete the program in a loop. This will fail until the program completes. However, no error information is retrieved. For example, the following describes and links a VBScript program to deploy an executable to remote computers:

    http://www.rlmueller.net/Deploy.htm

    which is based on this Microsoft example:

    http://support.microsoft.com/default.aspx?scid=kb;en-us;827227

    Richard Mueller


    MVP ADSI
    Thursday, September 2, 2010 12:12 AM
  • Hi,

    I have seen an example using VB .NET but I am not sure how that can be (if at all) translated to vbscript.

    http://blogs.msdn.com/b/padmanr/archive/2010/05/08/execute-a-process-on-remote-machine-wait-for-it-to-exit-and-retrieve-its-exit-code-using-wmi.aspx

     

    Thursday, September 2, 2010 1:47 AM
  • Here is the code from that blog entry converted to VBScript. Note that you won't be able to use it on Windows Server 2003, Windows XP and older because Win32_ProcessStopTrace.ExitStatus is not available (see here):

    strComputer = "Computer Name"
    strProcessName = "Notepad.exe"
    
    Set objWmiService = GetObject _
      ("winmgmts:\\" & strComputer & "\root\cimv2")
    
    Set objProcess = objWmiService.Get("Win32_Process")
    intReturnValue = objProcess.Create(strProcessName, , , strHandle)
    
    If intReturnValue = 0 Then
      Set objEventSource = objWmiService.ExecNotificationQuery _
        ("Select * From Win32_ProcessStopTrace " _
        & "Where ProcessId = " & strHandle)
      
      Set objNextEvent = objEventSource.NextEvent()
      WScript.Echo "Process " & strProcessName & _
        " exited with code " & objNextEvent.ExitStatus
    Else
      WScript.Echo "Couldn't start the process."
    End If
    
    
    

    Uros Calakovic
    Friday, October 22, 2010 1:54 PM