We help IT Professionals succeed at work.
Get Started

VBScript and WMI Error Trapping

Last Modified: 2013-11-08
Just a quick question.
I use VBScript and WMI to perform tasks on remote machines but am a bit confused about error trapping.
Suppose I've enumerated instances of a particular Win32_Service and now I want to start one with
intReturnCode = objService.StartService()
Do I need to be looking at both the Err object and the value of the return code to see if there has been an error or just one of these?

If Err.Number <> 0 Then
   WScript.Echo "Error number " & Err.Number & " - " & Err.Description
ElseIf intReturnCode <> 0 Then
   WScript.Echo "Error code " & intReturnCode
   WScript.Echo "All was OK"
End If

Open in new window

Obviously this makes code very long if every single thing that might go wrong has to be trapped in two different ways...??

Watch Question
Most Valuable Expert 2012
Top Expert 2014
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE