Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trap Set or GetObject failure

50 views
Skip to first unread message

Dan Cassidy

unread,
May 2, 2003, 1:04:39 PM5/2/03
to
Hello World,

I am trying to trap an error and I don't exactly know how
to do it. I have a script where I ask the user to enter
the computer name he is looking for information on. If the
computer is not available or doesn't exist in the domain,
the script exits and that's it. I want to handle the
error, rather then just letting the script die
unexpectedly. The following is the line I am having
problems with:

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer
& "\root\cimv2")

If objWMIService can't be set because GetObject fails,
what do I do? How do I check for the error? I saw the
posts by Steve Fulton earlier in the newsgroup, but those
links don't deal with error handling. Any help will be
rewarded with a hearty "Atta Boy!" Thanks in advance

D. Cassidy

Torgeir Bakken (MVP)

unread,
May 2, 2003, 1:26:44 PM5/2/03
to
Dan Cassidy wrote:

> I am trying to trap an error and I don't exactly know how
> to do it. I have a script where I ask the user to enter
> the computer name he is looking for information on. If the
> computer is not available or doesn't exist in the domain,
> the script exits and that's it. I want to handle the
> error, rather then just letting the script die
> unexpectedly. The following is the line I am having
> problems with:
>
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" & strComputer
> & "\root\cimv2")
>
> If objWMIService can't be set because GetObject fails,
> what do I do? How do I check for the error?

Hi

This is how I would have done it (you should ping first, because the WMI
connection time-out is *long* if the computer is offline):

strComputer = "something"

' ping the computer to see if it is online
If IsConnectible(strComputer, "", "") Then

' error handling the connection to strComputer
On Error Resume Next


Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

If Err.Number = 0 Then
On Error Goto 0

' Do your WMI handling here

Else
WScript.Echo sCompName & " is online but not available"
End If
Else
WScript.Echo sCompName & " is not online"
End If


Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' Works an "all" WSH versions
' sHost is a hostname or IP

' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used

If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750

Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "\runresult.tmp"

oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True

Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsASCII)

sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)

Select Case InStr(sResults,"TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select
End Function


--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter


Richard Mueller

unread,
May 2, 2003, 1:36:14 PM5/2/03
to
Hi,

The "On Error Resume Next" statement turns off normal
error handling, while "On Error GoTo 0" restores it. The
built-in Err object can be used to deal with errors:

On Error Resume Next
Err.Clear


Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" _


& strComputer & "\root\cimv2")

If Err.Number <> 0 Then
Wscript.Echo "Error number: " & Err.Number
Wscript.Echo "Description: " & Err.Description
Err.Clear
End If
On Error GoTo 0

Richard
http://www.rlmueller.net

>.
>

Dan Cassidy

unread,
May 2, 2003, 2:58:02 PM5/2/03
to
Awesome. That works great! Thanks for your help.

>.
>

0 new messages