We help IT Professionals succeed at work.
Get Started

Help on error trapping VBScript

aideb
aideb asked
on
137 Views
Last Modified: 2015-07-23
I have code that I am reusing for a different purpose.

The script previously asked a user for a username and password, stored it in a registry key then used it to execute a drive mapping.

I have modified it to use 'Runas' to open Internet explorer as another user account.

Previously, if the user entered incorrect credentials, the error trapping would pick this up in order to prompt again for the credentials. I have broken this and it isn't obvious (to me) how to do this.

Can anyone help?






'###############

'## Constants ##

'###############

Private Const KeyLoc="HKCU\Runas\"


Private Const DomName="EURO"
Private Const URL="http://www.google.com"




'###############

'##  Globals  ##

'###############

Dim wshNetwork, wshShell



Main()



'###############

'##   Main    ##

'###############

Sub Main()

  Dim varReturn, strUserName, objUser, objGroup, colGroups, strGroupDesc

  Dim strUserID, strPasswd, strServType, strDriveLet, strFullPath, strSrvName

  Dim colDrives, intCounter, blnMapFlag, objShell

  Dim UserName



  'On Error Resume Next

  'Create Global Objects

  Set wshNetwork = WScript.CreateObject("WScript.Network")

  Set wshShell = WScript.CreateObject("WScript.Shell")

 ' Set ObjShell = CreateObject("shell.applications")






      If Not (IsNull(varReturn) or (varReturn = 0)) Then

        strUserID = ""

        strPasswd = ""

        blnMapFlag = True

     End If

	
      blnMapFlag = True 


        If blnMapFlag Then

          If RegEntryExists(DomName) Then


            If Not ReadRegEntry(DomName, strUserID, strPasswd) Then MsgBox "An error occured reading your stored credentials for the " & DomName & " Domain."

          End If

          If strUserID = "" Then

            VarReturn= PromptForDetails(DomName, strUserID, strPasswd)

          End If

          If Not (strUserID = "") Then

            Err.Clear


WshShell.Run ("%comspec% /c ""runas /profile /user:" & DomName & "\" & strUserID & " " & """C:\Program Files\Internet Explorer\iexplore.exe """ & URL & " | d:\temp\sanur " & strPasswd & " """)




		If Err.Number = 0 Then

              If Not WriteRegEntry(DomName, strUserID, strPasswd) Then MsgBox "An error occured when saving your " & DomName & " credentials."

            Else

              varReturn = Msgbox ("An error occurred attempting to use " & DomName & ".  Would you like to re-enter you credentials?", 36, "error")

              If varReturn = 6 Then

                blnMapFlag = False

                VarReturn= PromptForDetails(DomName, strUserID, strPasswd)

                If Not (strUserID = "") Then

                  Err.Clear

                  

                  If Err.Number = 0 Then


                    If Not WriteRegEntry(DomName, strUserID, strPasswd) Then MsgBox "An error occured when saving your " & DomName & " credentials."

                  Else

                    MsgBox "Unable to launch. Please contact the Service Desk"

                  End If

                End If

              End If

            End If

          End If

        End If






End Sub





'###############

'## Functions ##

'###############

Private Function PromptForDetails(ByVal strServerName, ByRef strUserIDRef, ByRef strPasswdRef)

  PromptForDetails = False

  strUserIDRef = InputBox("Please enter your Username for the " & DomName & " Domain.")

  If strUserIDRef = "" Then Exit Function

  PromptForDetails = True '## Put here because password could be blank. ##

  strPasswdRef = InputBox("Please enter your Password for the " & DomName & " Domain.")

  If strPasswdRef = "" Then Exit Function

End Function



'###############

Private Function ReadRegEntry(ByVal strServerName, ByRef strUserIDRef, ByRef strPasswdRef)

  On Error Resume Next

  ReadRegEntry = False

  Err.Clear

  strUserIDRef = wshShell.RegRead(KeyLoc & strServerName & "\UserID")

  If Err.Number <> 0 Then Exit Function

  strPasswdRef = wshShell.RegRead(KeyLoc & strServerName & "\Passwd")

  If Err.Number <> 0 Then Exit Function

  ReadRegEntry = True

End Function



'###############

Private Function RegEntryExists(ByVal strServerName)

  Dim strRetVal

  On Error Resume Next

  RegEntryExists = False

  Err.Clear

  strRetval = wshShell.RegRead(KeyLoc & strServerName & "\UserID")

  If Err.Number <> 0 Then Exit Function

  If strRetval = "" Then Exit Function

  RegEntryExists = True

End Function



'###############

Private Function WriteRegEntry(ByVal strServerName, ByVal strUserID, ByVal strPasswd)

  On Error Resume Next

  WriteRegEntry = False

  Err.Clear

  wshShell.RegWrite KeyLoc& strServerName & "\UserID", strUserID, "REG_SZ"

  If Err.Number <> 0 Then Exit Function

  wshShell.RegWrite KeyLoc & strServerName & "\Passwd", strPasswd, "REG_SZ"

  If Err.Number <> 0 Then Exit Function

  WriteRegEntry = True

End Function

Open in new window

Comment
Watch Question
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 2 Answers and 5 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE