首页 > 解决方案 > 运行命令后控制不返回原始 vbs 文件

问题描述

我正在尝试从另一个调用 vbs 文件。被调用的文件执行。但是,控件不会返回到原始 vbs 文件。当我在任务管理器中看到 wscript 进程时,该文件似乎正在运行。但我没有看到输出 - 运行命令之后的步骤。任何帮助/建议将不胜感激。

1.) vbs文件1(原始vbs文件test3.vbs)

Set objShell = WScript.CreateObject ("WScript.shell")
strErrorCode  = objShell.run("cmd /K C:\temp\a\test2.vbs", 0, True)
msgbox "complete test3"
Set objShell = Nothing

2.) vbs 文件 2(称为 vbs 文件 - test2.vbs)

msgbox "in test2"
WScript.Sleep 10000
msgbox "complete - test2"

3.) 预期输出:

in test2
complete - test2
complete test3

4.) 实际:

in test2
complete - test2

标签: vbscript

解决方案


objShell.run("cmd /K C:\temp\a\test2.vbs", 0, True)由于/K切换,永远不会结束:

cmd /?
Starts a new instance of the Windows command interpreter

CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
    [[/S] [/C | /K] string]

/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains

因此cmdwindow 保持打开但由于intWindowStyle参数而被隐藏: 0 =隐藏窗口并激活另一个 window,假设Run方法语法模式为.Run(strCommand, [intWindowStyle], [bWaitOnReturn])

使用任一

strErrorCode  = objShell.run("cmd /C C:\temp\a\test2.vbs", 0, True)

或者

strErrorCode  = objShell.run("wscript.exe C:\temp\a\test2.vbs", 0, True)

甚至

 strErrorCode  = objShell.run("C:\temp\a\test2.vbs", 0, True)

推荐阅读