Announcement

Collapse
No announcement yet.

VBScript Bug?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VBScript Bug?

    Ok after a recent post http://forums.petri.com/showthread.php?t=18604 I made the nessacary changes but now I see a new problem develop.

    For some reason when the (sDriveLetter) is mapped in my (MapDrive) subroutine the wscript.sleep (5000) and Call DriveCheck statements right under it work fine. BUT if the mapping fails due to a bad password or such and the drive didn't map, those 2 statements or any statement added will be ignored completely if its under that line of code and the script finishes out.

    The third party DLL can be found here:

    http://internet.cybermesa.com/~bstewart/passdlg/


    Any Idea whats causeing that? or is that a bug?


    Code:
    On Error Resume Next
    Dim intDrive
    Dim CheckDrive
    Dim WshNetwork
    Dim sDriveLetter
    Dim AlreadyConnected
    Dim objPassDlg
    Dim sLabel
    Dim objShell
    Dim strMessage
    Dim MessageBox
    
    sDriveLetter = "K:"
    sLabel = "AUM Common"
     
    strMessage = MsgBox("Will you need AUM's Common Drive Today?   ", 36, "Welcome To AWA DET")
      If strMessage = 7 Then 
      	WScript.Quit
      Else
     	Call MapDrive
      End If
    
    Sub MapDrive
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    Set objPassDlg = CreateObject("PassDlg.LogonDialog")
    Set objShell = CreateObject("Shell.Application")
     objPassDlg.Confirm = True
     objPassDlg.ShowDialog "Connecting to AUM's Common Drive"
      If objPassDlg.Canceled Then
    	'Wscript.Echo "Cancelled"
    	WScript.quit
      Else
     	WshNetwork.MapNetworkDrive sDriveLetter, "\\x.x.x.x\Common", False, objPassDlg.UserName, objPassDlg.Password
     	objShell.NameSpace(sDriveLetter).Self.Name = sLabel
     	WScript.Sleep(5000)
     	Call DriveCheck
      End If
    End Sub
    
    Sub DriveCheck
     Set CheckDrive = WshNetwork.EnumNetworkDrives
     For intDrive = 0 To CheckDrive.Count - 1 Step 2 
      If CheckDrive.Item(intDrive) = sDriveLetter _
      	Then AlreadyConnected = True
      	Next
      If AlreadyConnected = True Then
       	MsgBox "Drive Mapping Successfull!" & vbCrLf & vbCrLf & "Welcome to AUM's Common Drive." _
       	,64,"Connecting to AUM's Common Drive"
       	WScript.Quit
      Else 
      	MessageBox = MsgBox ("Drive Mapping Unsuccessfull" & vbCrLf & vbCrLf & _
      	"Please check your password and make sure your username is as follows.   " _
        & vbCrLf & vbCrLf & "Example: aisinworld\username",21,"Connecting to AUM's Common Drive") 
     	Call Retry
     End If
    End Sub
    
    Sub Retry 
     If MessageBox = 4 Then
      	Call MapDrive
      If MessageBox = 2 Then
      	WScript.Quit
      End if
     Else
      	WScript.Quit
     End If
    End Sub
    Last edited by Dumber; 27th September 2007, 13:03. Reason: wrapped code tags arround the script.

  • #2
    Re: VBScript Bug?

    can you place your sample code between [CODE]...[/CODE]
    You can do this by selecting the text and click on the # button.
    - - -

    In your script there was an error raised during the drive mapping, but the script is not written to handle the error.

    For the subroutinte I now added a line
    On Error Resume next
    This will cause the script not to terminate on error And it will capture any error codes.
    Next is to translate the error code ( If Err.number <> 0 Then..... ) in an action. In the script below that is just a msgbox.
    It is a best practice to clear errors after dealing with it ( err.clear ), and to disable On Error Resume Next when you don't planned error handling anymore, with the line:
    On Error GoTo 0 (in this case error handling ends automaticaly when the sub is ended), or the script quits with (0).

    example:
    Code:
    Dim sDriveLetter : sDriveLetter = "K:"
    Dim sLabel       : sLabel = "AUM Common"
    Dim WshNetwork, strMessage
    
    Set WshNetwork = WScript.CreateObject("WScript.Network")
      
    strMessage = MsgBox("Will you need AUM's Common Drive Today? ", 36, "Welcome To AWA DET")
    If strMessage = 7 Then 
      WScript.Quit
    Else
      Call MapDrive(sDriveLetter, sLabel)
      Call DriveCheck(sDriveLetter)
    End If
    Wscript.quit(0)
    
    Sub MapDrive(sDriveLetter, sLabel)
      ON ERROR RESUME NEXT
      Set objPassDlg = CreateObject("PassDlg.LogonDialog")
      Set objShell = CreateObject("Shell.Application")
      objPassDlg.Confirm = True
      objPassDlg.ShowDialog "Connecting to AUM's Common Drive"
      If objPassDlg.Canceled Then
        'Wscript.Echo "Cancelled"
        WScript.quit(0)
      Else
        WshNetwork.MapNetworkDrive sDriveLetter, "\\x.x.x.x\Common", False, objPassDlg.UserName, objPassDlg.Password
      End If
      If Not CBool(Err.Number) Then
          objShell.NameSpace(sDriveLetter).Self.Name = sLabel
          ' no need to pause 5 second'
      Else
          If Err.Number = -2147024811 Then
           msgbox "drive letter already in use"
           err.clear
          ElseIf Err.Number = -2147023570 Then
           msgbox "bad password/user"
           err.clear
          Else
           msgbox "Unknown Error during drive mapping"
           err.clear
          End If
      End If    
    End Sub
    
    
    Sub DriveCheck(sDriveLetter)
      Set CheckDrive = WshNetwork.EnumNetworkDrives
      For intDrive = 0 To CheckDrive.Count - 1 Step 2 
        If CheckDrive.Item(intDrive) = UCase(sDriveLetter) Then _
          AlreadyConnected = True
      Next
      If AlreadyConnected = True Then
        CreateObject("Wscript.Shell").popup _
          "Drive Mapping Successfull!" & vbCrLf & vbCrLf _
          & "Welcome to AUM's Common Drive." , _
          3, "Connecting to AUM's Common Drive",64
        WScript.Quit
      Else 
        MessageBox = MsgBox ("Drive Mapping Unsuccessfull" & vbCrLf _
        & vbCrLf & "Please check your password and " _
        & "make sure your username is as follows. " & vbCrLf _
        & vbCrLf & "Example: aisinworld\username", _
        21,"Connecting to AUM's Common Drive") 
      End If
    End Sub
    With Error handling enabled, now the Sub DriveCheck(sDriveLetter) is only there for the "Go ahead" message.


    I used the function CBool() in the script. This function translates any stated value but '0' to True, and Null to False (w/out this function; a result '0' would be equal to True and only result '1' is equal to False)
    And I used the function UCase() in the other sub. This function forces the value to uppercase, for better comparasing

    \Rems
    Last edited by Rems; 10th October 2007, 20:30. Reason: ~ about the CBool() Function

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: VBScript Bug?

      Well all I can say is thanks again man!!! That worked and its finally done!!

      Where do you get the error codes? The ones you had are not workin so I took them out.

      Here is the final script. Although I did like your error arguments.

      Thanks again!!!!!!!!!!!!!!!!

      Code:
      Dim intDrive
      Dim CheckDrive
      Dim WshNetwork
      Dim sDriveLetter
      Dim AlreadyConnected
      Dim objPassDlg
      Dim sLabel
      Dim objShell
      Dim strMessage
      Dim MessageBox
      WScript.Sleep (30000)
      sDriveLetter = "K:"
      sLabel = "AUM Common"
      strMessage = MsgBox("Will you need AUM's Common Drive Today?   ", 36, "Welcome To AWA DET")
        If strMessage = 7 Then 
        	WScript.Quit
        Else
       	Call MapDrive
        End If
      Sub MapDrive
      On Error Resume Next
      Set WshNetwork = WScript.CreateObject("WScript.Network")
      Set objPassDlg = CreateObject("PassDlg.LogonDialog")
      Set objShell = CreateObject("Shell.Application")
      objPassDlg.ShowDialog "Connecting to AUM's Common Drive"
        If objPassDlg.Canceled Then
      	'Wscript.Echo "Cancelled"
      	WScript.quit
        Else
       	WshNetwork.MapNetworkDrive sDriveLetter, "\\x.x.x.x\Common", False, objPassDlg.UserName, objPassDlg.Password
       	objShell.NameSpace(sDriveLetter).Self.Name = sLabel
        End If
        If Not CBool(Err.Number) Then
            	WScript.Sleep (3000)
            	Call DriveCheck
        Else
            	WScript.Sleep (3000)
            	Call DriveCheck
        End If    
      End Sub  
      Sub DriveCheck
       Set WshNetwork = WScript.CreateObject("WScript.Network")
       Set CheckDrive = WshNetwork.EnumNetworkDrives
       For intDrive = 0 To CheckDrive.Count - 1 Step 2 
        If CheckDrive.Item(intDrive) = sDriveLetter _
        	Then AlreadyConnected = True
        	Next
        If AlreadyConnected = True Then
         	MsgBox "Drive Mapping Successfull!" & vbCrLf & vbCrLf & "Welcome to AUM's Common Drive." _
         	,64,"Connecting to AUM's Common Drive"
         	WScript.Quit
        Else 
        	MessageBox = MsgBox ("Drive Mapping Unsuccessfull" & vbCrLf & vbCrLf & _
        	"Please check your password and make sure your username is as follows.   " _
              & vbCrLf & vbCrLf & "Example: aisinworld\username",21,"Connecting to AUM's Common Drive") 
       	Call Retry
        End If
      End Sub
      
      Sub Retry 
       If MessageBox = 4 Then
        	Call MapDrive
        If MessageBox = 2 Then
        	WScript.Quit
        End if
       Else
        	WScript.Quit
       End If
      End Sub
      Last edited by Dumber; 27th September 2007, 13:02. Reason: Wrapped codetags arround the script.

      Comment


      • #4
        Re: VBScript Bug?

        What error number do you get?
        Code:
        On Error Resume Next
        WshNetwork.MapNetworkDrive sDriveLetter, "\\x.x.x.x\Common", False, objPassDlg.UserName, objPassDlg.Password
        ErrCheck(err.number)
        If Not CBool(Err.Number) Then
          objShell.NameSpace(sDriveLetter).Self.Name = sLabel
          ErrCheck(err.number)
        End If
        
        
        
        Sub ErrCheck(nr)
         Select Case nr
        
          Case 0
           Wscript.Echo "Mapping done succesfully", sDriveLetter
        
          Case -2147023570
           Wscript.Echo "Last error:", vbNewLine, _
                        "bad password or username "
        
          Case -2147023665
           Wscript.Echo "Last error:", vbNewLine, _
                        "Check server name "
        
          Case -2147023694
           Wscript.Echo "Last error:", vbNewLine, _
                        "Persistent connection found, try a second time "
           WshNetwork.RemoveNetworkDrive sDriveLetter
        
          Case -2147024811, 
           WScript.Echo "Last error:", vbNewLine, _
                        "Drive was in use, try again "
           WshNetwork.RemoveNetworkDrive sDriveLetter
        
          Case -2147023693
           WScript.Echo "Last error:", vbNewLine, _
                        "Check share name / Check UNC Path / No Server "
        
          Case -2147024829
           Wscript.echo "Last error:", vbNewLine, _
                        "Another share name problem / Network resource doesn't exist "
        
          Case -2147942453
           WScript.Echo "Last error:", vbNewLine, _
                        "Network PATH not found "
        
          Case -2147942467
           WScript.Echo "Last error:", vbNewLine, _
                        "Network NAME not found "
        
          Case Else
           Wscript.Echo "Research this number:  ", nr
         End Select
         Wscript.Echo "Errors = ", err.count 
         err.clear :  err.number = vbEmpty 
        End Sub
        Find out when the error raise, after the drive mapping or after the drive naming.


        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: VBScript Bug?

          Sorry for the delay of response, been gone a few days. Well its not that I get an error its that the "bad/username" and "drive already in use" arguments are ignored. I can't get them to trigger. Thatís why I was thinking maybe the code was not right.

          Itís ok thought I am happy with the script as it is now. We can put this one to rest.

          Thanks again for your input on this I never would have figured it out being noob in all .

          Comment

          Working...
          X