Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling

Status
Not open for further replies.

sn0rg

IS-IT--Management
Aug 5, 2005
95
GB
I've got some code to connect to remote machines via WMI. I need to build in some error handling if the machine is down/unavailable etc.

The problem is that the "if err.number" part of the code is not being executed when an error occures, the script just trundles on. If I remove "on error resume next" then I just get the error message. Can anyone help?

Code:
on error resume next
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComp1 & "\root\cimv2")
	If Err.Number <> 0 then
	wscript.echo Err.Number
	wscript.quit

The error I'm expecting is this:
Script: c:\blah\somscript.vbs
Line: 106
Char: 1
Error: The remote server machine does not exist or is unavailable: 'GetObject'
Code: 800A01CE
Source; microsoft VBScript runtime error

If I change the "if err.number" line to:
Code:
if err.number = 800A01CE then

I just get an error about "expected 'then'" statement on that line.

Help!
 
>[tt]if err.number = 800A01CE then
if err.number = [red]&h[/red]800A01CE then
[/tt]
 
I also find it useful to ping the remote host in the script before trying to run a WMI as the timeout is lower for ping.
 
Thanks tsuji, but that didn't work either. However, I managed to trap the err.number, and echo it out. It actually comes out as 462, thus I've changed the code (see below) and it works now. Thanks for your help.

Code:
on error resume next
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComp1 & "\root\cimv2")
    If Err.Number = 462 then
    wscript.echo "Machine is down"
    wscript.quit


 
>that didn't work either.
>>if err.number = &h800A01CE then
That works to check &h800a01ce nothing more.
 
I couldn't make it work with:
if err.number = &h800A01CE then

It would/will only work as:
if err.number = 462 then

I can post the whole script if you like? Maybe its a result of earlier script actions.
 
>I couldn't make it work with:
>if err.number = &h800A01CE then
That is a confusion of reasoning.

You original line
>if err.number = 800A01CE then
is [red]wrong[/red] okay, pure and simple. I correct it for you to see how a good line of vbs is.
 
tsuji, I understand that the original line was wrong, thanks for pointing it out. I was saying that the addition of "&h[/color red]" in front of the error number did not work to catch that error, though I don't understand why not.

Perhaps the err.number property is case sensitive, and using upper case caused the error number to be missed? I don't know.

Either way, using "if err.number = 462" works.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top