Sign in to follow this  
Followers 0
Docfxit

RunAs not working

3 posts in this topic

                The script  should call the same script and run as user SCANS
                    The current user is running as administrator
                    The $User to runas also runs as administrator
                    The MsgBox after RunAs shows there is no error

Why isn't the RunAs working?

 

#RequireAdmin
#include <Array.au3>
#include <security.au3> ; Get OWNER from SID.

AutoItSetOption('TrayIconDebug', 1)
Global $User = "SCANS" ; This is the user that will run on remote PC's
;Global $IPAddressFound, $x ; Common
$tag_WTS_PROCESS_INFO= _
  "DWORD SessionId;" & _
  "DWORD ProcessId;" & _
  "PTR pProcessName;" & _
  "PTR pUserSid"

;This will list the user the process is running as
MsgBox(0,"Process by name or number(PID)", "'Owner' of " & @AutoItExe & " is " & _ProcessListOWNER_WTS("AutoIt3.Exe"))

If _ProcessListOWNER_WTS("AutoIt3.Exe") <> $User Then ;Check for power user
    $AdminPass = InputBox("Password Required", "Please enter the password for user " & @CRLF & @CRLF & $User, "", "*")
    Select
        Case @error = 0 ;OK - The string returned is valid
;################################ Problem Description ##########################################
                ;After the next statement, This script is still running as the current user logged in
                ;It should run as user SCANS
                ;   The current user is running as administrator
                ;   The $User to runas also runs as administrator
                ;   The MsgBox after RunAs shows there is no error
;################################ END Problem Description ##########################################
                RunAs($User, @ComputerName, $AdminPass, 1, '"' & @AutoItExe & '" "' & @ScriptFullPath & '"')
                $RunAsError = @error
                MsgBox(0, "", "RunAs = " & $User & ", "  & @ComputerName & ", "  & $AdminPass & ", " & "1" & ", "  & '"' & @AutoItExe & '" "' & @ScriptFullPath & '"' & @CRLF & @CRLF & "$RunAsError = " & $RunAsError)
                ;            If StringInStr(@ScriptFullPath, '.au3') Then
                ;                Run('"' & @AutoItExe & '" "' & @ScriptFullPath & '" ' & $AdminPass)
        Case @error = 1 ;The Cancel button was pushed
            MsgBox(0, "Error1", "The Cancel button was pushed" & @CRLF & "The program will exit")
            Exit
    EndSelect
Else
MsgBox(0, "", "The user is correct")
EndIf

; ############ Show the user a process is running as ####################
Func _ProcessListOWNER_WTS($PID=0)
    Local $i, $ret, $ret1, $mem
    $ret=DllCall("WTSApi32.dll", "int", "WTSEnumerateProcesses", "int", 0, "int", 0, "int", 1, "ptr*", 0, "int*", 0)
    Local $array[$ret[5]][4]
    $mem=DllStructCreate($tag_WTS_PROCESS_INFO,$ret[4])
    for $i=0 to $ret[5]-1
        $mem=DllStructCreate($tag_WTS_PROCESS_INFO, $ret[4]+($i*DllStructGetSize($mem)))
        ;if DllStructGetData($mem, "pProcessName") Then
            $string=DllStructCreate("char[256]", DllStructGetData($mem, "pProcessName"))
            $array[$i][0]=DllStructGetData($string,1)
        ;EndIf
        $array[$i][1]=DllStructGetData($mem, "ProcessId")
        $array[$i][2]=DllStructGetData($mem, "SessionId")
        ;if DllStructGetData($mem, "pUserSid") Then
            $ret1 = _Security__LookupAccountSid(DllStructGetData($mem, "pUserSid"))
            if IsArray($ret1) Then $array[$i][3]=$ret1[0]
        ;EndIf
    Next

    DllCall("WTSApi32.dll", "int", "WTSFreeMemory", "int", $ret[4])

    if $PID Then
        if IsInt($PID) Then
            for $i=0 to UBound($array, 1) - 1
                if $array[$i][1] = $PID Then
                    Return $array[$i][3]
                EndIf
            Next
        Else
            for $i=0 to UBound($array, 1) - 1
                if $array[$i][0] = $PID Then
                    Return $array[$i][3]
                EndIf
            Next
        EndIf
    EndIf

    Return $array
EndFunc

Thank you,

Docfxit

Share this post


Link to post
Share on other sites



Hey DocFixIt,

Try this:

RunAs($User, @ComputerName, $AdminPass, 1, '"' & @AutoItExe & ' /AutoIt3ExecuteScript" "' & @ScriptFullPath & '"')

I haven't tested it, But this parameter is required to run AU3 files using a compiled script, So i assume the same applies here.

Cheers

Javi


give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.

Share this post


Link to post
Share on other sites

Thanks for the reply...

 

I tried your suggestion and compiled it.  It didn't run with the new RunAs user name.

I tried your suggestion and didn't compile it.  It gives me a RunAs error 1

Originally I wasn't running it compiled and was not getting a RunAs error.

Thanks,

Docfxit

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