How does the "On Error Resume Next" behave?

Home Forums Scripting General Scripting How does the "On Error Resume Next" behave?

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    JeremyW
    Member
    #126296

    Right now I’m going through the Windows 2000 Scripting Guide so I’m still learning.

    I have a script that I wrote (borrowing some code from Rems :D) that works when I state On Error Resume Next just before the lines that need it. But if I put it at the beginning of the script it seems to exit the subroutine that the error occurs in.

    Is that how it’s supposed to work?

    Here’s the version that works

    Code:
    Option Explicit
    [color=red][b]’On Error Resume Next[/b][/color]

    Dim intMappedDrv, intLoopA, intLoopB, intTime
    Dim strIPGateway, strComputer, strUsername, strPassword, preDrive
    Dim sFolderShare, sAlias, strDrvLtr, strSharePath, strDisplayName, strConfGate
    Dim FreeDrive
    Dim objWMIService, objNet, colNetCons, objNetCon, colNICSet, objNICSet, fso
    Dim oShell, objNetDrv

    strIPGateway = “172.19.12.129”
    strDrvLtr = “P:”
    strSharePath = “\servershare”
    strDisplayName = “Authenticator”
    strComputer = “.”
    intLoopA = 0
    intLoopB = 0
    intTime = 30000 ‘< --------equal to 30 seconds Do Until intLoopA = 11 Checker Wscript.Sleep(intTime) Loop Sub Checker Set objNet = CreateObject("Wscript.Network") Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootCIMV2") Do Set colNetCons = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkConnection" & _ " WHERE RemotePath='\\server\share'") Set colNICSet = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration") intLoopB = 0 If colNetCons.Count = 0 Then For Each objNICSet in colNICSet [color=green][b]On Error Resume Next[/b][/color] strConfGate = Join(objNICSet.DefaultIPGateway,",") [color=green][b]On Error GoTo 0[/b][/color] If strConfGate = strIPGateway Then strConfGate = "" MapDrive strDrvLtr,strSharePath,strDisplayName Exit Sub Else strConfGate = "" End If Next Else For Each objNetCon In colNetCons If NOT objNetCon.Status = "OK" Then objNet.RemoveNetworkDrive objNetCon.LocalName,True,True intLoopB = 1 End If Next End If Loop Until intLoopB = 0 End Sub Sub MapDrive(preDrive,sFolderShare,sAlias) Set objNet = WScript.CreateObject("WScript.Network") Set fso = CreateObject("Scripting.FileSystemObject") Set oShell = CreateObject("Shell.Application") Set objNetDrv = objNet.EnumNetworkDrives '********************************************************** '* Checks to see if the "strDrvLtr" is in use. '* map to the next available drive letter on "Else" '********************************************************** If Not fso.DriveExists(preDrive) Then FreeDrive = preDrive Else For i = Asc("D") To Asc("Z") If Not fso.DriveExists(Chr(i)) Then FreeDrive = Chr(i) & ":" Exit For End If Next End If '***************************************** '* Maps the drive and sets the description '***************************************** GetUserPass objNet.MapNetworkDrive FreeDrive, sFolderShare, False, strUserName, strPassword oShell.NameSpace(FreeDrive).Self.Name = sAlias Set oShell = Nothing FreeDrive = preDrive End Sub '****************************** 'Inserted by the Install Script '****************************** Sub GetUserPass strUsername = "username" strPassword = "password" End Sub

    Here’s the version that doesn’t

    Code:
    Option Explicit
    [color=green][b]On Error Resume Next[/b][/color]

    Dim intMappedDrv, intLoopA, intLoopB, intTime
    Dim strIPGateway, strComputer, strUsername, strPassword, preDrive
    Dim sFolderShare, sAlias, strDrvLtr, strSharePath, strDisplayName, strConfGate
    Dim FreeDrive
    Dim objWMIService, objNet, colNetCons, objNetCon, colNICSet, objNICSet, fso
    Dim oShell, objNetDrv

    strIPGateway = “172.19.12.129”
    strDrvLtr = “P:”
    strSharePath = “\servershare”
    strDisplayName = “Authenticator”
    strComputer = “.”
    intLoopA = 0
    intLoopB = 0
    intTime = 30000 ‘< --------equal to 30 seconds Do Until intLoopA = 11 Checker Wscript.Sleep(intTime) Loop Sub Checker Set objNet = CreateObject("Wscript.Network") Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootCIMV2") Do Set colNetCons = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkConnection" & _ " WHERE RemotePath='\\server\share'") Set colNICSet = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration") intLoopB = 0 If colNetCons.Count = 0 Then For Each objNICSet in colNICSet [color=red][b]'On Error Resume Next[/b][/color] strConfGate = Join(objNICSet.DefaultIPGateway,",") [color=red][b]'On Error GoTo 0[/b][/color] If strConfGate = strIPGateway Then strConfGate = "" MapDrive strDrvLtr,strSharePath,strDisplayName Exit Sub Else strConfGate = "" End If Next Else For Each objNetCon In colNetCons If NOT objNetCon.Status = "OK" Then objNet.RemoveNetworkDrive objNetCon.LocalName,True,True intLoopB = 1 End If Next End If Loop Until intLoopB = 0 End Sub Sub MapDrive(preDrive,sFolderShare,sAlias) Set objNet = WScript.CreateObject("WScript.Network") Set fso = CreateObject("Scripting.FileSystemObject") Set oShell = CreateObject("Shell.Application") Set objNetDrv = objNet.EnumNetworkDrives '********************************************************** '* Checks to see if the "strDrvLtr" is in use. '* map to the next available drive letter on "Else" '********************************************************** If Not fso.DriveExists(preDrive) Then FreeDrive = preDrive Else For i = Asc("D") To Asc("Z") If Not fso.DriveExists(Chr(i)) Then FreeDrive = Chr(i) & ":" Exit For End If Next End If '***************************************** '* Maps the drive and sets the description '***************************************** GetUserPass objNet.MapNetworkDrive FreeDrive, sFolderShare, False, strUserName, strPassword oShell.NameSpace(FreeDrive).Self.Name = sAlias Set oShell = Nothing FreeDrive = preDrive End Sub '****************************** 'Inserted by the Install Script '****************************** Sub GetUserPass strUsername = "username" strPassword = "password" End Sub

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.