Sign in to follow this  
Followers 0
hcstarck

RegWrite as Admin

16 posts in this topic

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

Share this post


Link to post
Share on other sites



If anything else fail you can always create a .reg file on the fly with FileWrite() and Run('regedit /s thatfile.reg') after setting Runasadmin and finally delete the reg.

Share this post


Link to post
Share on other sites

Interesting solution. Anything thats not quite that messy? I'll get starting on that in the meantime though, thanks.

Share this post


Link to post
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")

Share this post


Link to post
Share on other sites

Thanks ezzetabi. Thats roughly what I came up with. I just forgot to use the crlf in the keys so the reg wasn't running,

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

Mine looks better...

Share this post


Link to post
Share on other sites

Mine looks better...

well, maybe you'd want to add it to the wiki?

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

well, maybe you'd want to add it to the wiki?

<{POST_SNAPBACK}>

What is that?!?

Share this post


Link to post
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.

Share this post


Link to post
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)

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  
Followers 0