Jump to content

RunAs, ProcessEx UDF and returned errorlevels - (Moved)


Recommended Posts


I've found and used @TheDcoder's ProcessEX UDF, and have found it and invaluable tool in my scripting arsenal. Recently, I found myself needing to create a script which then attempts to run another program as a different user. I was able to heavily borrow from the _Process_RunCommand function to create _Process_RunAsCommand:

; #FUNCTION# ====================================================================================================================
; Name ..........: _Process_RunAsCommand
; Description ...: Runs a command or an executable under a different user security privilege.
; Syntax ........: _Process_RunAsCommand($iMode, $sUserName, $sUserPass, $sUserDomain, $sExecutable [, $sWorkingDir = @TempDir [, $iRunOptFlag = $STDERR_MERGED]])
; Parameters ....: $iMode               - Mode in which this function should operate, See Remarks.
;                  $sUserName           - User name under which you would like to run the command/executable.
;                  $sUserPass           - Password for $sUserName.
;                  $sUserDomain         - Domain name to which the $sUserName belongs.
;                  $sExecutable         - The command to run/execute (along with any arguments).
;                  $sWorkingDir         - [optional] The working directroy for the command. Default is @TempDir. $sUserName must have
;                                         privileges to create/modify files on this directory.
;                  $iRunOptFlag         - [optional] The Opt flag for the Run function. Default is $STDERR_MERGED.
; Return values .: Success: Mode $PROCESS_RUN     : Will return the process handle & @extended will contain the PID of the command
;                           Mode $PROCESS_RUNWAIT : Will return the output & @extended will contain the exit code for the function
;                  Failure: Will return False & @error will contain:
;                           1 - If the $iMode flag is invalid
;                           2 - If the command is invalid
; Author ........: J. Sanchez, heavily borrowing from code by TheDcoder
; Modified ......: N/A
; Remarks .......: 1. The ONLY valid modes are: $PROCESS_RUN & $PROCESS_RUNWAIT
;                     $PROCESS_RUN     : Will act similarly to Run function, See Return values
;                     $PROCESS_RUNWAIT : Will act similarly to RunWait function, See Return values
;                     If you use $PROCESS_RUN then use _Process_GetExitCode to get the exit code & use StdoutRead to get the output of the command
;                  2. Use $PROCESS_COMMAND to run commands like this: $PROCESS_COMMAND & "ping"
;                  3. Add $PROCESS_DEBUG to $iMode to automagically debug the command, $PROCESS_RUN is equivalent to $PROCESS_RUNWAIT in this case
; Related .......: RunAs, RunWait
; Link ..........: http://bit.ly/ProcessUdfForAutoIt
; Example .......: Yes, see example.au3
; ===============================================================================================================================; Functions
Func _Process_RunAsCommand($iMode, $sUserName, $sUserPass, $sUserDomain, $sExecutable, $sWorkingDir = @TempDir, $iRunOptFlag = $STDERR_MERGED)
    Local $iExitCode = 0 ; Declare the exit code variable before hand
    Local $sOutput = "" ; Declare the output variable before hand
    Local $bDebug = False ; Declare the debug variable before hand
    If BitAND($iMode, $PROCESS_DEBUG) Then $bDebug = True
    If BitAND($iMode, $PROCESS_RUN) Then
        $iMode = $PROCESS_RUN
    ElseIf BitAND($iMode, $PROCESS_RUNWAIT) Then
        $iMode = $PROCESS_RUNWAIT
        Return SetError(1, 0, False)
    ; If Not $iMode = $PROCESS_RUN Or Not $iMode = $PROCESS_RUNWAIT Then Return SetError(1, 0, False) ; If the mode is invalid...
    ;Local $iPID = Run($sExecutable, $sWorkingDir, @SW_HIDE, $iRunOptFlag) ; Run!!! :P
    Local $iPID = RunAs($sUserName,$sUserDomain,$sUserPass,BitAND(0,4),$PROCESS_COMMAND & " " & $sExecutable,$sWorkingDir,@SW_HIDE,$iRunOptFlag)
    If @error Then Return SetError(2, @error, False) ; If the command is invalid...
    Local $hProcessHandle = _Process_GetHandle($iPID) ; Get the handle of the process
    If $iMode = $PROCESS_RUN Then
        If Not $bDebug Then Return SetExtended($iPID, $hProcessHandle) ; If the function is in Run mode then return the PID & Process Handle
        $sOutput = _Process_DebugRunCommand($hProcessHandle, $iPID) ; Debug the process
        $iExitCode = _Process_GetExitCode($hProcessHandle) ; Note the exit code
        Return SetExtended($iExitCode, $sOutput) ; Return the output & exit code
    If Not $bDebug Then
        While ProcessExists($iPID)
            $sOutput &= StdoutRead($iPID) ; Capture the output
            Sleep(250) ; Don't kill the CPU
        $sOutput &= StdoutRead($iPID) ; Capture any remaining output
        $iExitCode = _Process_GetExitCode($hProcessHandle) ; Note the exit code
        Return SetExtended($iExitCode, $sOutput) ; Return the exit code & the output :D
    $sOutput = _Process_DebugRunCommand($hProcessHandle, $iPID) ; Debug the process
    $iExitCode = _Process_GetExitCode($hProcessHandle) ; Note the exit code
    Return SetExtended($iExitCode, $sOutput) ; Return the output & exit code

The issue that I currently have is that, regardless of what the errorlevel returned by the program being executed, the errorlevel returned by the _Process_RunAsCommand is 259, which, according to this page it means that there's no more data (I'm guessing from the STDIO and STDERR?)

Any guidance would be greatly appreciated.

Link to comment
Share on other sites

  • Moderators

Moved to the appropriate forum, as the Developer General Discussion forum very clearly states:


General development and scripting discussions. If it's super geeky and you don't know where to put it - it's probably here.

Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


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

  • Create New...