WSH .Exec和.Execute不会运行自定义Jscript文件

时间:2014-09-16 23:36:19

标签: exec execute jscript wsh

当我尝试.Exec()时,我收到错误“不是有效的win32应用程序”。这是产生它的代码:

var oShell = new ActiveXObject("WScript.Shell");
        try {
        var subRoutine = oShell.Exec("sayTest.js")
        } catch (e) {
            WScript.Echo('Error Message: ' + e.message);
            WScript.Echo('Error Description: ' + e.description);
            WScript.Sleep(15000);
        }

.Execute()的情况下,我收到错误“自动化服务器无法创建对象”:

var Controller = WScript.CreateObject("WSHController");
        try {
        var RemoteScript = Controller.CreateScript("sayTest.js");
        } catch (e) {
            WScript.Echo('Error Message: ' + e.message)
        }
        try {
        RemoteScript.Execute();
        } catch(e) {
            WScript.Echo('Error Message: ' + e.message)
        }

现在我可以让.Run()正常工作,但它不适合我的目的,因为它不提供对我的应用程序至关重要的processID属性或Terminate()方法。我尝试使用.Exec.Execute()的完全限定路径,但这也不起作用。这些是我尝试过的路径:

C:\Program Files (x86)\MIDIOX\WSH\sayTest.js

C:/Program Files (x86)/MIDIOX/WSH/sayTest.js

C:\\Program Files (x86)\\MIDIOX\\WSH\\sayTest.js

1 个答案:

答案 0 :(得分:2)

.Exec尝试启动可执行文件,而不是可能知道(ftype,assoc)如何使用c / wscript.exe运行.js的shell。所以在你的命令行中提到c / wscript;为了安全起见,请对.exe和.js使用完整的引用路径。

证据:

25880027-A.js:

var oShell = new ActiveXObject("WScript.Shell");
var aCmds = [
    "25880027-B.js",
    "cscript 25880027-B.js"
];
for (var xCmd in aCmds) {
    xCmd = aCmds[xCmd];
    WScript.Echo("------ " + xCmd);
    try {
        var subRoutine = oShell.Exec(xCmd);
        WScript.Echo(subRoutine.StdOut.ReadLine());
        WScript.Echo("ok");
    } catch (e) {
        WScript.Echo('Error Message: ' + e.message);
    }
}

25880027-B.js:

WScript.Echo("25880027-B.js executed.");

输出:

cscript 25880027-A.js
------ 25880027-B.js
Error Message: 25880027-B.js is not a valid Win32 application.

------ cscript 25880027-B.js
25880027-B.js executed.
ok