Sign in to follow this  
Followers 0
dobbelina

Re-run same script with runas admin

7 posts in this topic

#1 ·  Posted (edited)

Hi everyone!

Long time since i did some Autoit stuff, but now i'm hooked again by this

fantastic little script engine.

I saw a few threads about re-running the same script with admin credentials.

Similar to this:

Local $user = "Administrator"
Local $pass = "Password"

If Not IsAdmin() Then
    RunAs($user, @ComputerName, $pass, 0, @ScriptName, @ScriptDir)
    Exit
EndIf
MsgBox(48, "Success!", "I Am Admin")

But it doesn't work, at least not on my Windows 7 rig?

Substituting @ScriptName with "secondary.bat" works, but is not as sexy as above.

One can always execute external bat or cmd files, and then do

the shuffling via temp folder.

But wouldn't it be neat to to be able to re-run the same script as admin.

Comments...

Edited by dobbelina

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

That doesn't make much sense to me. Are you trying to run a .au3 or .exe?

.au3 files aren't executable so you need to use the @AutoItExe in that case.

Edited by AdmiralAlkex

Share this post


Link to post
Share on other sites

Ahh, okay.

Probably wishfull thinking then from my part.

I saw a thread where a guy claimed he did it this way.

I'll just let it run external files with admin cred then.

Thanks for replying :(

Share this post


Link to post
Share on other sites

Uhh, wait now, i just had my coffe.

So using @AutoItExe instead of @ScriptName work you mean ?

(I don't have my own computer in front of me)

Share this post


Link to post
Share on other sites

This may work as untested. It will restart whether compiled or not.

Local $user = "Administrator"
Local $pass = "Password"

; test passed switch to prevent multiple executions if something goes weird
If Not IsAdmin() And Not $CMDLINE[0] Then
    RunAs($user, @ComputerName, $pass, 0, _
            '"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & @ScriptFullPath & '" /restart', _
            @ScriptDir)
    Exit
EndIf
MsgBox(48, "Success!", "I Am Admin")

Share this post


Link to post
Share on other sites

Wow !

That works, and i don't need to compile all the time for testing to.

Big thanks! , you guys rock :(

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Since I have to be able to manage a great number of computers with different admin accounts (four) on each one, can this script be modified in a way, that:

- it checks IsAdmin(), if not, restart as adminuser1

- it checks IsAdmin(), if not, restart as adminuser2

- it checks IsAdmin(), if not, restart as adminuser3

- it checks IsAdmin(), if not, restart as adminuser4

- in case of fail, close script.

Tried with this script. Works ok on Win XP, fails in Win 7

#include<Array.au3>

$countFile = (@ScriptDir & "\count.txt")
$countFileCopy = (@ScriptDir & "\count_copy.txt")

Local $userArray[5]
$userArray[0] = ""
$userArray[1] = "admin1"
$userArray[2] = "admin2"
$userArray[3] = "admin3"
$userArray[4] = "admin4"

Local $passArray[5]
$passArray[0] = ""
$passArray[1] = "pass1"
$passArray[2] = "pass2"
$passArray[3] = "pass3"
$passArray[4] = "pass4"

If Not FileExists($countFile) Then ; if countfile dont exist, create it
    FileOpen($countFile, 1) 
    $count = 0
    FileWrite($countFile, $count)
    FileClose($countFile)
    FileOpen($countFile, 0)
    $count = FileReadLine($countFile, 1) 
    FileClose($countFile)
Else ; if countfile exists, read it
    FileOpen($countFile, 0)
    $count = FileReadLine($countFile, 1)
    If $count > 5 Then
        MsgBox(0, "ERROR", "No more passwords")
        Exit
    EndIf

EndIf


If Not IsAdmin() And Not $CMDLINE[0] Then
    MsgBox(0, $count, "NOT ADMIN", 5)

    RunAs($userArray[$count], @ComputerName, $passArray[$count], 0, _
            '"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & @ScriptFullPath & '" /restart', _
            @ScriptDir)
            $count = $count + 1
            FileWrite($countFile, $count)
            FileClose($countFile)
    Exit
EndIf

;check if restart was succesful
If IsAdmin() Then
    MsgBox(0, $userArray[$count], "IS ADMIN", 3)
Else
    MsgBox(0, $userArray[$count], "IS NOT ADMIN", 3)
    Exit
EndIf

Run('whatever.exe')

FileClose($countFile)
FileCopy($countFile, $countFileCopy)
FileDelete($countFile)

Exit
Edited by neebo

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