Function Reference

RunAs

Runs an external program under the context of a different user.

RunAs ( "username", "domain", "password", logon_flags, "filename" [, "workingdir" [, flag[, standard_i/o_flag]]] )

 

Parameters

username The username to log on with.
domain The domain to authenticate against.
password The password for the user.
logon_flags 0 - Interactive logon with no profile.
1 - Interactive logon with profile.
2 - Network credentials only.
4 - Inherit the calling processes environment instead of the user's.
filename The name of the executable (EXE, BAT, COM, or PIF) to run.
workingdir [optional] The working directory. If not specified, then the value of @SystemDir will be used.
flag [optional] The "show" flag of the executed program:
  @SW_HIDE = Hidden window (or Default keyword)
  @SW_MINIMIZE = Minimized window
  @SW_MAXIMIZE = Maximized window
standard_i/o_flag [optional] Provide a meaningful handle to one or more STD I/O streams of the child process.
  1 ($STDIN_CHILD) = Provide a handle to the child's STDIN stream
  2 ($STDOUT_CHILD) = Provide a handle to the child's STDOUT stream
  4 ($STDERR_CHILD) = Provide a handle to the child's STDERR stream
  8 ($STDERR_MERGED) = Provides the same handle for STDOUT and STDERR. Implies both $STDOUT_CHILD and $STDERR_CHILD.

 

Return Value

Success: The PID of the process that was launched.
Failure: Returns 0 and sets @error to non-zero.

 

Remarks

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 continues. To pause execution of the script until the spawned program has finished use the RunAsWait function instead.

Providing the Standard I/O parameter with the proper values permits interaction with the child process through the StderrRead, StdinWrite and StdoutRead functions. Combine the flag values (or use $STDERR_CHILD, $STDIN_CHILD & $STDOUT_CHILD, defined in Constants.au3) to manage more than one stream.

In order for the streams to close, the following conditions must be met: 1) The child process has closed it's end of the stream (this happens when the child closes). 2) AutoIt must read any captured streams until there is no more data. 3) If STDIN is provided for the child, StdinWrite() must be called to close the stream. Once all streams are detected as no longer needed, all internal resources will automatically be freed.
StdioClose can be used to force the STDIO streams closed.

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

 

Related

Run, RunWait, RunAsWait, ShellExecute, ShellExecuteWait, StderrRead, StdinWrite, StdoutRead, StdioClose, ProcessClose

 

Example


; Fill in the username and password appropriate for your system.
Local $sUserName = "Username"
Local $sPassword = "Password"

; Run a command prompt as the other user.
RunAs($sUserName, @ComputerName, $sPassword, 0, @ComSpec, @SystemDir)