1
votes

I have a problem I am hoping someone can help me with. I am running a VBScript in WScript where these 2 functions work together to see when a process has closed

function closeApp(appNameStr)
    '* What ever code required to close the application here *
    Do Until isRunningProcess(appNameStr) = False
        Wscript.Sleep 250
    Loop
end function

function isRunningProcess(processNameStr)
    Dim wmiObj, processObjCol
    processNameStr = "'" & processNameStr & "'"

    Set wmiObj = GetObject("WinMgmts:{impersonationLevel=Impersonate}")
    Set processObjCol = wmiObj.ExecQuery("SELECT Name FROM Win32_Process Where Name = " & processNameStr)

    If processObjCol.Count = 0 Then
        isRunningProcess = False
    Else
        isRunningProcess = True
    End If
End function

When I run a script with these functions I end up with an error like this on processObjCol.Count

Line: 115 Char: 2 Error: Invalid query Code: 80041017 Source: SWbemObjectSet

After sending almost everything I could think to a log file, I found that it was running through the Do Until Loop fine the first time but after that is when this error would show up. I've tried setting wmiObj and processObjCol to Nothing at the end of isRunningProcess with no changes to the error. Any thoughts? Do I need to terminate the WMI Service some how?

1

1 Answers

0
votes

I'm not sure if you still need help with this or you have figured it out but you're still missing some information on your query there. Try these updates on your two-lines in the sample provided.

Function isRunningProcess(processNameStr)
    status = false
    Dim wmiObj, processObjCol
    Set wmiObj = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set processObjCol = wmiObj.ExecQuery("SELECT * FROM Win32_Process WHERE Name ='" & processNameStr & "'")
    If processObjCol.Count <> 0 Then Status = True
    isProcessRunning = status
End function