?
Solved

Need to get result of WMI query for error handling

Posted on 2011-03-03
3
Medium Priority
?
891 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
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 …
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

777 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