Jump to content

Restart a function


31290
 Share

Recommended Posts

Hi guys, 

Hope you are fine today :)

I'm trying to restart a function by calling it back in the script:

Here's my code so far:

Func f_VPN()
    $iInputBox = InputBox("Password", "Please Enter the User's password.")
    If @Error = 1 Then 
            GUICtrlSetState($fVPN, $GUI_UNCHECKED)
    Else
        ShellExecute("C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe")
        $hVPN = WinWait("Cisco AnyConnect Secure Mobility Client", "Ready to connect")
        ControlClick($hVPN, "", "Button1")
        $hVPN1 = WinWait("Cisco AnyConnect | SEE VPN", "Cancel")
        ControlSetText($hVPN1, "", "Edit2", @Username)  
        Sleep(250)
        ControlSetText($hVPN1, "", "Edit3", $iInputBox)
        Sleep(250)
        ControlClick($hVPN1, "", "Button1")
        Sleep(5000)
        If ControlGetText("Cisco AnyConnect Secure Mobility Client", "", "Static2") = "Login Failed" Then 
            ControlClick("Cisco AnyConnect Secure Mobility Client", "", "Button4")
            WinClose($hVPN)
            f_VPN()
        Else
        ShellExecute("C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe")
        If ControlGetText("Cisco AnyConnect Secure Mobility Client", "", "Static2") = "Connected To SEE VPN." Then ControlClick("Cisco AnyConnect Secure Mobility Client", "", "Button1")
        Sleep(1000)
        WinClose("Cisco AnyConnect Secure Mobility Client", "")
        ProcessClose("vpnui.exe")
        ; ProcessClose("explorer.exe") <<< TO UNCOMMENT
        IniWrite($oIniFile, "LaptopChkBox", "VPN", "1")
        EndIf
    EndIf   
EndFunc

I'd like to the function to be restarted in the case the password provided is not correct. 
I've googled many things but nothing very relevant.

Any ideas over here?

Thanks :)

-31290

 

~~~ Doom Shall Never Die, Only The Players ~~~

Link to comment
Share on other sites

How about breaking things up into nice separate functions so your program flow is much easier to control/steer?

Something like this (with a lot of pseudocode that you can fill in yourself :) ):

Global $MAX_PWD_FAIL_COUNT = 3

; main script
If Not fireItUp() Then
    MsgBox(16, "error", "something went wrong, you probably got an earlier error about this")
Else
    MsgBox(64, "all good", "vpn logged in!")
EndIf

Exit


Func fireItUp()

    $clientGui = startClient()
    If Not $clientGui Then
        MsgBox(64, "failure to start client", "failed")
        Return False
    EndIf

    $failCount = 0
    While Not loginClient($clientGui)
        $failCount += 1
        If $failCount > $MAX_PWD_FAIL_COUNT Then
            MsgBox(16, "failure to login client", "failed " & $MAX_PWD_FAIL_COUNT & " times! Cancelling.")
            ; break down gui, kill process, whatever
            cleanupAfterFailedLogin()
            ; Return False, so the calling code can tell that stuff went wrong
            Return False
        EndIf
    WEnd

    ; If we get here, we know that everything was ok
    cleanupAfterCorrectLogin()

    Return True ;
EndFunc   ;==>fireItUp

Func startClient()
    ; Start client
    ; Return $hVPN1 if success, False if failure
EndFunc   ;==>startClient

Func loginClient($clientGui)
    ; Do login stuff using $clientGui
    ; Wait for window to reflect success (then return True) or failure (then return False)
EndFunc   ;==>loginClient

Func cleanupAfterCorrectLogin()
    cleanupGeneral()
    ; ini write, log write, whatever specific for handling correct login
EndFunc   ;==>cleanupAfterCorrectLogin

Func cleanupAfterFailedLogin()
    cleanupGeneral()
    ; ini write, log write, whatever specific for handling failed login
EndFunc   ;==>cleanupAfterFailedLogin

Func cleanupGeneral()
    ; gui process/windows close, ini write etc., anything that needs to be done whether everything worked or not
EndFunc   ;==>cleanupGeneral

 

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Thanks for your input SadBunny.

Thing is that this "VPN" function (like many other function present in one file) is called when  a tech is clicking on a checkbox coming from a main GUI. Then  the tech have to enter the end user's password so the VPN connection can be tested. 

 

$gMainGUI = GUICreate("SEE CHECKLISTS", 333, 500)
    GUISetBkColor($Color_White)
    GUISetFont(8.5, 700, 0)
    ;GUICtrlCreatePic($sResources & "SAClogo.jpg", 75, 27, 183, 59)
    ;GUICtrlCreateLabel("SEE CHECKLISTS", 120, 114)
    GUICtrlSetColor(-1, 0x800080)
    ;GUICtrlCreateSeperator(0, 0, 135, 2, 390)
    GUICtrlCreateLabel("[ Current Computer name: " & @ComputerName & " ]", 50, 151, 324, 17)    
    GUICtrlSetColor(-1, $COLOR_RED)
    ;GUICtrlCreateSeperator(0, 0, 181, 2, 390)
    $fVPN = GUICtrlCreateCheckbox("Test the VPN connection", 5, 391)
    GUICtrlSetTip(-1, "Open Cisco and login with user's credentials")
    GUICtrlSetOnEvent($fVPN, "f_VPN")
    GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
    GUISetState(@SW_SHOW)

If login is OK, then go on with script. But if Login failed, VPN process test has to be launched one or two more times. That's why I really need the VPN function to be recalled within the function.

~~~ Doom Shall Never Die, Only The Players ~~~

Link to comment
Share on other sites

I believe the issue is with your Window naming, I can get it to work fine, if I change the ControlGetText and ControlClick "Title" and "ClassNN" after the "Sleep(5000)" line, I also removed the WinClose as I couldn't see the use of this.  I'm not sure why you can't just get the Tech to type the Password into the VPN Client though, especially since the Password window stays open after it fails.  You could just check once VPN has successfully logged in before updating your ini file.

Link to comment
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
 Share

×
×
  • Create New...