Solved

Need to get result of WMI query for error handling

Posted on 2011-03-03
3
890 Views
Last Modified: 2012-05-11
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
Comment
Question by:advcom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 35033952
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
 

Author Comment

by:advcom
ID: 35033968
That did it, thanks
0
 
LVL 13

Expert Comment

by:connectex
ID: 35033988
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question