RE: Error handling in a Do Loop

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



In your error handling code, use following line to clear the error info stored
in ERR object:

err.clear

In your code, you are printing error desc in the loop...
Ideally, what you should do is: comment the "on error resume next" statement.
- execute the script
- find out the line of code in script that is producing error messages
- put error handling code below that (make sure u uncomment "on error resume
next")
probably, u want to continue processing the loop when error occurs
- after the line of error handling, put the line: ERR.CLEAR
this will flush the ERR object.

before the line:

Do While Not objExecObject.StdOut.AtEndOfStream

put:

strText=""

So, if last computer had successfull ping, strText would contain echo
replies. If you make
this string empty, it will process correctly for next computer.

Regards,
Umesh
When you are unable to keep your eyes open, do go and sleep for few hours!!!


"Wmi.Query" wrote:

> I'm trying to put together a script that iterates through all of the computer
> objects in Active Directory, pings them, and if they're up, pulls some
> information from them. I started by using a template called "Select and Ping
> all computers in a domain", available from the Microsoft Script Center
> (http://www.microsoft.com/technet/scriptcenter/scripts/templates/wmi/basic/tmwbvb26.mspx).
> What I'm running into is that I'm having a problem with a few computers
> (maybe they're not on the domain, or maybe something's wrong with them,
> etc... it doesn't matter). Using "On error resume next", at the beginning of
> the script lets me keep working; but the problem I have is that inaccurate
> information is getting captured for the problem computers.
>
> For example, I have a computer called "ProblemPC". The script runs along
> fine until the do-loop hits this machine... instead of getting an error code
> of "is not accessible..." when I echo the OperatingSystem or the
> SerialNumber, I get the results of the computer immediatly preceeding it.
>
> I've tried adding "Echo Err.Description" into the do-loop, which works a
> little bit better when a hit a problem workstation. But the Err.Description
> never gets cleared... it seems as though I can't say "Set Err.Description =
> Nothing". I've also tried resetting my variables at the begging of the "Do
> Until objRecordSet.EOF" such that "strComputer = " "", as well as "Set
> strComputer=Nothing" (as I'm now doing in the below), but I still get the
> information from the previous computer.
>
> Thanks for your help. The code is copied below.
>
>
> ---
> On error resume next
> Option Explicit
> Const ADS_SCOPE_SUBTREE = 2
>
> Set objConnection = CreateObject("ADODB.Connection")
> Set objCommand = CreateObject("ADODB.Command")
> objConnection.Provider = "ADsDSOObject"
> objConnection.Open "Active Directory Provider"
>
> Set objCOmmand.ActiveConnection = objConnection
> objCommand.CommandText = _
> "Select Name, Location from 'LDAP://DC=domain,DC=local' " _
> & "Where objectClass='computer'"
> objCommand.Properties("Page Size") = 1000
> objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
> Set objRecordSet = objCommand.Execute
> objRecordSet.MoveFirst
>
> Do Until objRecordSet.EOF
>
> set strComputer = Nothing
> Set strOS = Nothing
> Set strDellTag = Nothing
>
> ' Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
> strComputer = objRecordSet.Fields("Name").Value
>
> Set objShell = CreateObject("WScript.Shell")
> strCommand = "%comspec% /c ping -n 3 -w 1000 " & strComputer & ""
> Set objExecObject = objShell.Exec(strCommand)
>
> Do While Not objExecObject.StdOut.AtEndOfStream
> strText = objExecObject.StdOut.ReadAll()
> If Instr(strText, "Reply") > 0 Then
>
> Set objWMIService = GetObject _
> ("winmgmts:\\" & strComputer & "\root\cimv2")
> Wscript.Echo Err.Description
>
> Set colItems = objWMIService.ExecQuery _
> ("Select * From Win32_OperatingSystem")
> Set colItems2 = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
>
> For Each objItem in ColItems
> strOS = objItem.Caption
> Wscript.Echo "Computer Name: " & strComputer
> Wscript.Echo "Operating System: " & strOS
> Next
> For Each objItem in colItems2
> strDellTag = objItem.SerialNumber
> Wscript.Echo "Dell Service Tag: " & strDellTag
> Next
> Wscript.Echo " "
>
> Else
> Wscript.Echo strComputer & " could not be reached."
> Wscript.Echo " "
> End If
> Loop
> objRecordSet.MoveNext
> Loop
>
.



Relevant Pages

  • RE: Error handling in a Do Loop
    ... When the loop hits a machine which produces an error description, ... On the very next loop, I get a ping reply, but I can't pull any information, ... > In your error handling code, use following line to clear the error info stored ... > - find out the line of code in script that is producing error messages ...
    (microsoft.public.windows.server.scripting)
  • Re: Error handling problem.
    ... The script appears to be working fine, ... >a problem with the error handling. ... The Err object is implemented in scope-specific manner. ... will implicitly clear (reset) any error settings. ...
    (microsoft.public.windows.server.scripting)
  • Re: Help with values for Error Trapping
    ... The point of a script like VBS is to use many different ... The Err object is an intrinsic global object available under all hosts. ... is error-trapping status in effect at any level in the stack. ...
    (microsoft.public.scripting.vbscript)
  • Re: Help with values for Error Trapping
    ... The point of a script like VBS is to use many different ... >The Err object is an intrinsic global object available under all hosts. ... >is error-trapping status in effect at any level in the stack. ...
    (microsoft.public.scripting.vbscript)
  • Re: Questions about GetRef() and ASP, etc.
    ... > stack does NOT extend into a called procedure's scope. ... > script requires catch-all error handling, however, it is often best to set ... > unexposed functions, mix VBS and JS, etc. WSCs are fairly well documented ... > The Err object appears to be misdocumented in the MS VBS documentation. ...
    (microsoft.public.scripting.vbscript)