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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Err Object Problems

Status
Not open for further replies.

twooly

MIS
Feb 22, 2004
122
US
I am trying to add a simple error handeling check in my script but doesn't seem to be working.

The script will work fine with servers that exist and don't cause error contitions but ones that would cause an error it doesn't seem to work.

Basicly I am testing to connect to a server that doesn't exist, which would cause an error condition. So I added some msgbox statements to track it and the server name passes fine and then connects to wmi but the next statement doesn't happen I never see the err.number result and the condition test never happens either. It just seems like it just exits the function.

Can someone point me in the correct direction? Thanks

Code:
On Error Resume Next
ShowDnsWins("testnoserver")

Sub ShowDnsWins( strServer )
	MsgBox strServer
	Set objNICs = GetObject("winmgmts:{impersonationLevel=impersonate}!//"& strServer)
	MsgBox Err.Number
	If Err.Number <> 0 Then
		MsgBox "Here:" & Err.Number
		strLine = strServer & "," & Err.Description
		Err.Clear
		objReport.WriteLine (strLine)
	Else
		Set colNICs = objNICs.InstancesOf( "Win32_NetworkAdapterConfiguration" )
		strLine = strServer & ","
		For Each NIC In colNICs
			If NIC.IPEnabled Then
					strLine = strLine & NIC.Description & ","
	        	    n = 1
			    	For Each strDns In NIC.DNSServerSearchOrder 
				    	strLine = strLine & strDns & ","
				    	n = n + 1
					Next
					n=1
					strLine = strLine & NIC.WINSPrimaryServer & ","
					strLine = strLine & NIC.WINSSecondaryServer
			End If
		Next
		objReport.WriteLine (strLine)
	End If
	
End Sub
 
Got it figured out I needed to have the On Error Resume Next
in the subroutine.
 
Actually it is meant to be inserted just prior to any statement you want to trap errors on, and then reset via [tt]On Error GoTo 0[/tt] as soon as you can after testing [tt]Err.Number[/tt].

Otherwise errors that are really errors (and not just exceptions you want to trap) go unreported and your script will be a nightmare to debug.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top