Jump to content
Sign in to follow this  
JoymanyMahn

Running Script Contents in Administrator Mode

Recommended Posts

JoymanyMahn

I know how run a particular task in AutoIt as an Admin using RunAsSet, but I can't find a way to run an entire script.

For example, I would like for Non-Admin users to be able to launch an AutoIt executable file that modifies two entries in the HKLM part of the Registry. The following works--but obviously only in Admin mode. Any assistance would be appreciated.

CODE

$Reg01_keyname = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management"

$Reg01_valuename = "DisablePagingExecutive"

$Reg01_type = "REG_DWORD"

$Reg01_value = "00000001"

RegWrite ( $Reg01_keyname, $Reg01_valuename, $Reg01_type, $Reg01_value)

If @error Then

MsgBox(4096,"", "Error: " & $Reg01_keyname)

Else

MsgBox(4096, "Result for " & $Reg01_valuename, "Successfully wrote value: " & $Reg01_value & _

@LF & "To name: " & $Reg01_valuename & _

@LF & "Under key: " & $Reg01_keyname)

EndIf

;===Force Windows to Unload DLLs from Memory

$Reg02_keyname = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AlwaysUnloadDLL"

$Reg02_valuename = ""

$Reg02_type = "REG_SZ"

$Reg02_value = "1"

RegWrite ( $Reg02_keyname, $Reg02_valuename, $Reg02_type, $Reg02_value)

If @error Then

MsgBox(4096,"", "Error: " & $Reg02_keyname)

Else

MsgBox(4096, "Result for " & $Reg02_valuename, "Successfully wrote value: " & $Reg02_value & _

@LF & "To name: " & $Reg02_valuename & _

@LF & "Under key: " & $Reg02_keyname)

EndIf

Share this post


Link to post
Share on other sites
Tripredacus

I had this problem also, so I made 2 scripts. One is the one that does things (like edit the registry). The other is a wrapper that executes the actual app.

Dim $Username, $Password
$Username = "Administrator"
$Password = "Password"
RunAsSet ( $Username, @Computername, $Password )
RunWait ( "registry.exe" )

The program "registry.exe" is a separate script that actually has your code in it that you posted.

Share this post


Link to post
Share on other sites
Jos

Just restart the script with admin credentials ..

$USERNAME = "Administrator"
$PASSWORD = "Secret"
$RUN = 0       ; run indicator 
; retrieve the cycle from commandline
If $CMDLINE[0] = 1 Then $RUN = $CMDLINE[1]
If $RUN = 0 Then
   RunAsSet($USERNAME, @ComputerName, $PASSWORD)
   Run('"' & @ScriptFullPath & '" " 1"') 
   If @error Then MsgBox(4096+32,"Error", "Error starting under admin mode")
   Exit
EndIf
; commands go here that require Administrator rights

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
JoymanyMahn

Thank you so much JdeB! This worked perfectly!

Share this post


Link to post
Share on other sites
Tripredacus

Just restart the script with admin credentials ..

$USERNAME = "Administrator"
$PASSWORD = "Secret"
$RUN = 0       ; run indicator 
; retrieve the cycle from commandline
If $CMDLINE[0] = 1 Then $RUN = $CMDLINE[1]
If $RUN = 0 Then
   RunAsSet($USERNAME, @ComputerName, $PASSWORD)
   Run('"' & @ScriptFullPath & '" " 1"') 
   If @error Then MsgBox(4096+32,"Error", "Error starting under admin mode")
   Exit
EndIf
; commands go here that require Administrator rights
I guess that would work too, but its really the same thing I posted except you added a messagebox. Edited by Tripredacus

Share this post


Link to post
Share on other sites
herewasplato

...but its really the same thing I posted...

JdeB's method does not require two scripts.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites
Jos

I guess that would work too, but its really the same thing I posted except you added a messagebox.

Principal is the same but a lot easier to accomplish what the op wanted I would say ... :)

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
Tripredacus

JdeB's method does not require two scripts.

OH I see I did not read the comment on the last line. I will have to try this in the future.

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
Sign in to follow this  

×