Sign in to follow this  
Followers 0
Sl4ck3r

Help Optimize my script

7 posts in this topic

Hi all - Im wrote this script about a year ago and i was wondering if there is anything i can do to optimize it. There seems to be a lot of repetition with if, then, if, then, and i would like to slim it down since i plan to add another section for region selection. Thanks

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=drive.ico
#AutoIt3Wrapper_outfile=console.exe
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>

; Hopefully minimizing and disabling the main WINPE dos window
winsetstate("Administrator:  Altiris Windows PE2 x86 Pre-boot Automation Environment (DISK boot)", "", @SW_MINIMIZE)


; Creates the GUI that has the Gold Image and Cancel buttons etc, etc etc... keep asking for more Ed
$GUI = GUICreate("Intermec HII USB Console", 506, 227)
GUICtrlCreateLabel ("Intermec HII USB Console v1.2", 175, 32, 180, 17)
$Button_1 = GUICtrlCreateButton("GOLD XP" ,48, 80, 185, 97, $BS_BITMAP)
GUICtrlSetImage($Button_1, "winxp_flag.bmp")
$Button_2 = GUICtrlCreateButton("GOLD WIN 7" ,272, 80, 185, 97, $BS_BITMAP)
GUICtrlSetImage($Button_2, "win7_flag.bmp")
$Button_3 = GUICtrlCreateButton("Disk Part" , 215, 190, 80, 20)
GUISetState()

WinSetTrans("Intermec HII USB Console","", 240)

$run = 0
While 1
    WinSetState("Intermec HII USB Console", "",@SW_DISABLE)
    $pass = InputBox ( "Password Needed", "Password Required by Intermec IT" , "" , "*", 250, 125) ;password box required to image system
    If @error = 1 Then run("wpeutil reboot"); restart if cancel is pressed
    If $pass = "rainbow.six" Then ; this is the password dont peek :P
        $run = 1
        ExitLoop
    Else
        msgbox(0, "", "Incorrect - Are you sure your IT?")
    EndIf
WEnd
    WinSetState("Intermec HII USB Console", "",@SW_ENABLE)

If $run = 1 Then
While 1
    $msg = GUIGetMsg()
    Select;
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button_2 ; Windows 7
            If FileExists("c:\rdeploy.exe") Then
                msgbox(0, "", "A hard drive does not exist or is not partitioned correctly")
                Exit
                EndIf
            If FileExists("d:\rdeploy.exe") Then
                Run("Win7.bat")
                Run("cscript.exe d:\drive\ddrivereplace.vbs")
                Run("d:\imagex.exe /ref d:\final*.swm /apply d:\final.swm 1 c:")
                EndIf
            If FileExists("e:\rdeploy.exe") Then
                Run("Win7.bat")
                Run("cscript.exe e:\drive\edrivereplace.vbs")
                Run("e:\imagex.exe /ref e:\final*.swm /apply e:\final.swm 1 c:")
                EndIf
            If FileExists("f:\rdeploy.exe") Then
                Run("Win7.bat")
                Run("cscript.exe f:\drive\fdrivereplace.vbs")
                Run("f:\imagex.exe /ref f:\final*.swm /apply f:\final.swm 1 c:")
                EndIf
            If FileExists("g:\rdeploy.exe") Then
                Run("Win7.bat")
                Run("cscript.exe g:\drive\gdrivereplace.vbs")
                Run("g:\imagex.exe /ref g:\final*.swm /apply g:\final.swm 1 c:")
                EndIf
            If FileExists("h:\rdeploy.exe") Then
                Run("Win7.bat")
                Run("cscript.exe h:\drive\hdrivereplace.vbs")
                Run("h:\imagex.exe /ref h:\final*.swm /apply h:\final.swm 1 c:")
                EndIf
        winsetstate("Intermec HII USB Console", "", @SW_DISABLE)
        winsetstate("Intermec HII USB Console", "", @SW_MINIMIZE)
            sleep(30000)
    while winexists("d:\imagex.exe", "")=1
        winsetstate("d:\imagex.exe", "", @SW_DISABLE)
        sleep(10000)
    WEnd
    while winexists("e:\imagex.exe", "")=1
        winsetstate("e:\imagex.exe", "", @SW_DISABLE)
        sleep(10000)
    WEnd
    while winexists("f:\imagex.exe", "")=1
        winsetstate("f:\imagex.exe", "", @SW_DISABLE)
        sleep(10000)
    WEnd
    while winexists("g:\imagex.exe", "")=1
        winsetstate("g:\imagex.exe", "", @SW_DISABLE)
        sleep(10000)
    WEnd
    while winexists("h:\imagex.exe", "")=1
        winsetstate("h:\imagex.exe", "", @SW_DISABLE)
        sleep(10000)
    WEnd
        run("hii.bat")
            ExitLoop
        Case $msg = $Button_1 ;Windows XP
            If FileExists("c:\rdeploy.exe") Then
                msgbox(0, "", "A hard drive does not exist or is not partitioned correctly")
                Exit
                EndIf
            If FileExists("d:\rdeploy.exe") Then
                Run("WinXP.bat")
                Run("cscript.exe d:\drive\ddrivereplace.vbs")
                Run("d:\rdeploy.exe -md -p2 -fd:\final.img -s100p")
                EndIf
            If FileExists("e:\rdeploy.exe") Then
                Run("WinXP.bat")
                Run("cscript.exe e:\drive\edrivereplace.vbs")
                Run("e:\rdeploy.exe -md -p2 -fe:\final.img -s100p")
                EndIf
            If FileExists("f:\rdeploy.exe") Then
                Run("WinXP.bat")
                Run("cscript.exe f:\drive\fdrivereplace.vbs")
                Run("f:\rdeploy.exe -md -p2 -ff:\final.img -s100p")
                EndIf
            If FileExists("g:\rdeploy.exe") Then
                Run("WinXP.bat")
                Run("cscript.exe g:\drive\gdrivereplace.vbs")
                Run("g:\rdeploy.exe -md -p2 -fg:\final.img -s100p")
                EndIf
            If FileExists("h:\rdeploy.exe") Then
                Run("WinXP.bat")
                Run("cscript.exe h:\drive\hdrivereplace.vbs")
                Run("h:\rdeploy.exe -md -p2 -fh:\final.img -s100p")
                EndIf
        winsetstate("Intermec HII USB Console", "", @SW_DISABLE)
        winsetstate("Intermec HII USB Console", "", @SW_MINIMIZE)
            sleep(30000)
    while winexists("RapiDeploy 6.9", "")=1
        winsetstate("RapiDeploy 6.9", "", @SW_DISABLE)

        sleep(10000)
    WEnd
        run("hii.bat")
            ExitLoop
        Case $msg = $Button_3 ; Diskpart
            If FileExists("c:\rdeploy.exe") Then
                msgbox(0, "", "A hard drive does not exist or is not being recognized by the system")
                Exit
                EndIf
            run("diskp.bat")
            run("console.exe")
            ExitLoop
    EndSelect
WEnd
EndIf

Share this post


Link to post
Share on other sites



Just where ever you have a bunch of if...thens change it to either a if...then...elseif...then or select...cases.

You don't need a community to do that for you, right?


For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I'm just curious to see if my version works:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=drive.ico
#AutoIt3Wrapper_Outfile=console.exe
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <Array.au3>

AutoItSetOption("MustDeclareVars", 1)

Dim $pass

; Hopefully minimizing and disabling the main WINPE dos window
If WinExists("Administrator: Altiris Windows PE2 x86 Pre-boot Automation Environment (DISK boot)") Then
    WinSetState("Administrator: Altiris Windows PE2 x86 Pre-boot Automation Environment (DISK boot)", "", @SW_MINIMIZE)
Else
    _term(1)
EndIf

While 1
    $pass = InputBox("Password Needed", "Password Required by Intermec IT", "", "*", 250, 125) ;password box required to image system
    If @error = 1 Then Run("wpeutil reboot"); restart if cancel is pressed
    If $pass = "rainbow.six" Then ; this is the password don't peek :P
        _Func1()
    Else
        MsgBox(0, "", "Incorrect - Are you sure you're IT?")
    EndIf
WEnd

Exit

Func _Func1()
    Local $GUI, $Button_1, $Button_2, $Button_3, $dir
    ; Creates the GUI that has the Gold Image and Cancel buttons etc, etc etc... keep asking for more Ed
    $GUI = GUICreate("Intermec HII USB Console", 506, 227)
    $Button_1 = GUICtrlCreateButton("GOLD XP", 48, 80, 185, 97, $BS_BITMAP)
    $Button_2 = GUICtrlCreateButton("GOLD WIN 7", 272, 80, 185, 97, $BS_BITMAP)
    $Button_3 = GUICtrlCreateButton("Disk Part", 215, 190, 80, 20)
    GUICtrlSetImage($Button_1, "winxp_flag.bmp")
    GUICtrlSetImage($Button_2, "win7_flag.bmp")
    GUICtrlCreateLabel("Intermec HII USB Console v1.2", 175, 32, 180, 17)
    GUISetState()
    While 1
        Switch GUIGetMsg()
            Case $Button_1 ;Windows XP
                $dir = _func3("rdeploy")
                Run("WinXP.bat")
                Run("cscript.exe " & $dir & ":\drive\" & $dir & "drivereplace.vbs")
                Run($dir & ":\rdeploy.exe -m" & $dir & " -p2 -f" & $dir & ":\final.img -s100p")
                _func2("RapiDeploy 6.9")
                ExitLoop
            Case $Button_2 ; Windows 7
                $dir = _func3("imagex")
                Run("Win7.bat")
                Run("cscript.exe " & $dir & ":\drive\" & $dir & "drivereplace.vbs")
                Run($dir & ":\imagex.exe /ref " & $dir & ":\final*.swm /apply " & $dir & ":\final.swm 1 c:")
                _func2($dir & ":\imagex.exe")
                ExitLoop
            Case $Button_3 ; Diskpart
                Run("diskp.bat")
                Run("console.exe")
                ExitLoop
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd
    Return
EndFunc ;==>_Func1

Func _func2($text)
    WinSetState("Intermec HII USB Console", "", @SW_DISABLE + @SW_MINIMIZE)
    Sleep(30000)
    While WinExists($text, "")
        WinSetState($text, "", @SW_DISABLE)
        Sleep(10000)
    WEnd
    Run("hii.bat")
    Return
EndFunc ;==>_func2

Func _func3($text)
    Local $dirList = DriveGetDrive("Fixed")
    Local $driveCount = $dirList[0] - 1
    Local $dir1, $flag = 0
    _ArrayDelete($dirList, 0)
    ; does 'redeploy.exe' or 'imagex.exe' exist on the system?
    For $i = 0 To $driveCount
        If FileExists($dirList[$i] & "\" & $text & ".exe") Then
            $dir1 = $dirList[$i]
            $flag = 1
            Return $dir1
        Else
            $flag = 0
            ContinueLoop
        EndIf
    Next
    If $flag = 0 Then _term(0, $text)
EndFunc ;==>_func3

Func _term($error = 2, $text = "")
    If $error = 0 Then
        MsgBox(0, "Error", $text & ".exe does not exist.")
    ElseIf $error = 1 Then
        MsgBox(0, '', "The 'Administrator: Altiris Windows PE2 x86 Pre-boot Automation Environment (DISK boot)' window does not exist")
    ElseIf $error = 2 Then
        MsgBox(0, 'Hello', "Goodbye")
    EndIf
    Exit
EndFunc ;==>_term
Edited by jaberwocky6669

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Looks ok to me. You did test run it yourself r19ht?.

Of course not.

yes of course but he has programs on his computer that I do not have on mine.

Edited by jaberwocky6669

Share this post


Link to post
Share on other sites

Sorry, not working. I am just getting the Error 2 msgbox - Even though the window does exist. I tried commenting out the error and making a couple changes but no gui is ever drawn.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Ok, i commented out "Else _term(1)" and I get a GUI. Not sure why it doesn't pickup on the window name. Also after hitting diskpart it opens 2 console.exe files. When selecting the XP button it tells me rdeploy cannot be found.

Ill tinker with it a little more to see what I can make work. Thanks for the rewrite though looks a lot cleaner then mine did.

Edited by Sl4ck3r

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