Jump to content

Re-run same script with runas admin


Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
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")
Link to comment
Share on other sites

  • 10 months later...

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