函数参考


RunAsWait

在不同的用户环境中运行一个外部程序并暂停脚本执行直到程序结束.

RunAsWait ( "用户名", "域", "密码", 登录标志, "程序" [, "工作目录" [, 显示标志 [, 可选标志 ]]] )

参数

用户名 登录的用户名.
依赖验证的域.
密码 用户的登录口令.
登录标志 0 - 交互登录,没有 profile.
1 - 交互登录,有 profile.
2 - 只使用网络证书.
4 - 继承调用程序的环境变量替换用户的环境变量.
程序 程序所在的完整路径(文件格式为 EXE,BAT,COM 或 PIF).
工作目录 [可选参数] 工作目录. 如果不指定, 则使用 @SystemDir.这个路径不一定指向程序所在路径.
显示标志 [可选参数] 执行程序的显示状态:
  @SW_HIDE = 隐藏窗口 (或 Default 关键字)
  @SW_MINIMIZE = 最小化窗口
  @SW_MAXIMIZE = 最大化窗口
可选标志 [可选参数] Controls various options related to how the parent and child process interact.
  0x10000 ($RUN_CREATE_NEW_CONSOLE) = 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.

返回值

成功: 返回运行程序的退出代码.
失败: 返回 0 并设置 @error 为非零.

注意/说明

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 2000, Windows XP SP2 or later, or Windows Vista are not affected.

相关

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

示例/演示


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

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

; Wait for the process to close.
ProcessWaitClose($pid)

; Show a message.
MsgBox(0, "", "The process we were waiting for has closed.")