adrianpmartinez Posted December 14, 2023 Share Posted December 14, 2023 (edited) Hi everyone, I am new to AutoIT. I tried creating a script for the following task: 1. First, executes install_collectors_requirements.bat file in specified folder, and sends enter key when "pause" command is read from cmd window ( moment when the execution of .bat file ends) 2. Then, it executes collectors_run.bat file with administrator previleges. I used a function i found here in this forum (_ShellExecuteWaitAsAdmin). When I run the script, everything works fine. However, when I compile the script to a .exe file, the part that requires administrator previleges doesnt work anymore. Here are the options I used to compile the .exe file: Output Arch: Compile x64 version Add required Constants *.au3 to your script RequestedExecutionLevel: requireAdministrator (Other options are default) Here is the code: expandcollapse popup#RequireAdmin #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=performance.ico #AutoIt3Wrapper_Outfile=run.exe #AutoIt3Wrapper_Outfile_x64=run.exe #AutoIt3Wrapper_Compression=0 #AutoIt3Wrapper_Change2CUI=y #AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator #AutoIt3Wrapper_Add_Constants=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <AutoItConstants.au3> #include <MsgBoxConstants.au3> #include <WinAPIError.au3> ;Function to execute batch file as administrator Func _ShellExecuteWaitAsAdmin($sUserName, $sDomain, $sPassword, $iLogonFlag, $sFile, $sParamters = "") Local $sCmd = ' /AutoIt3ExecuteLine "ShellExecuteWait(''' & $sFile & ''', ''' & $sParamters & ''', '''', ''runas'')"' Return RunAsWait($sUserName, $sDomain, $sPassword, $iLogonFlag, @AutoItExe & $sCmd) EndFunc ;Executing dependency installation file Local $programPath = "C:\Program Files\Prometheus\requirements" Local $batchFileName = "install_collectors_requirements.bat" Local $cmdApp = Run('cmd /c "cd /d "' & $programPath & '" && ' & $batchFileName & '"', "", @SW_SHOW, $STDOUT_CHILD + $STDERR_CHILD) WinWaitActive("[CLASS:ConsoleWindowClass]") ; cmd window Sleep(500) ; Allow time for the cmd window to activate Local $data While True $data &= StdoutRead($cmdApp) ; Reading cmd console data If StringInStr($data, 'C:\Program Files\Prometheus\requirements>pause') Then ; Send Enter key when installation is complete Send('{ENTER}') ExitLoop EndIf Sleep(200) WEnd ; Debug MsgBox (shows stdout content from cmd console) ; MsgBox(0,"Debug",$data) ; Change the username and password to the appropriate values for your system. Local $sUserName = "user" Local $sPassword = "pass" Local $sDirectory = "C:\Program Files\Prometheus\" Local $sFiletoRun = "collectors_run.bat" $iReturn = _ShellExecuteWaitAsAdmin($sUserName,@ComputerName, $sPassword, 0, $sDirectory & $sFileToRun) If @error Then Local $sLastError = _WinAPI_GetLastErrorMessage() Local $iExtendedError = @extended MsgBox($MB_SYSTEMMODAL + $MB_ICONERROR, "Error", "Batch file has not Run :" & @CRLF & @CRLF & $sLastError & @CRLF & "Extended Error Code: " & $iExtendedError) EndIf Sorry if this is a basic question, and thanks for your attention. Edited December 14, 2023 by adrianpmartinez Better title Link to comment Share on other sites More sharing options...
Nine Posted December 14, 2023 Share Posted December 14, 2023 Hard to say without being able to run your script ourselves, but at first glance, you seem to be running uncompiled at 32bits, while compiled at 64bits... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
adrianpmartinez Posted December 14, 2023 Author Share Posted December 14, 2023 Hi Nine, thanks for the reply. After your comment, I tried this option also: Output Arch: Compile x86 version. (default) Add Required Constants *.au3 to your script RequestedExecutionLevel: requireAdministrator However, the result was the same. Is this what you meant? To try to compile as 32bit? Link to comment Share on other sites More sharing options...
argumentum Posted December 14, 2023 Share Posted December 14, 2023 so you made a batch file to do stuff for https://prometheus.io/download/ ? I would add to the task scheduler to run the batch file as admin and forget the AutoIt script. Or move the batch file functionality to an AutoIt script. My 2 cents if am guesstimating right. Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
Nine Posted December 14, 2023 Share Posted December 14, 2023 (edited) You need to add : #pragma compile (AutoItExecuteAllowed, True) at the beginning of your script Edited December 15, 2023 by Nine SkysLastChance 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
argumentum Posted December 14, 2023 Share Posted December 14, 2023 3 hours ago, adrianpmartinez said: /AutoIt3ExecuteLine there's no need for that. Just RunAs the exe. Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
adrianpmartinez Posted December 15, 2023 Author Share Posted December 15, 2023 20 hours ago, argumentum said: so you made a batch file to do stuff for https://prometheus.io/download/ ? I would add to the task scheduler to run the batch file as admin and forget the AutoIt script. Or move the batch file functionality to an AutoIt script. My 2 cents if am guesstimating right. The first batch file installs some python dependencies on the machine. The second one runs many Prometheus exporters at once. It needs admin previleges to free network ports, if needed. Yeah, I am considering to move the batch files functionality to the script, since I didnt have any success yet. 20 hours ago, Nine said: You need to add : #pragma compile (AutoItExecuteAllowed, True) at the beginning of your script I added this to the first line of the code. Didnt seem to have any effect. The execution of .exe file is going like this: First, I get the UAC prompt. Then , cmd window opens and executes the first batch file. Then, UAC prompt appears again, but it appears the same part of the code runs again. I suspect this because when I uncomment this line of code: ; MsgBox(0,"Debug",$data) This MsgBox appears three times. After cmd closes and reopens these three times, script execution ends. I can record a video to show you if you want. When running this script directly with AutoIT , i get the UAC prompt with the AutoIT icon, first batch executes, then i get UAC prompt with CMD icon, and second batch executes. 20 hours ago, argumentum said: there's no need for that. Just RunAs the exe. I tried using only RunAs function first, but I couldnt make it work. I read somewhere in this forum that RunAs function doesnt elevate the program to all admin rights. Not sure if thats correct, because like I said, I am very unexperiencied in autoit. This function (_ShellExecuteWaitAsAdmin) worked for me , so I prefer to not alter it by myself if its working haha Link to comment Share on other sites More sharing options...
Nine Posted December 15, 2023 Share Posted December 15, 2023 When you execute run.exe without #pragma, only one UAC should be displayed (it is the #RequireAdmin one). With #pragma in run.exe, a second UAC should be displayed proving that the #pragma is working as intended. Now if the bat file executed by the RunAsWait does not work correctly, you should simplify the bat file to a simple echo, and increase its complexity till you find where the problem is. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Solution adrianpmartinez Posted December 18, 2023 Author Solution Share Posted December 18, 2023 After some days of struggle, I followed argumentum suggestion and just implemented the work done in the .bat files in autoit. Even though I was curious of why my problem happened, my problem is solved. Thanks anyways for the reply guys. argumentum 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now