Comments

Latest Posts

AutoIt Scripts Examples

One of my projects is going to use AutoIt script to launch client application on Windows machine. It will usually require to auto-fill in some information on popped up windows. 

This post is to collect some useful scripts I found from Internet. Some of them are made by myself for certain purpose. 





AutoInstallation Script


;
; NComputing AutoInstallation Script v1.1.1.06262012
; AutoIt Version: 3.0
; Language: English
; Platform: Win7
; Author:    Matthew Roslevich
;    /email/
;
; Script Function:
; Opens NComputing vSpace Windows 7 x64 installer and fills in the registration information.
;


; Run vSpace installer
Run("vSpace_X-6.2.5.4_setup.exe")


; Wait for the vSpace installer window to become active - it is titled "NComputing vSpace"
WinWaitActive("NComputing vSpace")


; Now that the vSpace window is active, start the installer

; Initial installer screen
Sleep(1000)
Send("{TAB 2}")
Sleep(500)
Send("{ENTER}")


; vSpace End User License Agreement
Sleep(1000)
Send("{TAB}")
Sleep(500)
Send("{RIGHT}")
Sleep(500)
Send("{ENTER}")


; vSpace Terms of Use Agreement
Sleep(1000)
Send("{TAB}")
Sleep(500)
Send("{RIGHT}")
Sleep(500)
Send("{ENTER}")


; Initial Screen Resolution (default is 1024x768 16bpp 60Hz)
Sleep(1000)
Send("{ENTER}")


; Select Installation Folder (default is C:\Program Files\NComputing\NComputing vSpace\)
Sleep(1000)
Send("{ENTER}")


; Confirm Installation
Sleep(1000)
Send("{ENTER}")


; Now a screen will pop up and ask to install device software, the window is called
; "Windows Security" and has some text "Install" and "Don't Install"
WinWaitActive("Windows Security")
Sleep(1500)
Send("{LEFT 2}")
Sleep(500)
Send("{SPACE}")
Sleep(500)
Send("{RIGHT}")
Sleep(500)
Send("{ENTER}")


; Another screen will pop up with registration details, the window is called
; "License wizard" and has a form which needs filled out.
WinWait("License wizard")
Sleep(1000)
Send("Technology Department")
Sleep(500)
Send("{TAB}")
Sleep(500)
Send("School District X")
Sleep(500)
Send("{TAB 2}")
Sleep(500)
Send("Address 1")
Sleep(500)
Send("{TAB}")
Sleep(500)
Send("City")
Sleep(500)
Send("{TAB}")
Sleep(500)
Send("PA")
Sleep(500)
Send("{TAB}")
Sleep(500)
Send("17701")
Sleep(500)
Send("{TAB}")
Sleep(500)
Send("[email protected]")
Sleep(500)
Send("{TAB}")
Sleep(500)
Send("570-555-1212")
Sleep(500)
Send("{TAB}")
Sleep(500)
Send("NComputing")
Sleep(500)
Send("{TAB}")
Sleep(500)
Send("{DOWN}")
Sleep(500)
Send("{TAB}")
Sleep(500)
Send("{ENTER}")


; Yet another screen will pop up reminding user about attaching device(s) after installation finishes.
; The window is called "NComputing vSpace" and has an "OK" button.
WinWaitActive("NComputing vSpace")
Sleep(1000)
Send("{ENTER}")


; The installer is finished! Last thing is to press the "CLOSE" button.
WinWaitActive("NComputing vSpace")
Sleep(1000)
Send("{ENTER}")


; Standard [you must restart the system to have change take effect] window.
; The window is still called "NComputing vSpace", and has a "YES" and "NO" button.
WinWaitActive("NComputing vSpace")
Sleep(1000)
Send("{ENTER}")


; Finished!

OpenLinkInIE.au3


;
; this script copies an address from Mozilla Firefox and opens it in IE
; click in Firefox's address bar before running the script

Opt("WinTitleMatchMode", 2) ; this tells AutoIt to match any part of window title in the following window commands
WinActivate("Mozilla Firefox") ; activates the Mozilla Firefox window
WinWaitActive("Mozilla Firefox") ; waits for Mozilla Firefox to be the active window before continuing
Send("{HOME}") ; the Send command tells AutoIt to type text; this line simulates pressing the Home key
Send("+{END}") ; this line simulates pressing Shift+End
Send("^c") ; this line simulates CTRL+c
Run("C:\Program Files\Internet Explorer\iexplore.exe") ; starts IE; you may need to edit this path
WinActivate("Internet Explorer") ; activates the IE window
WinWaitActive("Internet Explorer") ; waits for IE to be the active window before continuing
Send("^t") ; opens new tab in IE
WinWaitActive("New Tab - Windows Internet Explorer") ; you may need to edit this text depending on you IE version 
Send("^v") ; pastes the contents of the clipboard
Send("{ENTER}") ; this line simulates pressing Enter



OpenLinkInIE.au3

Link: https://www.autoitscript.com/forum/topic/161289-simple-example-scripts-for-beginners-tutorial/
;
; this script copies an address from Mozilla Firefox and opens it in IE
; click in Firefox's address bar before running the script

Opt("WinTitleMatchMode", 2) ; this tells AutoIt to match any part of window title in the following window commands
WinActivate("Mozilla Firefox") ; activates the Mozilla Firefox window
WinWaitActive("Mozilla Firefox") ; waits for Mozilla Firefox to be the active window before continuing
Send("{HOME}") ; the Send command tells AutoIt to type text; this line simulates pressing the Home key
Send("+{END}") ; this line simulates pressing Shift+End
Send("^c") ; this line simulates CTRL+c
Run("C:\Program Files\Internet Explorer\iexplore.exe") ; starts IE; you may need to edit this path
WinActivate("Internet Explorer") ; activates the IE window
WinWaitActive("Internet Explorer") ; waits for IE to be the active window before continuing
Send("^t") ; opens new tab in IE
WinWaitActive("New Tab - Windows Internet Explorer") ; you may need to edit this text depending on you IE version 
Send("^v") ; pastes the contents of the clipboard
Send("{ENTER}") ; this line simulates pressing Enter


CopyPasteNotepadToWord_2.au3

Link: https://www.autoitscript.com/forum/topic/161289-simple-example-scripts-for-beginners-tutorial/
; This script copies and pastes 5 lines between two open Word documents.
; Before running this script, open two Word files and save one as old.docx (or old.doc)
; and one as new.docx. The names must be in lower case to match the script.
; Type 5 lines of text in the file named old.

Opt("WinTitleMatchMode", 2) ; configures AutoIt to find a search term in any part of the window title

Dim $x

WinActivate("old") ; activates window with old in the title
WinWaitActive("old") ; waits for the window to be active
Send("^{HOME}") ; simulates pressing CTRL+Home to go to top of document
For $x = 1 to 5 ; this is a loop that is done 5 times
   WinActivate("old") ; activates window with Old in the title
   WinWaitActive("old"); waits for the window to be active
   Send("{HOME}") ; simulates pressing the Home key
   Sleep(500) ; pauses for 1/2 a second; I added the pauses to show you what the script is doing
   Send("+{END}") ; simulates pressing Shit+End
   Sleep(500) ; pauses for 1/2 a second
   Send("^c") ; simulates pressing CTRL+c
   Send("{RIGHT}") ; simulates pressing the right arrow key
   Sleep(500) ; pauses for 1/2 a second
   WinActivate("new") ; activates window with new in the title
   WinWaitActive("new"); waits for the window to be active
   Send("^v")  ; simulates pressing CTRL+v
   Sleep(500) ; pauses for 1/2 a second
Next




MouseSwitchButtons.au3

Link: https://www.autoitscript.com/forum/topic/161289-simple-example-scripts-for-beginners-tutorial/
; Switches right and left mouse buttons.
; This works with the standard Mouse Properties Control Panel.
; If you installed a program that changes the Mouse Properties control panel,
; it may not work.
; For a list of all the Control Panel applets,
; see http://en.wikipedia.org/wiki/Control_Panel_%28Windows%29
; or google "CPL list."

Run("control.exe main.cpl") ; Opens the Mouse Properties Control Panel applet 
WinWaitActive("Mouse Properties") ; Waits for the window to open
Sleep(1000) ; (Optional) Pauses one second so you can watch the script work
Send("!s{ENTER}") ; Types ALT+s then presses the Enter key



Launch Local Application



;Thycotic ssms.exe with sql credential launcher script

;set filepath of ssms.exe

$Path = "C:\Program Files (x86)\Microsoft SQL Server Management Studio 18\Common7\IDE\Ssms.exe"

;execute ssms.exe

ShellExecute($Path)

;Wait for the 'connect to server' window to appear

WinWaitActive("Connect to Server", "", 0)

;set auth mode to SQL authenticaiton

ControlSend("Connect to Server" , "" , "[NAME:comboBoxAuthentication]", "[NAME:SQL]")

;set server instance to servername passed from Secret Server launcher in cmd line parameter 1

ControlSetText("Connect to Server", "", "[NAME:serverInstance]", $CmdLine[1],1)

;set username to username passed from Secret Server launcher in cmd line parameter 2

ControlSetText("Connect to Server", "", "[NAME:userName]", $CmdLine[2],1)

;set password to password passed from Secret Server launcher in cmd line parameter 3

ControlSetText("Connect to Server", "", "[NAME:password]", $CmdLine[3],1)

;reactivate the connect to server window, required for the click of buttons within the window

WinActivate ("Connect to Server")

;click the connect button

ControlClick("Connect to Server", "", "[NAME:connect]")



Error Handling


From: https://www.autoitscript.com/forum/topic/101030-simple-error-handling-_throwerror/

;===============================================================================
; Description:      Display an error message and optionally exit or set
;                   error codes and return values. Enables single-line
;                   error handling for basic needs.
; Parameter(s):     $txt        = message to display
;                   [$exit]     = 1 to exit after error thrown, 0 to return
;                   [$ret]      = return value
;                   [$err]      = error code to return to parent function if $exit = 0
;                   [$ext]      = extended error code to return to parent function if $exit = 0
;                   [$time]     = time to auto-close message box, in seconds (0 = never)
; Requirement(s):   None
; Return Value(s):
; Note(s):          Icon is STOP for EXIT/FATAL errors and EXCLAMATION for NO_EXIT/WARNING errors.
;                   For single-line error-reporting. If reporting an error in a function,
;                   can call this with a Returned value as:
;                       If $fail Then Return _ThrowError("failed",0,$return_value)
;===============================================================================
Func _ThrowError($txt, $exit = 0, $ret = "", $err = 0, $ext = 0, $time = 0)
    If $exit = 0 Then
        MsgBox(48, @ScriptName, $txt, $time) ; Exclamation, return with error code
        Return SetError($err, $ext, $ret)
    Else
        MsgBox(16, @ScriptName, $txt, $time) ; Stop, quit after error
        Exit ($err)
    EndIf
EndFunc

Examples in use:

$file = FileOpen($filename,0)
if $file =-1 then _ThrowError("File " & $file & " not found!",1) ; Exit when msgbox closed

$clip = ClipGet()
if $clip = "" then _ThrowError("Clipboard is empty. Continuing...", 0, 0, 0, 3) ; No exit, no error, auto-close in 3 seconds

Func parse_html_title($source_string)
    Local $title = StringRegExp($source_string,"<title>([^>]*)</title>",1)
    If Not IsArray($title) then Return _ThrowError("Failed to extract title", 0, "-no title found-", 1, 0, 3) ; No exit, error=1, auto-close in 3 seconds
    Return $title[0]
EndFunc



Check Active Window Title, Class, Size


#include <WinAPI.au3>
#include <MsgBoxConstants.au3>
Sleep(500)

CheckWinSize()


Func CheckWinSize()
   Local $hwnd= WinGetHandle("[ACTIVE]")
   Local $aClientSize = WinGetClientSize($hWnd)
   ;MsgBox($MB_SYSTEMMODAL, "", "Width: " & $aClientSize[0] & @CRLF & "Height: " & $aClientSize[1])
   if $aClientSize[0]<280 then
	  ;MsgBox($MB_SYSTEMMODAL, "", "Wrong Password. Width: " & $aClientSize[0] & @CRLF & "Height: " & $aClientSize[1])
	  MsgBox($MB_SYSTEMMODAL, "", "Wrong Password. Please close all XCA windows and contact system admin!")
	  ;WinClose($hWnd)
   Else
	  ;MsgBox($MB_SYSTEMMODAL, "", "Right Password. Width: " & $aClientSize[0] & @CRLF & "Height: " & $aClientSize[1])
   EndIf
EndFunc   ;



Func CheckWinClass()

   Local $hwnd= WinGetHandle("[ACTIVE]")
   if $hwnd = 0x0019081 then
	  MsgBox(4096, "Password Correct", "Password is correct!" & $hwnd)
	  ;MsgBox(4096, "Get ClassName", "ClassName of " & $hwnd & ":" & _WinAPI_GetClassName($hwnd))
   Else
	  MsgBox(4096, "Password Wrong", "Password is wrong! Please close all XCA window and contact system admin" & $hwnd)
   EndIf

EndFunc   ;

Func CheckWinTitle()
    ; Retrieve the window title of the active window.
    Local $sText = WinGetTitle("[ACTIVE]")
   if StringInStr ($sText, "Key management") AND StringInStr ($sText, "X Certificate") Then
	  MsgBox($MB_SYSTEMMODAL, "", "Wrong Password, please close all Windows and contact Thycotic System Admin!")
    ; Display the window title.
   else
	  MsgBox($MB_SYSTEMMODAL, "", $sText+"is correct!")
   EndIf
EndFunc   ;


Func _ThrowError($txt, $exit = 0, $ret = "", $err = 0, $ext = 0, $time = 0)
    If $exit = 0 Then
        MsgBox(48, @ScriptName, $txt, $time) ; Exclamation, return with error code
        Return SetError($err, $ext, $ret)
    Else
        MsgBox(16, @ScriptName, $txt, $time) ; Stop, quit after error
        Exit ($err)
    EndIf
EndFunc


;Finish



Launch Local Application and Autofill Password

Link: https://www.autoitscript.com/forum/topic/161289-simple-example-scripts-for-beginners-tutorial/
;Thycotic xca.exe with password launcher script
;Author: Netsec
;set filepath 
;$Path = "c:\tools\xca\xca.exe c:\tools\certs\xca.xdb"
$Path = "C:\tools\xca\xca.exe"
;execute xca.exe to open xdb
;ShellExecute($Path)
ShellExecute($Path, "c:\tools\certs\xca.xdb")
;Wait for the 'Password' window to appear
WinWaitActive("Password", "", 0)
;WinActivate ("Password")
;set password to password passed from Secret Server launcher in cmd line parameter 3
;ControlSetText("Password", "", "[NAME:password]", $CmdLine[3],1)
;ControlSetText("Password", "", "[NAME:password]", "Password1234")

;$value=Send($CmdLine[1])
$value=Send("Password1234")
if $value =-1 then _ThrowError("Password" & $value & " not enter correctly! Please close program and contact system admin",1) ; Exit when msgbox closed

Sleep(200)

Opt("WinTitleMatchMode",2); Windows title Match
;reactivate the connect to server window, required for the click of buttons within the window
WinActivate ("Password")
WinWaitActive("Password", "", 0)
;click the connect button
;ControlClick("Password", "", "[NAME:OK]")
Send("{Enter}")

Sleep(500)

Another Example to Auto-Fill Credentials


#include <WinAPI.au3>
#include <MsgBoxConstants.au3>
;Start:
HotKeySet ("{F10}","BlockinputOff")
;Thycotic SolarWinds ARMs with password launcher script
;Author: Net Sec
;set filepath
$Path = "C:\Program Files\SolarWinds\ARM\bin\app8Man.exe"

If WinExists("ARM 2020.2.5.4920") Then
   WinClose("ARM 2020.2.5.4920")
   ;WinActivate ("ARM 2020.2.5.4920")
   MsgBox(0,"Closing ARMS window", "For Safety, Closed ARM window. Please re-run your launcher again")
   Exit
Else

   ;execute app8Man.exe to open Arms
   ShellExecute($Path)

   ;Wait for the 'Password' window to appear
   WinWaitActive("ARM 2020.2.5.4920", "", 0)

EndIf



Sleep(200)




;$value=Send($CmdLine[1])
;$value=Send("Password1234")

;if $value =-1 then _ThrowError("Password" & $value & " not enter correctly! Please close program and contact system admin",1) ; Exit when msgbox closed

;Sleep(200)

;Opt("WinTitleMatchMode",2); Windows title Match
;reactivate the connect to server window, required for the click of buttons within the window
;WinActivate ("Password")
;WinWaitActive("Password", "", 0)
;click the connect button
;ControlClick("Password", "", "[NAME:OK]")
;Send("{Enter}")

GetScreenMiddle()

;MsgBox($MB_SYSTEMMODAL, "", "Width: " & $ScreenWidth & @CRLF & "Height: " & $ScreenHeight)

Local $WinPos = WinGetPos("[ACTIVE]")
;MsgBox($MB_SYSTEMMODAL, "", "x-pos: " & $WinPos[0] & @CRLF & "y-pos: " & $WinPos[1] & @CRLF & "win width: " & @CRLF &  $WinPos[2] "win height " & $WinPos[3])
;MsgBox($MB_SYSTEMMODAL, "", "x-pos: " & $WinPos[0])
;MsgBox($MB_SYSTEMMODAL, "", "y-pos: " & $WinPos[1])
;MsgBox($MB_SYSTEMMODAL, "", "Window width: " & $WinPos[2])
;MsgBox($MB_SYSTEMMODAL, "", "Window Height: " & $WinPos[3])

;MsgBox($MB_SYSTEMMODAL, "", "Mouse move To " & $ScreenWidth/2+$WinPos[1]/2-10)
;MouseMove ($ScreenWidth/2+$WinPos[2]/2-10, $ScreenHeight/2-10, 10)

WinActivate("ARM 2020.2.5.4920")
$var1 = PixelGetColor( $ScreenWidth/2+$WinPos[2]/2-50, $ScreenHeight/2-40)
$var2 = PixelGetColor( $ScreenWidth/2, $ScreenHeight/2)
;MsgBox(0,"The middle decmial color is 2718100", $var)
;MouseMove ($ScreenWidth/2+$WinPos[2]/2-50, $ScreenHeight/2-40, 10)
;Sleep(500)
;MsgBox(0,"The middle hex color is 297994, $ScreenWidth/2+$WinPos[2]/2-10's color is 444444", Hex($var1, 6))
;MouseMove ($ScreenWidth/2, $ScreenHeight/2, 10)
;MsgBox(0,"The middle hex color is 297994, $ScreenWidth/2+$WinPos[2]/2-10's color is 444444", Hex($var2, 6))

;If it is login page (three text boxes), the var1=FFFFFF=var2. If it is AD user icon page (No text box), the var2 is 297994, var1=444444

If Hex($var1,6) = "444444" And Hex($var2,6) = "297994" Then
	  ;MsgBox(0,"AD Account Page", "This AD Account, no text box Page", 6)
   	  Send("{Tab 2}")
	  Send("{Enter}")
   Else
	  If Hex($var1,6) = "FFFFFF" and Hex($var2,6) = "FFFFFF" Then
			;MsgBox(0,"Three Textboxes Page", "This three Textboxes Page", 6)
			Send("{Tab 1}")

			;set password to password passed from Secret Server launcher in cmd line parameter 3
			;ControlSetText("Password", "", "[NAME:password]", $CmdLine[3],1)
			;ControlSetText("Password", "", "[NAME:password]", "Password1234")

			Blockinput(1)
			;$value=Send($CmdLine[1])
			$value1=Send("test1")
			;Sleep(100)
			Send("{Tab 1}")
			Sleep(100)
			;$value=Send($CmdLine[1])
			$value2=Send("password1234",1)
			;Sleep(100)
			;if $value1 =-1  or $value2 =-1 then _ThrowError("Password" & $value & " not enter correctly! Please close program and contact system admin",1) ; Exit when msgbox closed

			Send("{Enter}")

			Sleep(10500)

			If WinExists("ARM 2020.2.5.4920") Then
				  WinClose("ARM 2020.2.5.4920")
				  ;WinActivate ("ARM 2020.2.5.4920")
				  BlockInput(0)
				  MsgBox(0,"Closing ARMS window", "Logging failure, please contact your TSS sys admin!")
				  Exit
			EndIf
			BlockInput(0)

	  EndIf
EndIf




;CheckWinSize()

Func GetScreenMiddle()
   ;Global [email protected]/2
   ;Global [email protected]/2
   Local $ScreenSize=WinGetPos("Program Manager")
   Global $ScreenWidth=$ScreenSize[2]
   Global $ScreenHeight=$ScreenSize[3]


EndFunc

Func CheckWinSize()
   Local $hwnd= WinGetHandle("[ACTIVE]")
   Local $aClientSize = WinGetClientSize($hWnd)
   ;MsgBox($MB_SYSTEMMODAL, "", "Width: " & $aClientSize[0] & @CRLF & "Height: " & $aClientSize[1])
   if $aClientSize[0]<280 then
	  ;MsgBox($MB_SYSTEMMODAL, "", "Wrong Password. Width: " & $aClientSize[0] & @CRLF & "Height: " & $aClientSize[1])
	  MsgBox($MB_SYSTEMMODAL, "", "Wrong Password. Please close all XCA windows and contact system admin!")
	  ;WinClose($hWnd)
   Else
	  ;MsgBox($MB_SYSTEMMODAL, "", "Right Password. Width: " & $aClientSize[0] & @CRLF & "Height: " & $aClientSize[1])
   EndIf
EndFunc   ;



Func CheckWinClass()

   Local $hwnd= WinGetHandle("[ACTIVE]")
   if $hwnd = 0x0019081 then
	  MsgBox(4096, "Password Correct", "Password is correct!" & $hwnd)
	  ;MsgBox(4096, "Get ClassName", "ClassName of " & $hwnd & ":" & _WinAPI_GetClassName($hwnd))
   Else
	  MsgBox(4096, "Password Wrong", "Password is wrong! Please close all XCA window and contact system admin" & $hwnd)
   EndIf

EndFunc   ;

Func CheckWinTitle()
    ; Retrieve the window title of the active window.
    Local $sText = WinGetTitle("[ACTIVE]")
   if StringInStr ($sText, "Key management") AND StringInStr ($sText, "X Certificate") Then
	  MsgBox($MB_SYSTEMMODAL, "", "Wrong Password, please close all Windows and contact Thycotic System Admin!")
    ; Display the window title.
   else
	  MsgBox($MB_SYSTEMMODAL, "", $sText+"is correct!")
   EndIf
EndFunc   ;


Func _ThrowError($txt, $exit = 0, $ret = "", $err = 0, $ext = 0, $time = 0)
    If $exit = 0 Then
        MsgBox(48, @ScriptName, $txt, $time) ; Exclamation, return with error code
        Return SetError($err, $ext, $ret)
    Else
        MsgBox(16, @ScriptName, $txt, $time) ; Stop, quit after error
        Exit ($err)
    EndIf
EndFunc

Func BlockinputOff()
	  Blockinput(0)
EndFunc
;Finish


Launch CheckPoint SmartConsole



#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.14.5
Script Function: Start Checkpoint SmartConsole using Secret Server parameters (to be used alongisde Thycotic Secret Server custom launcher)
Checkpoint SmartConsole Filler.
#ce ----------------------------------------------------------------------------
run("C:\Program Files (x86)\CheckPoint\SmartConsole\R80.40\PROGRAM\SmartConsole.exe") ; Run the Thick Client program
WinWaitActive("Check Point SmartConsole") ; Wait for the program window to be active
Sleep(5000) ; Wait 5000ms (5s) to get by the splash screen
; Upon opening, the cursor defaults to the Username box so enter the username by issuing the �Send�
;command. The argument �1� is used to send the text in RAW format to cater for special characters that might
;appear in the username
Send($CmdLine[1],1)
Send("{TAB}") ; Go over to the next box by sending one TAB keystroke
Send($CmdLine[2],1) ; Enter the password
Send("{TAB}") ; Go over to the next box by sending one TAB keystroke
Send($CmdLine[3],1) ; Enter the IP address
For $i=1 to 3 ; Enter TAB 3 times to highlight the login button
 Send("{TAB}")
 Next
 send("{enter}")
 Exit






No comments