NODEJS 发送input到派生进程child.stdin.write();
我在PowerShell中有一组脚本。 我用.HTA构build了一个GUI,但是我正在迁移到一个类似NODE.js的方法。
我有一个工作脚本,但我不得不做一个修改,以解决function丢失从命令行input。 我曾经有CMD提示窗口出现在我会看到所有输出,如果有提示( Read-Host
),用户可以与它交互。 为了解决这个问题,我现在使用[Microsoft.VisualBasic.Interaction]::InputBox()
在需要的时候从用户那里获取input。
我尝试使用child.stdin.write();
因为我的PowerShell进程只是挂起。 它不会回应我的意见。
我已经尝试了我在网上find的每一个答案,没有成功。 生产中调用的脚本是一个长时间运行的过程,具体取决于所选任务,需要20分钟到3小时。
所以,问题是,我不能让child.spawned进程发送input,或者PowerShell不接受来自stdin的input。
感谢您的帮助提前。
码:
HTML:
<button type="button" id="btn_ExecuteReport">Run Report</button> <button type="button" id="btn_StopReport" >Stop Report</button> <button type="button" id="btn_SendInput" >Send this...</button> <h2>Results:</h2> <p id="result_id">...</p> <br/>
JS(使用Electron v 0.31.0,io.js v 3.1.0,jQuery v 1.7.2):
$(document).ready(function () { $("#btn_ExecuteReport").click(function (event) { event.stopPropagation(); global.$ = $; var remote = require('remote'); // to get some paths depending on OS var app = remote.require('app'); //clipboard var clipboard = require('clipboard'); var shell = require('shell'); // for choosing a folder var dialog = remote.require('dialog'); var spawn = require('child_process').spawn; sCmd = "powershell.exe "; sCmdArg = '&' + "'" + __dirname + "/app.ps1' " ; // omit parameters for the purpose of this example // + "-ODir '" + sOutputDirectory // + "' -WDir '" + sWorkingDirectory + "'" // + " -Report " + sReport // + " -pSendEmail " + sSendEmail // + " -pSendEmailHTMLBody " + sSendEmailHTMLBody // + " -pCreateReport " + sCreateReport // + " -pCheckReport " + sCheckReport // + " -pEmailAction " + sEmailAction // ; $("#result_id").html("<p><font color='magenta'>Command: </font>" + sCmdConcat + "</p>"); // try again with spawn and event handlers var psSpawn = spawn(sCmd, [sCmdArg]); // add a 'data' event listener for the spawn instance psSpawn.stdout.on('data', function(data) { console.log('stdout: ' + data); }); // add an 'end' event listener to close the writeable stream psSpawn.stdout.on('end', function(data) { console.log('Done with psSpawn...' ); }); psSpawn.stderr.on('data', function (data) { console.log('stderr: ' + data); }); // when the spawn child process exits, check if there were any errors and close the writeable stream psSpawn.on('exit', function(code) { if (code != 0) { console.log('[EXIT] Failed: ' + code); } //Collect result from PowerShell (via clipboard) sResultData = clipboard.readText("Text"); }); psSpawn.on('close', function(code) { if (code != 0) { console.log('[ISSUE] code: ' + code); } console.log('[CLOSE]'); }); //sending the input with button #btn_sendInput $("#btn_SendInput").click(function (event) { psSpawn.stdin.setEncoding('utf8'); psSpawn.stdin.write('ok'); psSpawn.stdin.write('\n'); psSpawn.stdin.write(os.EOL); //uncomment following line to end stdin on first button press //psSpawn.stdin.end(); }); // killing process $("#btn_StopReport").click(function (event) { event.stopPropagation(); psSpawn.kill('SIGTERM'); console.log('killed ' + psSpawn.pid); }); }); });
电源shell:
#work around to lack of Read-Host functionality Write-Host "waiting" [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null $computer = [Microsoft.VisualBasic.Interaction]::InputBox("Enter a computer name", "Computer", "$env:computername") # Write-Host "[Done] waiting" #workaround END works Write-Host "Computer: [$computer]" # START omit ---------------- #if we remove the following lines, all works but we lose the functionality to ask for input from the command line Write-Host "Propmt for OK" $ok = Read-Host # Write-Host "Prompt. Value of 'ok': [$ok]" # END omit ---------------- #Copy new message to clipboard "we want to see this maessage on the browser. $computer" | clip #Exit script returning the appropriate value [Environment]::Exit(0) exit
我的console
输出是:
stdout: waiting stdout: stdout: [Done] waiting Computer: [COMPUTER_NAME] Propmt for OK
无论我按下“ Send this...
”多less次,脚本都会显示。 console
输出是(在按下#buton_StopReport
):
killed 2548 Done with psSpawn... [EXIT] Failed: null [ISSUE] code: null [CLOSE]
- 电子浏览器的JavaScript错误
- 我怎样才能从Web服务器的信息发送到一个电子应用程序?
- 为应用程序制作桌面安装程序?
- Electron的BrowserWindow zoomFactor可以在施工后更改吗? 以及如何select最近的邻居,而不是平滑?
- 没有显示一个吞噬一个电子应用程序的Console.logs
- 'console.log'在Webstorm中不适用于testingElectron应用程序
- 无法在Windows 10中安装电子
- 如何整合电子WebStorm没有“模块没有安装”的提示?
- 要求使用TypeScript,SystemJS和Electron的nodejs“child_process”