Jump to content

Recommended Posts

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
Posted

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 :(

Posted

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)

Posted

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")
  • 10 months later...
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

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
×
×
  • Create New...