Runs an external program under the context of a different user.
RunAs ( "username", "domain", "password", logon_flag, "program" [, "workingdir" [, show_flag [, opt_flag]]] )
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 if 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) 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 its 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.
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.
ProcessClose, Run, RunAsWait, RunWait, ShellExecute, ShellExecuteWait, StderrRead, StdinWrite, StdioClose, StdoutRead
#include <AutoItConstants.au3>
Example()
Func Example()
; Change the username and password to the appropriate values for your system.
Local $sUserName= "Username"
Local $sPassword= "Password"
; Run Notepad with the window maximized. Notepad is run under the user previously specified.
Local $iPID= RunAs ($sUserName,@ComputerName ,$sPassword,$RUN_LOGON_NOPROFILE,"notepad.exe","",@SW_SHOWMAXIMIZED )
; Wait 10 seconds for the Notepad window to appear.
WinWait ("[CLASS:Notepad]","",10)
; Wait for 2 seconds.
Sleep (2000)
; Close the Notepad process using the PID returned by RunAs.
ProcessClose ($iPID)
EndFunc ;==>Example