nacerbaaziz Posted July 9, 2018 Posted July 9, 2018 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
Developers Jos Posted July 9, 2018 Developers Posted July 9, 2018 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.
nacerbaaziz Posted July 9, 2018 Author Posted July 9, 2018 thank you dear to your fastest answer But I want to Require privileges for the current script Equivalent to RequireAdmin
Developers Jos Posted July 9, 2018 Developers Posted July 9, 2018 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.
AdamUL Posted July 9, 2018 Posted July 9, 2018 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 nacerbaaziz 1
nacerbaaziz Posted July 10, 2018 Author Posted July 10, 2018 Thanks for the replies greetings to you all
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