2

我正在尝试使用SystemWorkerWakanda 服务器端 API 来实现一些目标。但我无法实现它。我确定我在SystemWorker.

我想从 Windows 启动 PowerShell,然后在里面运行两个命令。要手动执行此操作,我只需powershellcmd.exe中运行,然后我就可以开始编写 PowerShell 命令并取回我的数据。

在 Wakanda,我使用该代码设置了一个ssjs文件:

var powershell = new SystemWorker(['powershell'],null);

powershell.postMessage('Add-Type -AssemblyName "PresentationCore"');
powershell.endOfInput();
powershell.postMessage('[Windows.Media.Fonts]::SystemFontFamilies');
powershell.endOfInput();

powershell.onmessage = function (event) {
    var data = event.data;
    debugger;
};
powershell.onerror = function (event) {
    debugger;
};
powershell.onterminated = function (event) {
    // potentially check event.exitStatus or event.forced
    debugger;
    exitWait();
};
wait();

我还有一个包含以下内容的systemWorkers.json文件:

[
    {
        "name" : "powershell",
        "executable" :
        [
            { "path" : "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" }
        ]
    }
]

我只想执行这两个命令Add-Type -AssemblyName "PresentationCore",并[Windows.Media.Fonts]::SystemFontFamilies在我的 PowerShell 中获取机器上所有可用字体的列表。

如果我查看任务管理器,则会启动 PowerShell,但我从未实现执行我的两个命令行或取回数据。我只能在onterminated回调中结束。

我错过了什么?

4

1 回答 1

1

最后我到达使它工作。

现在我把我的命令放在一个 ps1 文件中,作为我的参数SystemWorker

所以它看起来像这样:

var powershell = new SystemWorker(['powershell "C:/Users/<MyName>/Desktop/<myScript>.ps1"'],null);

powershell.onmessage = function (event) {

};
powershell.onerror = function (event) {
};
powershell.onterminated = function (event) {
    exitWait();
};
wait();

我认为这样做更容易,所以我的所有命令都在一个文件中,然后我只要求系统工作人员执行 PowerShell 并运行我的脚本。

于 2015-06-05T10:40:06.287 回答