我有一个运行以下代码的NodeJS(电子)客户端:

child = spawn("powershell.exe",['-ExecutionPolicy', 'ByPass', '-File', require("path").resolve(__dirname, '../../../../updater.ps1')]);
child.on("exit",function(){
    require('electron').remote.getCurrentWindow().close();
});


所打开的文件是一个Powershell文件,可下载并解压缩更新。如果我手动运行此文件,则会得到Powershell控制台,该控制台会显示下载和解压缩的进度条。但是,从上面的代码运行它不会显示控制台。

如何使我的代码在运行时显示Powershell控制台?我很难制定搜索条件来找到答案。

我尝试过的事情:


'-NoExit'添加到我的第二个参数数组
添加{ windowsHide: false }参数
'-WindowStyle', 'Maximized'添加到第二个参数数组


我也尝试过切换到exec

exec('powershell -ExecutionPolicy Bypass -File ' + updater_path, function callback(error, stdout, stderr){
    console.log(error);
});


哪个运行文件但仍不显示控制台。

最好使用一个答案让我运行未附加到NodeJS客户端的powershell文件,并在运行时显示powershell控制台。

这是我当前的代码:

updater = spawn("powershell.exe",['-ExecutionPolicy', 'ByPass', '-File', remote.app.getAppPath() + '\\app\\files\\scripts\\' + data.type + '_updater.ps1'], { detached: true, stdio: 'ignore' });
updater.unref();


实际上什么也没做,甚至似乎根本没有运行脚本。

我已经使用批处理文件尝试过相同的操作,但从未打开过。

updater = spawn("cmd",[remote.app.getAppPath() + '\\app\\files\\scripts\\launch_updater.bat'], { detached: true, stdio: ['ignore', 'ignore', 'ignore'] });
updater.unref();

最佳答案

我最终通过使用exec调用批处理文件来解决此问题,并且该批处理文件运行了powershell文件。

//call buffer .bat file, close main window after 3 seconds to make sure it runs before closing.
exec('start ' +  remote.app.getAppPath() + '\\app\\files\\scripts\\launch_updater.bat ' + data.type);
setTimeout(function() {
    require('electron').remote.getCurrentWindow().close();
}, 3000);


launch_updater.bat:

@ECHO OFF
set arg1=%~1
start powershell.exe -executionpolicy bypass -File "%~dp0/%arg1%_updater.ps1"
for /f "skip=3 tokens=2 delims= " %%a in ('tasklist /fi "imagename eq cmd.exe"') do (
    if "%%a" neq "%current_pid%" (
        TASKKILL /PID %%a /f >nul 2>nul
    )
)
exit /b


批处理文件中的循环本质上就是这样,它将自动关闭而不会打开命令窗口。我根据它是哪种更新来传递参数。

关于node.js - 允许Powershell控制台在NodeJS中显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56780478/

10-12 12:34
查看更多