RE: Error handling in a Do Loop



I made the changes you suggested to my strings - blanking them correctly now.
Unfortunatly, I couldn't get this work work... I tried moving around the
location of the blank strings, but I continued getting the same results.

Instead of proceeding down this path further, I changed the logic around...
I built my collections first, then moved the values into strings, and finally
used an If statement to determine the error status. If there is an error, I
add the computer name where the error occured to StrErrorSystems, and and
then continued on to the next computer. At the end of the script, I print
the error line to summerize.

Thanks again for all of your help. I have copied my changes below; if you
have any further suggestion or pointers, please let me know.

Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_OperatingSystem")
Set colItems2 = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
' Wscript.Echo Err.Description

For Each objItem in ColItems
strOS = objItem.Caption
Next

For Each objItem in colItems2
strDellTag = objItem.SerialNumber
strManu = objItem.Manufacturer
Next
If Err.Number > 0 then
strErrorSystems = strComputer & ", " & strErrorSystems
else
Wscript.Echo "Computer Name: " & strComputer
Wscript.Echo "Operating System: " & strOS
WScript.Echo "Manufacturer: " & strManu
Wscript.Echo "Dell Service Tag: " & strDellTag
Wscript.Echo " "
end if




"Umesh Thakur" wrote:

> Change the following lines in DO UNTIL loop:
> set strComputer = Nothing
> Set strOS = Nothing
> Set strDellTag = Nothing
>
> WITH:
>
> strComputer = ""
> strOS = ""
> strDellTag = ""
>
> We use SET when we are working with OBJECTS. As these are simple string
> variables,
> we simply need to blank it.
>
> When loop is able to ping a computer and able to pull the information, these
> variables
> have values. However, when the loop does not ping a computer/unable to
> retrieve info,
> it uses the value of above variables (of previous loop execution).
>
> This is what I think the problem could be...
> Try it out...
>
> Regards,
> Umesh
> --
> When you are unable to keep your eyes open, do go and sleep for few hours!!!
>
>
> "Wmi.Query" wrote:
>
> > Thanks for the insight... I had tried commenting the "on error resume next",
> > which helped me find where I needed to add the error handling; I just didn't
> > know to use err.clear.
> >
> > Having made the changes you recommended, I'm having the following issue:
> >
> > When the loop hits a machine which produces an error description, I'm
> > getting data from the previous loop filled in the "Operating system", and
> > "Service tag" fields..
> >
> > For exapmle, my output might look like this for a good loop:
> >
> > Computer Name: DFS01
> > Operating System: Windows 2003 Server, standard edition
> > Service tag: 1234567
> >
> > On the very next loop, I get a ping reply, but I can't pull any information,
> > and I get the following:
> >
> > The remote server machine does not exist or is unavailable.
> > Computer Name: CORPWS01
> > Operating System: Windows 2003 Server, standard edition
> > Service tag: 1234567
> >
> > In the case of CORPWS01, I know that it's actually a Windows XP machine, and
> > I know it can't have the exact same service tag. I also know that I can get
> > good ping replies from it.
> >
> > Thanks again for the assitance. Let me know if you have any further
> > suggesions.
> >
> > "Umesh Thakur" wrote:
> >
> > > 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: C# coding guidelines: use "this." or not when referring to member fields/properties within the
    ... Alphabetic for strings isn't quite so ... One example of where people go wrong is when they want to optimise loop ... implementation so that each iteration takes 10% less time will only ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: timeit module: am I missing something obvious?
    ... Why am I passing strings around when functions are ... best of 3 trials: 0.792 usec per loop ... quickly choosing number of iterations. ... - The result from the final pass through the convergence loop ...
    (comp.lang.python)
  • Re: Stupid Newbie Needs Help
    ... Without the loop the program works fine with the ... with 0-terminated strings, that way you can take advantage of C's ... hold up to 10 tokens, where each token may be up to 80 characters ... should give a clue of how the variable/constant/function/macro is ...
    (comp.lang.c)
  • Re: To Richard Heathfield: enoughs enough
    ... the effect of using strlen in the for loop ... > with larger strings but couldn't be bothered to hoist the strlen ... >> causes trouble when you scale up. ... > an evil language too, ...
    (comp.programming)
  • Re: Calculating values of a 2d array by comparison of 2 strings
    ... I have 2 strings that I want to compare. ... If you do need the array form you can generate it from the count hash. ... Thank you very much for the suggestion... ...
    (comp.lang.perl.misc)