Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

child_process.exec() - open executed program in interactive window #4409

Closed
larssb opened this issue Feb 16, 2016 · 9 comments
Closed

child_process.exec() - open executed program in interactive window #4409

larssb opened this issue Feb 16, 2016 · 9 comments

Comments

@larssb
Copy link

larssb commented Feb 16, 2016

Hi fellow NWjs programmers,

I'm trying to solve/figure out the following: Is it possible to open a spawned external executable interactively. By external executable I mean: an executable that has not been packaged with the NWjs program I'm developing.
I'm able to execute the external executable quite well, I see it in Windows task manager, however the programs does not appear and therefore I'm not able to interact with it. This is quite essential as the exe I'm calling is powershell with a specific script to the powershell.exe -File parameter. The script in -File is asking for input from the person running it, so again, interaction is needed.
I can test and verify, that by using Shell I can open a file with its matching program. This gives me the impression that NWjs can fire external executables that will show their gui interactively, so either I'm doing something completely wrong or misunderstand what NWjs is capable of.

CODE EXAMPLE:
var exec = require('child_process').exec; exec('powershell -noexit -File MailboxManagement_SetFullMbxPerms.ps1');

Looking forward to hear from you.

Thank you very much.
/Larssb

@kucix
Copy link

kucix commented Feb 17, 2016

maybe is problem in exec.
exec run cmd.exe + your command
try execFile('powershell.exe', ['-noexit', '-File MailboxManagement_SetFullMbxPerms.ps1'])
https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback

@larssb
Copy link
Author

larssb commented Feb 17, 2016

Hi @kucix,

Thank you for replying. Just tried it. Same result. PowerShell shows up in task manager but not graphically with a window I can interact with.

@larssb
Copy link
Author

larssb commented Feb 17, 2016

@kucix could it be the way I trigger it? Inside my HTML I have a button --> this button have an onClick tag --> this calls a function that resides inside a <script> tag.
Like this:

`

<title>Box Of Power</title> Click it! <script> function executeScript() { var execFile = require('child_process').execFile; execFile('powershell.exe', ['-noexit', '-File MailboxManagement_SetFullMbxPerms.ps1']); } </script>
`

@kucix
Copy link

kucix commented Feb 17, 2016

I don't know.
I'm launching apps with execFile and it's ok.
But I'm launching exe - installer of an app.
Maybe run cmd batch file with exec?
Is there any errors in callback?

Maybe someone else can help.

@larssb
Copy link
Author

larssb commented Feb 17, 2016

hi @kucix,

I just tested with notepad.exe, I can launch that and it opens and I can see the program running. This does not happen when calling PowerShell. Tried to lower the number of possible error causes by just calling powershell.exe only, without a reference to a script file. Still no interactive window. Did further troubleshooting and this might be because PowerShell figures its streams have been redirected and this makes it run in a non-interactive process.....references:
1.
Q160. I'm trying to use PowerShell in a terminal session using the 'dumb' terminal type. It doesn't display a command prompt.
When PowerShell detects that its input or output streams have been redirected, it suppresses any prompts that it might normally display. If you want to host an interactive PowerShell prompt inside another application (such as Emacs), use "-" as the argument for the -File parameter. In many shells, this implies "taken from standard input."

powershell -File -

// The above was taken from: https://www.bitvise.com/winsshd-usage-faq.html

I’m guessing that the combination of ruby.exe allocating a console, running as LocalSystem within the service desktop and windows station, and the use of the -File powershell argument is causing this issue.

It may be that we can avoid this issue either by specifying -InputFormat None, specifying the powershell command inline, or using a shell to redirect the file, e.g. cmd /c powershell.exe < foo.ps1

// The above was taken from: https://projects.puppetlabs.com/issues/22258

I tried the suggestions in the two posts but I can't get it to work. I hope someone can assists.

Thank you very much.

@kucix
Copy link

kucix commented Feb 17, 2016

Looks like powershell feature.
Can you write script in another language, not under powershell?
Another idea includes more programing in your nw.js app.
ExecFile returns ChildProcess object and You can communicate through the object with process - read stdout, display it to user and show prompt, inpuzt field etc. for his input and then send it back to the process.
But... it's not simple

https://nodejs.org/api/child_process.html#child_process_child_stdin

@larssb
Copy link
Author

larssb commented Feb 17, 2016

Hmmm yeah, good idea, but a little overboard considering the use case here. The use case is to use NWjs --> make a dashboard of a kind with web programming --> the dashboard should the show different management categories, for example Exchange, when clicking on a category you are taken to a sub-menu --> you then find the PowerShell you need to execute in order to solve the task at hand --> when clicking on the corresponding script button in the dashboard PowerShell should open in an interactive window - as the script is most likely needing input from the user executing it......

Thank you

@kucix
Copy link

kucix commented Feb 17, 2016

I'm thinking.
Powershell run's noninteractive mode, because he is attached, he has redirected input/output streams.

Maybe it would help if you used a different method.
Try spawn
https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
And in options try detached: true.
spawn('powershell.exe', ['-noexit', '-File MailboxManagement_SetFullMbxPerms.ps1'], {detached: true})

just an idea...

@larssb
Copy link
Author

larssb commented Feb 17, 2016

Almost did it.....ended up doing:

var spawn = require('child_process').spawn; spawn('cmd.exe', ['/c', 'powershell.exe -noexit -File PATH TO FILE'], {detached: true});

Had to call powershell via cmd instead of PowerShell directly. The NodeJS doc also speaks about that and I tried the spawn method yesterday but without luck. However this is wonderful 👍

Finally it works - thank you very much for your great assistance.

Have a great day and good luck with the projects you are doing 🎱

@larssb larssb closed this as completed Feb 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants