Sign in to follow this  
Followers 0
Dave70

Password changer

6 posts in this topic

Hi all...

I am trying to develope a script to allow my users to change their SQL login password from their own PC's (not members of a domain).

So far I have developed this simple cut down script but I have a problem I just can not resolve. The script has four input boxes, if any of them do not have acceptable values for the MSSQL connection string then the script fails, but I don't seem to be able to capture the @error... anyone got any idea?... please.

$server = InputBox(@ScriptName, "Please enter target server.", "")
If @error = 1 Then Exit
$user = InputBox(@ScriptName, "Please enter your user name.", "")
If @error = 1 Then Exit
$old_password = InputBox(@ScriptName, "Please enter your old password.", "", "*M")
If @error = 1 Then Exit
$new_password = InputBox(@ScriptName, "Please enter your new password.", "", "*M")
If @error = 1 Then Exit
CONNECT()
; **** This line does not get reached if any of the input values are wrong??? ****
If @error Then MsgBox (0, @ScriptName, "Connection failed, error code = " & @error & ". Outside function.")

;************************************************************* FUNCTION DECLARATION ********************************************************
Func CONNECT()
$conn = ObjCreate("ADODB.Connection")
$conn.ConnectionString = "DRIVER={SQL Server};SERVER=" & $server & ";DATABASE=master;UID=" & $user & ";PWD=" & $old_password & ";"
$conn.ConnectionTimeout = 30
$conn.Open
; **** This line does not get reached if any of the input values are wrong??? ****
If @error Then MsgBox (0, @ScriptName, "Connection failed, error code = " & @error & ". Inside function.")
$conn.CommandTimeout = 600
$conn.Execute("ALTER LOGIN [" & $user & "] WITH PASSWORD=N'" & $new_password & "' OLD_PASSWORD=N'" & $old_password & "'")
$conn.Close
EndFunc

Share this post


Link to post
Share on other sites



You shouldn't mask the new password without a second validation that the user had actually type new password or what they think they typed are two different thing.

The error never get trigger for SQL because you don't have any error handler. Default script crashed before it reach your @error

1 person likes this

Share this post


Link to post
Share on other sites

Hi Wayfarer,

I realise I need to validate the password, as I indicated this is a cut down script, where I am simply try to show what I am trying to achieve. Can you by any chance elaberate on what you mean by the statement "because you don't have any error handler"?

I admit I am missing something, I really expect the script to go to the next line... its not like the syntax is wrong or that I have left out a variable. I am obviously missing something when it comes to error handling.

Thank you

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Try it this way:

Global $g_eventerror = 0 ; to be checked to know if com error occurs. Must be reset after handling.

$oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Initialize a COM error handler

$server = InputBox(@ScriptName, "Please enter target server.", "")
If @error = 1 Then Exit
$user = InputBox(@ScriptName, "Please enter your user name.", "")
If @error = 1 Then Exit
$old_password = InputBox(@ScriptName, "Please enter your old password.", "", "*M")
If @error = 1 Then Exit
$new_password = InputBox(@ScriptName, "Please enter your new password.", "", "*M")
If @error = 1 Then Exit
CONNECT()
; **** This line does get reached if any of the input values are wrong!!! ****
If $g_eventerror then
    $g_eventerror = 0
MsgBox(0, @ScriptName, "Connection failed, error code = " & @error & ". Outside function.")
Endif

;************************************************************* FUNCTION DECLARATION ********************************************************
Func CONNECT()
$conn = ObjCreate("ADODB.Connection")
$conn.ConnectionString = "DRIVER={SQL Server};SERVER=" & $server & ";DATABASE=master;UID=" & $user & ";PWD=" & $old_password & ";"
$conn.ConnectionTimeout = 30
$conn.Open
; **** This line does not get reached if any of the input values are wrong??? ****
If @error Then MsgBox(0, @ScriptName, "Connection failed, error code = " & @error & ". Inside function.")
$conn.CommandTimeout = 600
$conn.Execute("ALTER LOGIN [" & $user & "] WITH PASSWORD=N'" & $new_password & "' OLD_PASSWORD=N'" & $old_password & "'")
$conn.Close
EndFunc ;==>CONNECT

; This is my custom defined error handler
Func MyErrFunc()

MsgBox(0, "AutoItCOM Test", "We intercepted a COM Error !" & @CRLF & @CRLF & _
"err.description is: " & @TAB & $oMyError.description & @CRLF & _
"err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _
"err.number is: " & @TAB & Hex($oMyError.number, 8) & @CRLF & _
"err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _
"err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _
"err.source is: " & @TAB & $oMyError.source & @CRLF & _
"err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _
"err.helpcontext is: " & @TAB & $oMyError.helpcontext _
)

Local $err = $oMyError.number
If $err = 0 Then $err = -1

$g_eventerror = $err ; to check for after this function returns
EndFunc ;==>MyErrFunc
the new code is from the 2. example "ObjEvent" in the Helpfile

Edited by AutoBert

Share this post


Link to post
Share on other sites

Thank you so much... excellent.

I will be honest with you, I had no idea where to start. You providing the reference information about "ObjEvent", even if I knew that I think I would have still got it wrong.

Thanks heaps.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0