Good morning,

I have the following script (feel free to improve/comments so I can learn).
My problem is that it uses a text file to give it a series of computername to
run the script against. The issue is that when a computer is turned off, the
script returns an error. I would like to trap that error and then move on to
the next computer in the text file. Could someone guide me as to why my
error handler does not work.

'*********************VBS Code starts here*******************
On Error GoTo ErrHandler

ForReading = 1
strNewFile = "PCListing01.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strNewFile, ForReading)
Do Until objFile.AtEndOfStream
strComputer = objFile.ReadLine
'Set objOption = Document.createElement("OPTION")
'objOption.Text = strLine
'objOption.value = strLine
'CmptName.add(objOption)


Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery("Select * from
Win32_OperatingSystem")
For Each objOS in colOperatingSystems
dtmBootup = objOS.LastBootUpTime
dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now())
strHTML = strHTML & strComputer & " has been up for " &
dtmSystemUptime & " hrs" & vbcrlf

'Wscript.Echo strComputer
'Wscript.Echo dtmBootup
'Wscript.Echo dtmLastBootupTime
'Wscript.Echo dtmSystemUptime
Next

Loop
objFile.Close

Wscript.Echo strHTML

Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & Mid(dtmBootup,
7, 2) & "/" & Left(dtmBootup, 4) _
& " " & Mid (dtmBootup, 9, 2) & ":" & Mid(dtmBootup, 11, 2) & ":" &
Mid(dtmBootup, 13, 2))
End Function


ErrHandler: ' this is a named range called ErrHandler
' If there was an error raised by the function we would wind up in here 'so
display a message to the user to 'show what went wrong
if err.number=0x80041003 then
strHTML = strHTML & "Unable to reach " & strComputer
Next ObjFile
Resume Next
else
'MsgBox "An error happened here!" & vbCrLf & _
'"Error number: " & Err.Number & vbCrLf & _
'"Error Description" & Err.Description & vbCrLf & _
'"Error Help Context ID" & Err.HelpContext & vbCrLf & _
'"Error Help File" & Err.HelpFile, vbCritical, "ERROR MESSAGE"

'Err.Clear ' destroy the error andâ?¦
' â?¦ this resumes in the last known position.
' in this case, since its not more than a file opening occurring,
' it will simply exit the function for you.
Resume Next
end if
'*********************VBS Code ends here*******************
Thanks,

Daniel

Re: Error Handling by Bob

Bob
Tue Jul 19 06:52:35 CDT 2005

Daniel wrote:
> Good morning,
>
> I have the following script (feel free to improve/comments so I can
> learn). My problem is that it uses a text file to give it a series of
> computername to run the script against. The issue is that when a
> computer is turned off, the script returns an error. I would like to
> trap that error and then move on to the next computer in the text
> file. Could someone guide me as to why my error handler does not
> work.
>
> '*********************VBS Code starts here*******************
> On Error GoTo ErrHandler

VBScript does not support this. You can get the vbscript documentation at:
http://www.microsoft.com/downloads/details.aspx?FamilyID=01592c48-207d-4be1-8a76-1c4099d7bbb9&DisplayLang=en

Make sure you look up the article about VB features that aren't supported in
vbscript.

The only two error-handling statements supported by vbscript are:

On Error Resume Next - turns error-handling on
On Error GoTo 0 - turns error-handling off

It looks like you've tried to adapt some VB/VBA code, where this statement
would work. In vbscript you have to do something lke:

on error resume next
StatementThatCouldCauseError
if err <> 0 then
'do something to handle the error
end if
NextStatementThatCouldCauseError
if err <> 0 then
'do something to handle the error
end if
on error goto 0

That "'do something to handle the error" could be a call to a sub that
accepts the Error (Err) object as an argument.

HTH,
Bob Barrows
PS. Eric Lippert wrote a pretty good series about vbscript error handling in
his blog:
http://blogs.msdn.com/ericlippert/archive/2004/08/19/217244.aspx
http://blogs.msdn.com/ericlippert/archive/2004/08/23/218974.aspx
http://blogs.msdn.com/ericlippert/archive/2004/08/25/220373.aspx


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: Error Handling by Richard

Richard
Tue Jul 19 07:42:49 CDT 2005

Hi,

To add, I find that scripts using WMI can raise an error at two places.
First, when you bind to objWMIService if the computer is not available.
Second, the "Set colOperatingSystems" statement can fail if the machine does
not have WMI, or you don't have rights, or the machine doesn't support the
WMI class. Also, I find that text files often have blank lines, especially
at the end, so I check that the name is not blank. I would suggest something
similar to:

Do Until objFile.AtEndOfStream
strComputer = Trim(objFile.ReadLine)
If (strComputer <> "") Then
On Error Resume Next
Set objWMIService = GetObject("winmgmts:...
Set colOperatingSystems = objWMIService....
If (Err.Number <> 0) Then
On Error GoTo 0
strHTML = strHTML & strComputer _
& " not available or does not support WMI"
Else
On Error GoTo 0
For Each objOS In colOperatingSystems
....
Next
End If
End If
Loop

I try to turn off normal error handling only where I anticipate an error and
plan to handle it. Then I restore normal error handling. You can test for
error conditions with the Err built in object. In the above I trap errors
only on the two statements where I anticipate errors.

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab web site - http://www.rlmueller.net
--
"Daniel" <Daniel@discussions.microsoft.com> wrote in message
news:7C5EBCEC-7849-4686-A182-ADB0E8B395DB@microsoft.com...
> Good morning,
>
> I have the following script (feel free to improve/comments so I can
learn).
> My problem is that it uses a text file to give it a series of computername
to
> run the script against. The issue is that when a computer is turned off,
the
> script returns an error. I would like to trap that error and then move on
to
> the next computer in the text file. Could someone guide me as to why my
> error handler does not work.
>
> '*********************VBS Code starts here*******************
> On Error GoTo ErrHandler
>
> ForReading = 1
> strNewFile = "PCListing01.txt"
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.OpenTextFile(strNewFile, ForReading)
> Do Until objFile.AtEndOfStream
> strComputer = objFile.ReadLine
> 'Set objOption = Document.createElement("OPTION")
> 'objOption.Text = strLine
> 'objOption.value = strLine
> 'CmptName.add(objOption)
>
>
> Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
> Set colOperatingSystems = objWMIService.ExecQuery("Select * from
> Win32_OperatingSystem")
> For Each objOS in colOperatingSystems
> dtmBootup = objOS.LastBootUpTime
> dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
> dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now())
> strHTML = strHTML & strComputer & " has been up for " &
> dtmSystemUptime & " hrs" & vbcrlf
>
> 'Wscript.Echo strComputer
> 'Wscript.Echo dtmBootup
> 'Wscript.Echo dtmLastBootupTime
> 'Wscript.Echo dtmSystemUptime
> Next
>
> Loop
> objFile.Close
>
> Wscript.Echo strHTML
>
> Function WMIDateStringToDate(dtmBootup)
> WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" &
Mid(dtmBootup,
> 7, 2) & "/" & Left(dtmBootup, 4) _
> & " " & Mid (dtmBootup, 9, 2) & ":" & Mid(dtmBootup, 11, 2) & ":"
&
> Mid(dtmBootup, 13, 2))
> End Function
>
>
> ErrHandler: ' this is a named range called ErrHandler
> ' If there was an error raised by the function we would wind up in here
'so
> display a message to the user to 'show what went wrong
> if err.number=0x80041003 then
> strHTML = strHTML & "Unable to reach " & strComputer
> Next ObjFile
> Resume Next
> else
> 'MsgBox "An error happened here!" & vbCrLf & _
> '"Error number: " & Err.Number & vbCrLf & _
> '"Error Description" & Err.Description & vbCrLf & _
> '"Error Help Context ID" & Err.HelpContext & vbCrLf & _
> '"Error Help File" & Err.HelpFile, vbCritical, "ERROR MESSAGE"
>
> 'Err.Clear ' destroy the error and?
> ' ? this resumes in the last known position.
> ' in this case, since its not more than a file opening occurring,
> ' it will simply exit the function for you.
> Resume Next
> end if
> '*********************VBS Code ends here*******************
> Thanks,
>
> Daniel