Jump to content

Runas failing with regedit.exe


Recommended Posts

;Add RunOnce into registry while standard user is still logged in. This is required since standard users do not have write access to RunOnce Keys

    $RegFile=(@ScriptDir & "\RunOnce" & ".reg")
    $sString='"Install"' & "=" & '"' & @ScriptDir & "\Install.exe" & '"'
    Fileopen($RegFile,1)
    FileWriteLine ($RegFile,"REGEDIT4")
    FileWriteLine ($RegFile,"[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce]")
    $sString=StringReplace ($sString, "\", "\\")
    FileWriteLine($RegFile,$sString)
    FileClose($RegFile)
    Runas("ADMIN","DOMAIN","ADMINPASSWORD",4,@WindowsDir & "\regedit.exe /s " & $RegFile)
        ;Sleep(1000) This would cause the Runas to fail, as will any other command.

My Goal:

To create a runonce key in the registry while logged in as standard user on a XP machine. The only way I could get it to successfully work is by creating a reg.file then performing a RUNAS of regedit to import the .reg file.

My script works just fine as-is. However, if I place any other commands after the RUNAS, the RUNAS will fail.

Any suggestions as to what would cause this would greatly be appreciated.

Thanks!!!

Link to comment
Share on other sites

If you un-comment the Sleep(), exactly how does it fail? There's no way the Sleep() is causing it to fail, but without it or anything else coming after, your script is exiting as soon as the RunAs() kicks off, so how do you know it worked?

One thing to check is that your path to $RegFile is available to the "ADMIN" user, and properly quoted if there are any spaces in it:

$PID = Runas('ADMIN', 'DOMAIN', 'ADMINPASSWORD', 4, @WindowsDir & '\regedit.exe /s "' & $RegFile & '"')
MsgBox(64, "Started", "PID = " & $PID)

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

If you un-comment the Sleep(), exactly how does it fail? There's no way the Sleep() is causing it to fail, but without it or anything else coming after, your script is exiting as soon as the RunAs() kicks off, so how do you know it worked?

One thing to check is that your path to $RegFile is available to the "ADMIN" user, and properly quoted if there are any spaces in it:

$PID = Runas('ADMIN', 'DOMAIN', 'ADMINPASSWORD', 4, @WindowsDir & '\regedit.exe /s "' & $RegFile & '"')
MsgBox(64, "Started", "PID = " & $PID)

:mellow:

If I un-comment the Sleep, the script will still run completely,no errors, but the Runas does not seem to execute. I can run the script as-is and it will import the .reg file just fine. So I know the "ADMIN" user has rights and is properly quoted. But commands (besides Exit) after the Runas, and the .reg won't import.
Link to comment
Share on other sites

That just doesn't make any sense.

Have you tried brandishing a screwdriver threateningly in front of the monitor?

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Tell me about it! I tried scowling and sighing loudly to no avail.

I showed my co-worker, he just scrathed his head, hrmmphed and walked away.

Runas("admin","domain","password",4,@WindowsDir & "\regedit.exe") ; /s " & $RegFile)
    sleep(1000)

works

Runas("admin","domain","password",4,@WindowsDir & "\regedit.exe /s " & $RegFile)
    sleep(1000)

doesn't work

Runas("admin","domain","password",4,@WindowsDir & "\regedit.exe /s " & $RegFile)
    ;sleep(1000)

works

:mellow:

Edited by mtmartis
Link to comment
Share on other sites

Not opening and not closing file open handles correct can lead to strange behavior. Your script is the case of over looking this concern which is a trap that we may all suffer from once and a while.

I made corrections and changed RunAs to RunAsWait as RunAsWait seems suitable for the task.

$RegFile = (@ScriptDir & "\RunOnce" & ".reg")
$sString = '"Install"' & "=" & '"' & @ScriptDir & "\Install.exe" & '"'
$hFileWrite = FileOpen($RegFile, 1);; Get the handle for file write
If $hFileWrite <> -1 Then;; Test if handle is valid to use
    FileWriteLine($hFileWrite, "REGEDIT4")
    FileWriteLine($hFileWrite, "[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce]")
    $sString = StringReplace($sString, "\", "\\")
    FileWriteLine($hFileWrite, $sString)
    FileClose($hFileWrite);; close handle (not filename)
    RunAsWait("ADMIN", "DOMAIN", "ADMINPASSWORD", 4, '"' & @WindowsDir & '\regedit.exe" /s "' & $RegFile & '"'); Use wait
    ;Sleep(1000) This would cause the Runas to fail, as will any other command.;; Do you need this?
Else
    ;; unable to open file write handle so add error handling here
EndIf

Another item to note that the HKLM RunOnce key can accept just the path of the file ommitting the "Install"= part of the string.

See if those changes shown above help. :mellow:

Link to comment
Share on other sites

Not opening and not closing file open handles correct can lead to strange behavior. Your script is the case of over looking this concern which is a trap that we may all suffer from once and a while.

I made corrections and changed RunAs to RunAsWait as RunAsWait seems suitable for the task.

$RegFile = (@ScriptDir & "\RunOnce" & ".reg")
$sString = '"Install"' & "=" & '"' & @ScriptDir & "\Install.exe" & '"'
$hFileWrite = FileOpen($RegFile, 1);; Get the handle for file write
If $hFileWrite <> -1 Then;; Test if handle is valid to use
    FileWriteLine($hFileWrite, "REGEDIT4")
    FileWriteLine($hFileWrite, "[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce]")
    $sString = StringReplace($sString, "\", "\\")
    FileWriteLine($hFileWrite, $sString)
    FileClose($hFileWrite);; close handle (not filename)
    RunAsWait("ADMIN", "DOMAIN", "ADMINPASSWORD", 4, '"' & @WindowsDir & '\regedit.exe" /s "' & $RegFile & '"'); Use wait
    ;Sleep(1000) This would cause the Runas to fail, as will any other command.;; Do you need this?
Else
    ;; unable to open file write handle so add error handling here
EndIf

Another item to note that the HKLM RunOnce key can accept just the path of the file ommitting the "Install"= part of the string.

See if those changes shown above help. :mellow:

That seemed to do the trick. Thanks! And your questioning of the need for the Sleep command. Nope, I don't need it, I was just using it as a test command. I have the Shutdown command in the working script. :P

Edited by mtmartis
Link to comment
Share on other sites

Not opening and not closing file open handles correct can lead to strange behavior...

Opened the file, then didn't use the handle. Totally missed that. Ouch.

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...