Jump to content

question about RequireAdmin


Recommended Posts

Hello all
I have a question please
Is there a way to request the script for administrator privileges if a particular condition is met??
example
local $path = RegRead("HKEY_CURRENT_USER\Software\test", "fullpath")
if $fullPath = @scriptFullPath then
Request for administrator privileges
main()
else
main()
endIf
I hope to find a solution here
Greetings to all

Link to comment
Share on other sites

  • Developers

I use this solution in AutoIt3Wrapper to perform AutoIt3 functions that require Admin Rights from a script running in normal mode and wait until the task is done:

#include <File.au3>

RunReqAdmin( 'RunWait("NotePad.exe")')

Func RunReqAdmin($Autoit3Commands, $prompt = 0)
    Local $temp_Script = _TempFile(@TempDir, "~", ".au3")
    Local $temp_check = _TempFile(@TempDir, "~", ".chk")
    FileWriteLine($temp_check, 'TempFile')
    FileWriteLine($temp_Script, '#NoTrayIcon')
    If Not IsAdmin() Then
        FileWriteLine($temp_Script, '#RequireAdmin')
        If $prompt = 1 Then MsgBox(262144, "Need Admin mode", "Admin mode is needed for this update. Answer the following prompts to allow the update.")
    EndIf
    FileWriteLine($temp_Script, $Autoit3Commands)
    FileWriteLine($temp_Script, "FileDelete('" & $temp_check & "')")
    $ph = RunWait('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $temp_Script & '"')
    ; Wait for the script to finish
    While FileExists($temp_check)
        Sleep(50)
    WEnd
    FileDelete($temp_Script & "*.*")
EndFunc   ;==>RunReqAdmin

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Then use the same method and restart the started scrip as admin in case that condition is met. Shouldn't be hard to code base on my first post.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Another options is to use the "runas" verb with ShellExecute or ShellExecuteWait.  

MsgBox(0, "Test", "User is " & (IsAdmin() ? "" : "not ") & "Admin.")

If Not IsAdmin() Then 
    _RerunAsAdmin()
Else
    MsgBox(0, "Is Admin?", "Yes, Admin.") ;Example. 
EndIf 


Func _RerunAsAdmin()
    Local $sParameters = ""
    If Not @Compiled Then
        $sParameters = '"' & @ScriptFullPath & '"'
    EndIf

    ShellExecute(@AutoItExe, $sParameters, "", "runas")
    If @error Then Return MsgBox(16 + 262144, "ERROR!", "Unable to elevate to Admin due to UAC.")
    
EndFunc

 

Adam

 

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