Solved

Need to get result of WMI query for error handling

Posted on 2011-03-03
3
883 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
3 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
Comment Utility
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
Comment Utility
That did it, thanks
0
 
LVL 13

Expert Comment

by:connectex
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now