Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If Err.number <> 0 Then Loop?

Status
Not open for further replies.

tlitterio

IS-IT--Management
Jan 20, 2011
3
0
0
US
This is driving me crazy! I have the below code that works somewhat. What I want it to do is at the 2 points where it has the If Err.Number <> 0 Then statements to execute the code below then loop back to the top to start over again. What it is doing is executing the first computer in the list, then when it finds a machine with an error it displays as if all the following machines have the error; I know these machines work because I have moved them around in the ini file to make sure... Please help if you can, I am kind of new to all this.

Thanks!
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objOutput = objFSO.OpenTextFile(".\services.csv", ForAppending, True)
objOutput.Write ("System Name,IP Address,Date Ran,Caption,Service Status,Startup Type,Service Name,Account Name")
objOutput.Writeline

On Error Resume Next
Set InputFile = objFSO.OpenTextFile(".\computers.ini")
Do While Not (InputFile.atEndOfStream)
strComputer = InputFile.ReadLine
strCommand = "%comspec% /c ping -n 1 -w 100 " & 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:" _
' & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
If Err.Number <> 0 Then
objOutput.Write(strComputer) & ","
objOutput.Write("RPC Error")
objOutput.WriteLine
Exit
Else

Set objWMIService = objSWbemLocator.ConnectServer _
(strComputer, "root\cimv2", "%computername%\administrator", "Password")
objWMIService.Security_.ImpersonationLevel = 3

If Err.Number <> 0 Then
objOutput.Write(StrComputer) & ","
objOutput.Write("Authentication Error")
objOutput.WriteLine
Exit
Else

Set oServices = objWMIService.ExecQuery ("Select * from Win32_Service")
For Each objService in oServices
objOutput.Write(objService.SystemName) & ","
objOutput.Write(StrComputer) & ","
objOutput.Write(Date) & " "
objOutput.Write(Time) & ","
objOutput.Write(objService.Caption) & ","
objOutput.Write(objService.State) & ","
objOutput.Write(objService.StartMode) & ","
objOutput.Write(objService.Name) & ","
objOutput.Write(objService.StartName) & ","
objOutput.WriteLine
Next
End If
End If
End If
Loop
Loop

objOutput.Close
 
After you catch an error you will need to clear the error object or else it will retain the old value. Next time you check the error object, it thinks there is still an error. You'll need to use something like this:
Code:
If Err.Number <> 0 then
'your commands here
Err.Clear
End if
Also, what are you trying to do with the 'Exit' statements? I have never seen that keyword by itself (usually it is Exit Do, Exit For, Exit Function, or Exit Sub). Are you trying to exit the loop or the if block?
 
Also, "On Error Resume Next" means the program will continue even if a runtime error occurs. This is fine, as long as you immediately check the "Err.Number" value, handle your errors appropriately, and turn this off with "On Error Goto 0" when you're done. Otherwise, runtime errors will continue to be ignored and you can get quite unexpected results.

The following shows basic error trapping logic:
Code:
On Error Resume Next
'insert line of code you want to trap
If Err.Number <> 0 Then
   'Error handling code here...
End If
On Error Goto 0

 
The Err.Clear fixed it, thanks!!!

Thanks for the info on turning off the error checking too!

I was trying different things in my desperation and that is what the Exit was... I actually already knew that didn't work and thought I took it out prior to posting.

Thanks for the responses!
 
On Error Resume Next does not clear previous errors.
always have a Err.Clear immediately after it

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
On Error Resume Next does not clear previous errors
Really ?
script56.chm said:
The Err object's properties are reset to zero or zero-length strings ("") after an On Error Resume Next statement.
 
I think we can safely summarize like this:

[1] "On error resume next" will clear err object: this is only meaningful in the case of successive "on error resume next" statements without any "on error goto 0" statement in between, otherwise, err.number is zero in any case, as if there were any runtime error encountered, the script would have stopped already.

[2] "On error goto 0" will clear the err object: so that thereafter, it is having always err.number zero until it encounters new error and the script stops.

[3] In between the block where error is controlled, err.clear will reset err and make it ready to receive new error if any; if it has not been cleared (err.clear) and the script encounters new error in the meantime, the err object will be renewed and expose the most recent error encountered.
 
hmm, i stand corrected. I cant recall what situation i got myself in to believe that On Error Resume Next did not clear the Err.


I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top