• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 902
  • Last Modified:

Need to get result of WMI query for error handling

I have vbscript that uses WMI to enumerate all 32 and 64 bit processes. Then it takes the results and does another WMI query to get the loaded DLL's. It works most of the time, but often gets an automation error saying "unknown error". I realized that it is getting the list of processes and while it is queying for loaded drivers, a  process stops and so when the script tries to get the loaded DLL's of a process that no longer exists, I get an error when I try to use the results of the query. Obviously I need to check to see if I have a valid object before I use it, but it seems that nothing I do works to see if it is valid. The code is posted below. Here are the functions I have tried.
IsNull
IsObject
IsEmpty
Is Nothing
obj.Count
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Debug)}!\\" & "." & "\root\cimv2")

processPid = 6000 'ANY INVALID PID

strAssocQuery = "ASSOCIATORS OF {Win32_Process.Handle='" & processPid & "'} WHERE ResultClass=CIM_DataFile"
Set colModule = objWMIService.ExecQuery(strAssocQuery, , 48)

'NEED WAY TO MAKE SURE THAT colModule is valid

For Each module In colModule 'GIVES ERROR HERE WHEN INVALID PID
     msgbox module.Name      
Next

Open in new window

0
advcom
Asked:
advcom
1 Solution
 
RobSampsonCommented:
Maybe just this:

strError = ""
On Error Resume Next
For Each module In colModule
     If Err.Number = 0 Then
          msgbox module.Name
     Else
          strError = Err.Number & ": " & Err.Description
          Err.Clear
     End If
Next
If strError <> "" Then MsgBox strError

Open in new window


Regards,

Rob.
0
 
advcomAuthor Commented:
That did it, thanks
0
 
connectexCommented:
Add this code:

processValid = false
Set colProcessList = objWMIService.ExecQuery ("SELECT * FROM Win32_Process WHERE ProcessID = ' & processPID & "'")
For Each objProcess in colProcessList
  processFound = true
Next

Now if process is found it's still active or valid and you should be able to query it.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now