I have the following code where I am trying to gather rich information about processes in Windows Server 2003. My problem occurs when I’m trying to pull back the process owner for a process which finishes between the initial WMI query and the call to get the owner. If the process has finished in the mean time then an error is raised and the script stops. I’d rather just check if the process is not available and not output to the console if it isn’t. I’ve looked at the reference and there doesn’t seem to be an appropriate method for this. Any help much appreciated.
The error raised is…
getProcessInfo2.vbs(42, 5) SWbemObjectEx: Not found
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"_
& strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_Process",,48)
For Each objItem in colItems
'Calculated Fields
sngProcessTime = ( CSng(objItem.KernelModeTime) + CSng(objItem.UserModeTime)) / 10000000
Wscript.Echo
Wscript.Echo "ProcessId : " & objItem.ProcessId
Wscript.Echo "CommandLine : " & objItem.CommandLine
Wscript.Echo "CreationDate : " & objItem.CreationDate
Wscript.Echo "HandleCount : " & objItem.HandleCount
Wscript.Echo "Name : " & objItem.Name
Wscript.Echo "PageFaults : " & objItem.PageFaults
Wscript.Echo "PageFileUsage : " & objItem.PageFileUsage
Wscript.Echo "ParentProcessId : " & objItem.ParentProcessId
Wscript.Echo "PeakPageFileUsage : " & objItem.PeakPageFileUsage
Wscript.Echo "PeakVirtualSize : " & objItem.PeakVirtualSize
Wscript.Echo "PeakWorkingSetSize : " & objItem.PeakWorkingSetSize
Wscript.Echo "Priority : " & objItem.Priority
Wscript.Echo "QuotaNonPagedPoolUsage : " & objItem.QuotaNonPagedPoolUsage
Wscript.Echo "QuotaPagedPoolUsage : " & objItem.QuotaPagedPoolUsage
Wscript.Echo "QuotaPeakNonPagedPoolUsage : " & objItem.QuotaPeakNonPagedPoolUsage
Wscript.Echo "QuotaPeakPagedPoolUsage : " & objItem.QuotaPeakPagedPoolUsage
Wscript.Echo "ReadOperationCount : " & objItem.ReadOperationCount
Wscript.Echo "ReadTransferCount : " & objItem.ReadTransferCount
Wscript.Echo "ThreadCount : " & objItem.ThreadCount
Wscript.Echo "VirtualSize : " & objItem.VirtualSize
Wscript.Echo "WriteOperationCount : " & objItem.WriteOperationCount
Wscript.Echo "WriteTransferCount : " & objItem.WriteTransferCount
Wscript.Echo "CPUTime : " & sngProcessTime
Dim Return
Return = objItem.GetOwner(strNameOfUser)
If Return <> 0 Then strNameOfUser = "unavailable"
Wscript.Echo "Owner : " & strNameOfUser
Next
You just need to check whether your collection item still contains a valid WMI object reference. If the WMI object no longer exists, your reference will become null.