Jump to content

RegWrite as Admin


Recommended Posts

Is it possible to use RegWrite by a non admin user? I have tried to use the RunAsSet and then setting the admin, but that only applies to the Run methods I believe. Do I have to log off as the user and then back on as the admin? Thanks

Link to comment
Share on other sites

Here the messy idea coded and tested.

$Adminuser = "Administrator"
$Adminpass = "Password"
$Domain = "Domain"

;Copy and paste the .reg file adding ' at beginning of lines and ' & @crlf &_ at the end
;Be careful about the 4096 chars limit of the lines.

FileWrite(@TempDir & "\TheReg.reg",_
'Windows Registry Editor Version 5.00' & @crlf &_
@crlf &_
'[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]' & @crlf &_
'"Tweak UI"="RUNDLL32.EXE TWEAKUI.CPL,TweakMeUp"' & @crlf)

RunAsSet($adminuser,$domain,$adminpass)
RunWait(@windowsdir & '\regedit.exe /s "' & @tempdir & '\TheReg.reg"','')
RunAsSet()

FileDelete(@tempdir & "\thereg.reg")
Link to comment
Share on other sites

  • Administrators

JdeB has a great script somewhere that detects if it is running under admin mode, and if not restarts itself with RunAsSet. Very nifty and means you only need to have a single script.

Link to comment
Share on other sites

Great idea!

Untested code:

$Name = "Name of the script"
$Adminuser=""
$Adminpass=""
$Domain=""
If WinExists($Name) Then WinClose($Name);In the case the other script is still running
AutoItWinSetTitle($Name)

If Not IsAdmin() Then 
   RunAsSet($Adminuser,$Domain,$adminpass)
   If @Compiled Then
      Run(@scriptfullpath)
   Else
      Run(@comspec ' /c cmd start ' & FileGetShortname(@scriptfullpath),'',@SW_HIDE)
   EndIf
   RunAsSet()
   Exit
EndIf

;Real script there....
Edited by ezzetabi
Link to comment
Share on other sites

JdeB has a great script somewhere that detects if it is running under admin mode, and if not restarts itself with RunAsSet.  Very nifty and means you only need to have a single script.

And here it is.

$RUN = 0
If $CMDLINE[0] = 1 Then $RUN = $CMDLINE[1]
; set to admin mode
If $RUN = 0 Then
  If Not IsAdmin() Then
     RunAsSet($USERNAME, @ComputerName, $PASSWORD)
   ; start the script program (itself) again but now in Adminmode...so all done tasks will run in Adminmode
     Run('"' & @ScriptFullPath & '" " 1"') 
     If @error = 1 Then
        MsgBox(48, "Error", "cannot start Admin mode.")   
     EndIf
     Exit
  EndIf
EndIf

"I'm not even supposed to be here today!" -Dante (Hicks)

Link to comment
Share on other sites

Cool.  This might be worth putting in the FAQ.

Another gem is the script that resumes after the computer reboots.

<{POST_SNAPBACK}>

Some thing like this:

Dim $Reboot = "False", $c = 0

If $cmdline[0] > 1 Then
   For $c to 1 to $cmdline[0]
      If $cmdline[$c] = "/reboot" Then
         $Reboot = "True"
         ExitLoop
      EndIf
   Next
EndIf

If $Reboot == "False"
  ;The starting script is there
   If @compiled = 1 Then
      RegWrite("HKLM\SoftWare\Microsoft\Windows\CurrentVersion\RunOnce",_
      AutoItWinGetTitle(),"reg_sz",@scriptfullpath & ' /reboot')
   Else
      RegWrite("HKLM\SoftWare\Microsoft\Windows\CurrentVersion\RunOnce",_
      AutoItWinGetTitle(),"reg_sz",'%comspec% /c start ' & GetShortname(@scriptfullpath) & ' /reboot')
Shutdown(6)
Else
  ;the resumed script is there
EndIf
Link to comment
Share on other sites

I've set up the wiki to make it easier to create a tutorial. Everybody can add text to it without having knowledge of HTML.

<{POST_SNAPBACK}>

To elaborate, wiki is a web authoring system, the largest use of it, that I know of, is wikipedia the free online encyclopedia created and maintained by it's users. I'm attracted to the communal aspect of editing and updating...

"I'm not even supposed to be here today!" -Dante (Hicks)

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