aideb
asked on
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?
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the help - I got it sorted!
ASKER
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.