none
Background vbscript that waits for a process to start, terminates the process and exits when it sees a patricular process start

    Question

  • I' m trying to come up with some scripts to do an unattended install of Windows XP via runonceex/iernonce.  I got this to work but I run into to problems when the programs I install repeatedly start a browser session with IE or Chrome.

    When this happens, I have to manually terminate the process to prevent blocking of msiexec or which ever installer the program uses.

    What I would like to do via non-gui vbscript is to:
    1). Execute a background script that waits for a call to a  browser process to start,
         and immediately termininate the browser process.

    2). The VB script terminates itself once it sees a particular process starts.  In my case it would be SoftPerfect NetWorx startup.  At this point I know all of the programs installers have completed.  Basiclly, at this point, XP has logged in as Administrator via WINNT.SIF, the explorer shell is running, all of the core MS programs have been installed,  and all of the programs I want are now install.  I would watch for a windows process, but since login has run and explorer shell is running, but I not sure at this point during the end phase to reliably watch for.

    3. I would like to accomplish this without chewing up processor resources or creating memory leaks.

    Here is what I have so far, the msgboxes are for trouble shooting-

    Option Explicit
    On Error Resume Next
    dim strcomputer
    dim objwmiservice
    dim colmonitoredprocesses
    dim datstartdate
    dim objlatestprocess

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colMonitoredProcesses = objWMIService. _      
        ExecNotificationQuery("select * from __instancecreationevent " _
            & " within 1 where TargetInstance isa 'Win32_Process'")

    datStartdate = Now()


    'msgbox "start of monitor"

    Do While cint(DateDiff("s",datStartdate,Now())) < 300
        Set objLatestProcess = colMonitoredProcesses.NextEvent
        Wscript.Sleep 6000 'here we go again, yawn, yawn
        If objLatestProcess.TargetInstance.Name = "IEXPLORE.EXE" Then
            WScript.Sleep 6000 ' yes i know i should be shot
            objLatestProcess.TargetInstance.Terminate
            Exit Do
        End If
    Loop

    'msgbox "out of loop"
    Set objLatestProcess = Nothing
    Set colMonitoredProcesses = Nothing
    Set objWMIService = Nothing

    Wscript.Quit

    Thanks,
    mhmallory
    Friday, October 23, 2009 8:08 PM

Answers

  • First use Win32_Process to terminate all iexplore.exe instances and after that monitor iexplore.exe and other process creation (I used notepad.exe in the script). If an explore.exe instance is created terminate it and if the other process instance is created terminate the script:

    Option Explicit
    
    'On Error Resume Next
    
    Dim strComputer
    Dim objWmiService
    Dim colMonitoredProcesses
    Dim objLatestProcess
    
    Dim colProcesses
    Dim objProcess
    
    strComputer = "."
    
    Set objWmiService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\cimv2")
        
    Set colProcesses = objWmiService.ExecQuery _
        ("Select * From Win32_Process Where Name = 'iexplore.exe'")
        
    For Each objProcess In colProcesses
        objProcess.Terminate
    Next
    
    Set colMonitoredProcesses = objWmiService.ExecNotificationQuery _
        ("Select * From __InstanceCreationEvent " _
        & "Within 1 Where TargetInstance Isa 'Win32_Process' " _
        & "And (TargetInstance.Name = 'iexplore.exe' " _
        & "Or TargetInstance.Name = 'notepad.exe')")
    
    Do 
        
        Set objLatestProcess = _
        colMonitoredProcesses.NextEvent.TargetInstance
        
        If LCase(objLatestProcess.Name) = "iexplore.exe" Then
            objLatestProcess.Terminate
        Else
            WScript.Quit
        End If
        
    Loop
    


    You don't need WScript.Sleep, colMonitoredProcesses.NextEvent will block the script until an event is received.


    Uros Calakovic
    Saturday, October 24, 2009 8:14 AM
    Moderator
  • Thanks, will give it a shot.  Takes a little of time to test because I test Windows Install on a virtual machine.

    mhmallory
    • Marked as answer by mhmallory Sunday, October 25, 2009 2:50 AM
    Saturday, October 24, 2009 3:16 PM

All replies

  • First use Win32_Process to terminate all iexplore.exe instances and after that monitor iexplore.exe and other process creation (I used notepad.exe in the script). If an explore.exe instance is created terminate it and if the other process instance is created terminate the script:

    Option Explicit
    
    'On Error Resume Next
    
    Dim strComputer
    Dim objWmiService
    Dim colMonitoredProcesses
    Dim objLatestProcess
    
    Dim colProcesses
    Dim objProcess
    
    strComputer = "."
    
    Set objWmiService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\cimv2")
        
    Set colProcesses = objWmiService.ExecQuery _
        ("Select * From Win32_Process Where Name = 'iexplore.exe'")
        
    For Each objProcess In colProcesses
        objProcess.Terminate
    Next
    
    Set colMonitoredProcesses = objWmiService.ExecNotificationQuery _
        ("Select * From __InstanceCreationEvent " _
        & "Within 1 Where TargetInstance Isa 'Win32_Process' " _
        & "And (TargetInstance.Name = 'iexplore.exe' " _
        & "Or TargetInstance.Name = 'notepad.exe')")
    
    Do 
        
        Set objLatestProcess = _
        colMonitoredProcesses.NextEvent.TargetInstance
        
        If LCase(objLatestProcess.Name) = "iexplore.exe" Then
            objLatestProcess.Terminate
        Else
            WScript.Quit
        End If
        
    Loop
    


    You don't need WScript.Sleep, colMonitoredProcesses.NextEvent will block the script until an event is received.


    Uros Calakovic
    Saturday, October 24, 2009 8:14 AM
    Moderator
  • Thanks, will give it a shot.  Takes a little of time to test because I test Windows Install on a virtual machine.

    mhmallory
    • Marked as answer by mhmallory Sunday, October 25, 2009 2:50 AM
    Saturday, October 24, 2009 3:16 PM