Jump to content
nacerbaaziz

question about RequireAdmin

Recommended Posts

nacerbaaziz

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

Share this post


Link to post
Share on other sites
Jos

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 Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
nacerbaaziz

thank you dear to your fastest answer
But I want to Require privileges for the current script
Equivalent to RequireAdmin

Share this post


Link to post
Share on other sites
Jos

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 Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
AdamUL

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

 

  • Like 1

Share this post


Link to post
Share on other sites
nacerbaaziz

Thanks for the replies
greetings to you all

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

  • Similar Content

    • nacerbaaziz
      By nacerbaaziz
      Hi guys
      I have a question about the slider  controle
      There is a problem with screen readers, wen we Using a screen reader We usually move between GUI controls using the tab key.
      But when you create a slider , beside to it there are  buttons and check boxes or any other controls, When I move between the GUI controls using the tab key I can not find the slider.
      But if it were alone in the window, I would find it normally.
      Is there a solution to Solve this problem please?
      This is the code i tried
      #include <GUIConstantsEx.au3>
      #include <WindowsConstants.au3>
      #include <MsgBoxConstants.au3>
      #include <SliderConstants.au3>
      main()
      Func main()
          GUICreate("slider", 200, 200, -1, -1)
      GUICtrlCreateGroup("slider", 10, 10, 240, 50)
          Local $idSlider1 = GUICtrlCreateSlider(50, 30, 200, 20, BitOr($TBS_DOWNISLEFT, $TBS_BOTH))
          GUICtrlSetLimit(-1, 100, 0)
      ;GUIStartGroup("")
          Local $idButton = GUICtrlCreatebutton("Value?", 75, 70, 70, 20)
          GUISetState(@SW_SHOW)
          GUICtrlSetData($idSlider1, 10)
      while 1
      switch GUIGetMsg()
      case $GUI_EVENT_CLOSE
      exit
      case $idButton
                  MsgBox($MB_SYSTEMMODAL, "slider1", GUICtrlRead($idSlider1))
      endSwitch
      Wend
      EndFunc
      [Click and drag to move]
       
       
    • FroVN
      By FroVN
      Hello, i have a problem, first i create two gui , first gui use Fuction Soundplay to play the music, if the music playing in the end, it auto change a new song like playlist on soundcloud, but the problem that while i press a button in gui 1 to open gui 2 , the song when it end it not change to a new song, wait for gui 2 close then begin change a new song, have anyway to make gui 1 still working while gui 2 is working too?
    • RC86
      By RC86
      Morning! I've searched for a definitive answer on the forums on this but can't find one so here goes.  I need admin for one of my functions so I'm using #RequireAdmin.  I then noticed that regardless of that function being used or admin actually being required, the program pops up and requires admin all of the time.
      Is this the way it's designed and is there a way around it so that I can launch my program as normal until admin is required, then and only then prompt the user to run the program as admin?
      The only solution I could think of is to produce 2 executables and do something like:
      $adminrequired = 1 If($adminrequired = 1) Then Run(Run first executable which includes #RequireAdmin) Else Run(Run second identical executable without #RequireAdmin) EndIf Obviously I'd rather keep to making a single executable rather than having 2 or 3!
      Thanks
    • luny
      By luny
      Hello all, this is what I got so far .. it opens and It won't put in words, I need to log in,,  oops didnt put in code. LOL
       
      #include <IE.au3>
      Local $oUser, $oPass, $oSubmit
      Local $sUser = "lit"
      Local $sPass = "abc"
      Local $url = "http://woodstock:9090/login.jsp?url=%2Findex.jsp"
       

      Local $oIE = _IECreate($url, 1)

      _IELoadWait($oIE)
      $oInputs = _IETagNameGetCollection($oIE, "input")
      for $oInput in $oInputs
          if $oInput.type = "text" And $oInput.name = "userid" And $oInput.size = "12" Then $oUser = $oInput
          if $oInput.type = "password" And $oInput.name = "password" And $oInput.size = "12" Then $oPass = $oInput
          if $oInput.type = "submit" And $oInput.value = "loginformtable" Then $oSubmit = $oInput
          if isObj($oUser) And isObj($oPass) And isObj($oSubmit) then exitloop
      Next
      $oUser.value = $sUser
      $oPass.value = $sPass
      _IEAction($oSubmit, "click")
      _IELoadWait($oIE)
       
       
      1  
       
       
    • tremolux66
      By tremolux66
      I've abandoned the FileSelectFolder() approach and rolled my own UDF to create a dialog containing the folder list in a ListView, which seems to work fine. It's also a better fit to our requirements: we don't really want the user wandering around in the folder-selection dialog, plus the UDF displays some associated info for each folder in a second column. Thanks again to the forum members who took a look at this.
      I'm writing an installer script that needs to run as Administrator so it can, e.g., write files into protected directories. The problem is that when I call FileSelectFolder(), there is a 60-second delay before the dialog appears. If I run as an ordinary user (in the Administrators group), there's no delay, but I don't think that will work: for one thing, the installer needs to create a symbolic link, which a member of the Administrators group can't do unless the program is elevated. (This is Win 7 x64.)
      (The installer will be run using an Admin account; the other user accounts are locked down and don't have access to the filesystem, the Start menu, Computer, etc. - it's a turnkey system.)
      Any idea what causes the delay? And is there a way around it?
       
×