RE: Error handling in a Do Loop
- From: Umesh Thakur <UmeshThakur@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 30 Aug 2005 12:55:04 -0700
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
>
.
- Follow-Ups:
- RE: Error handling in a Do Loop
- From: Wmi.Query
- RE: Error handling in a Do Loop
- References:
- Error handling in a Do Loop
- From: Wmi.Query
- Error handling in a Do Loop
- Prev by Date: Re: Schedule vbscript everyday
- Next by Date: compare files with text document
- Previous by thread: Error handling in a Do Loop
- Next by thread: RE: Error handling in a Do Loop
- Index(es):
Relevant Pages
|