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

Catching Errors.

2 views
Skip to first unread message

anonymous

unread,
Jan 9, 2004, 2:46:06 PM1/9/04
to
Does anyone have examples or links to examples on how to
trap errors that can come from the following statement?

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

I'd like to trap the error when the remote computer is
not responing.

Thanks.

Torgeir Bakken (MVP)

unread,
Jan 9, 2004, 3:06:34 PM1/9/04
to
anonymous wrote:

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


Greg Martin

unread,
Jan 10, 2004, 2:43:53 AM1/10/04
to
To simplify - search for a commandline tool called alive.exe. It will ping
a single time and exit setting the errorlevel on the way out...

\\Greg


"Torgeir Bakken (MVP)" <Torgeir.B...@hydro.com> wrote in message
news:3FFF09CA...@hydro.com...

0 new messages