Jump to content

AutoIT script stops working when compiled ( does not run batch file as admin )


Go to solution Solved by adrianpmartinez,

Recommended Posts

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:

#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 by adrianpmartinez
Better title
Link to comment
Share on other sites

  • adrianpmartinez changed the title to AutoIT script stops working when compiled ( does not run batch file as admin )
Link to comment
Share on other sites

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.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

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

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.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...