Help on error trapping VBScript

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

LVL 2
aidebAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
I haven't studied the whole script, but I did notice it seems to be doing some error checking in the main portion, but I also see the following line is commented out, this will prevent that logic from working.  Try uncommenting out the following line and see if that acts like it used to:

  'On Error Resume Next

~bp
1
aidebAuthor Commented:
Well spotted....I have uncommented but it makes no difference

On line 97 it performs a command where it uses the credentials that have been provided

I need it to execute line 108 if it doesn't complete successfully.
0
aidebAuthor Commented:
I put in an echo to determin what the status of err.number is however it is returned before the command line finishes running.

The issue seems to be that merely executing opening the command window gives the error =0 and it doesn't wait for the result of the command issued in that window

Any ideas?
0
Bill PrewCommented:
Okay, I don't think the Err.Number will accomplish what you want.  It will only report an error if the Shell.Run command itself failed, like if the program specified couldn't be found, etc.  I don't think it will report an error if it successfully runs the command given, even if that command sets a logical error condition at the DOS / process level.

You might be able to catch it by examining the return value from that call, something like this (notice I also added a TRUE as the third parm to the RUN() method, this will tell it to pause and wait for the executed command to finish before continuing, which should allow it to gather the exit code):

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

Open in new window

But I'm not sure if that will work either.  What does the c:\temp\sanur program do - at best the exit code of that process will be bubbled up through the intReturn variable, but if SANUR isn't setting a non zero error code then you may not see that in the VBS script.

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aidebAuthor Commented:
Thanks for the help - I got it sorted!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.