【发布时间】:2012-09-04 20:09:27
【问题描述】:
我正在尝试使用 WshShell 对象的 Exec 方法调用 Powershell。我正在用 JScript 编写脚本,但我也在 VBScript 中重现了这个问题。以下两个简短的测试脚本都会导致 WSH 无限期挂起:
test.js
var shell = new ActiveXObject("WScript.Shell");
WScript.Echo(shell.exec("powershell -Command $Host.Version; Exit").StdOut.ReadAll());
test.vbs
dim shell
set shell = CreateObject("WScript.Shell")
WScript.Echo shell.exec("powershell -Command $Host.Version; Exit").StdOut.ReadAll
是我做错了什么,还是遇到了限制/不兼容? Run 方法效果很好,但我需要捕获输出,这是它无法做到的。
编辑:我忘了提到我的平台是 Windows 7 Pro,64 位,带有 PowerShell 3。我也在带有 PowerShell 1 的 Windows XP 上进行了测试。
编辑 2:我更新了我正在运行的测试脚本以适应 x0n 的答案。不幸的是,我仍然有麻烦。这是我目前的测试:
test.js:
var shell = new ActiveXObject("WScript.Shell");
WScript.Echo(shell.exec('powershell -noninteractive -noprofile -Command "& { echo Hello_World ; Exit }"').StdOut.ReadAll());
test.vbs:
dim shell
set shell = CreateObject("WScript.Shell")
WScript.Echo shell.exec("powershell -noninteractive -noprofile -Command ""& { echo Hello_World ; Exit }""").StdOut.ReadAll
【问题讨论】:
标签: powershell exec blocking wsh