bsh*_*ett 3 powershell wsh exec blocking
我试图用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());
Run Code Online (Sandbox Code Playgroud)
test.vbs
dim shell
set shell = CreateObject("WScript.Shell")
WScript.Echo shell.exec("powershell -Command $Host.Version; Exit").StdOut.ReadAll
Run Code Online (Sandbox Code Playgroud)
我做错了什么,或者我遇到了限制/不兼容?Run方法工作得很好,但是我需要捕获它无法做到的输出.
编辑:我忘了提到我的平台是Windows 7 Pro,64位使用PowerShell 3.我已经在Windows XP上使用PowerShell 1进行了测试.
编辑2:我已经更新了我正在运行的测试脚本以符合x0n的答案.不幸的是,我仍然遇到麻烦.这是我目前的测试:
test.js:
var shell = new ActiveXObject("WScript.Shell");
WScript.Echo(shell.exec('powershell -noninteractive -noprofile -Command "& { echo Hello_World ; Exit }"').StdOut.ReadAll());
Run Code Online (Sandbox Code Playgroud)
test.vbs:
dim shell
set shell = CreateObject("WScript.Shell")
WScript.Echo shell.exec("powershell -noninteractive -noprofile -Command ""& { echo Hello_World ; Exit }""").StdOut.ReadAll
Run Code Online (Sandbox Code Playgroud)
你必须关闭StdIn:
var shell = new ActiveXObject("WScript.Shell");
var exec = shell.Exec('powershell -noninteractive -noprofile -Command "& { echo Hello_World ; Exit }"');
exec.StdIn.Close();
WScript.Echo(exec.StdOut.ReadAll());
Run Code Online (Sandbox Code Playgroud)
微软说:
StdIn is still open so PowerShell is waiting for input. (This is an
"implementation consideration" that we're hoping to fix in V2. The
PowerShell executable gathers all input before processing.) So
objexec.StdIn.Close() needs to be added.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7585 次 |
| 最近记录: |