Function Reference


RunAsWait

Show description in

Runs an external program under the context of a different user and pauses script execution until the program finishes.

RunAsWait ( "username", "domain", "password", logon_flag, "program" [, "workingdir" [, show_flag [, opt_flag]]] )

Parameters

username The username to log on with.
domain The domain to authenticate against.
password The password for the user.
logon_flag     $RUN_LOGON_NOPROFILE (0) - Interactive logon with no profile.
    $RUN_LOGON_PROFILE (1) - Interactive logon with profile.
    $RUN_LOGON_NETWORK (2) - Network credentials only.
    $RUN_LOGON_INHERIT (4) - Inherit the calling process's environment instead of the user's environment.

Constants are defined in "AutoItConstants.au3".
program The full path of the program (EXE, BAT, COM, or PIF) to run (see remarks).
workingdir [optional] The working directory. If not specified, then the value of @SystemDir will be used. This is not the path to the program.
show_flag [optional] The "show" flag of the executed program:
    @SW_HIDE = Hidden window (or Default keyword)
    @SW_MINIMIZE = Minimized window
    @SW_MAXIMIZE = Maximized window
opt_flag [optional] Controls various options related to how the parent and child process interact.
    $RUN_CREATE_NEW_CONSOLE (0x10000) = The child console process should be created with its own window instead of using the parent's window. This flag is only useful when the parent is compiled as a Console application.

Constant is defined in AutoItConstants.au3

Return Value

Success: the exit code of the program that was run.
Failure: sets the @error flag to non-zero.

Remarks

Paths with spaces need to be enclosed in quotation marks.

It is important to specify a working directory the user you are running as has access to, otherwise the function will fail.

It is recommended that you only load the user's profile is you are sure you need it. There is a small chance a profile can be stuck in memory under the right conditions. If a script using RunAs() happens to be running as the SYSTEM account (for example, if the script is running as a service) and the user's profile is loaded, then you must take care that the script remains running until the child process closes.

When running as an administrator, the Secondary Logon (RunAs()) service must be enabled or this function will fail. This does not apply when running as the SYSTEM account.

After running the requested program the script pauses until the program terminates. To run a program and then immediately continue script execution use the RunAs() function instead.

Some programs will appear to return immediately even though they are still running; these programs spawn another process - you may be able to use the ProcessWaitClose() function to handle these cases.

The "load profile" and "network credentials only" options are incompatible. Using both will produce undefined results.

There is an issue in the Windows XP generation of Windows which prevents STDIO redirection and the show flag from working. See Microsoft Knowledge Base article KB818858 for more information about which versions are affected as well as a hotfix for the issue. Users running Windows XP SP2 or later, or Windows Vista or later are not affected.

Related

ProcessWait, ProcessWaitClose, Run, RunAs, RunWait, ShellExecute, ShellExecuteWait

Example

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIError.au3>

Example()

Func Example()
        ; Change the username and password to the appropriate values for your system.
        Local $sUserName = "Username"
        Local $sPassword = "Password"

        ; Run Notepad and wait for the Notepad process to close. Notepad is run under the user previously specified.
        Local $iReturn = RunAsWait($sUserName, @ComputerName, $sPassword, $RUN_LOGON_NOPROFILE, "notepad.exe")

        If @error Then
                Local $sLastError = _WinAPI_GetLastErrorMessage()
                MsgBox($MB_SYSTEMMODAL + $MB_ICONERROR, "Error", "Notepad has not Run :" & @CRLF & @CRLF & $sLastError)
        Else
                ; Display the return code of the Notepad process.
                MsgBox($MB_SYSTEMMODAL, "", "The return code from Notepad was: " & $iReturn)
        EndIf
EndFunc   ;==>Example