Re: Error Handling

Tech-Archive recommends: Fix windows errors by optimizing your registry



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@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:7C5EBCEC-7849-4686-A182-ADB0E8B395DB@xxxxxxxxxxxxxxxx
> 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


.


Quantcast