Coder Social home page Coder Social logo

Comments (6)

junjunruan avatar junjunruan commented on June 23, 2024 2

Thanks @jorangreef:
Yes, the process title is some weird characters that I didn't recognize. But with { name: 'Test' }, it works.

from sudo-prompt.

jorangreef avatar jorangreef commented on June 23, 2024

Hi @junjunruan, sudo.exec() is similar to child_process.exec() in that you simply pass the arguments as part of the command string (see the Node.js docs). There is therefore no [args] argument when calling sudo.exec().

If you need to pass parameters to your batch file, just pass "test.bat arg1 arg2 ..." as your command string. You then need to know how to access arguments from within a Windows batch file (see StackOverflow).

from sudo-prompt.

junjunruan avatar junjunruan commented on June 23, 2024

Thanks @jorangreef. It works.

I have a few questions. Is it the right place to ask questions or is there other ways to contact you?
My questions are:

  1. From your documentation, I see this message: Passing options.name or options.icns is currently not supported by sudo-prompt on Windows. Then what the syntax should be for windows?
    I tried delete options json, and remove "options" from sudo.exec(), it doesn't work.
    The only way it works for me now is leave options json and parameter there.
  2. I have several commands in my .cmd batch file need admin right, is there any ways to remember user's choice ("Yes") in the first time so that the dialog doesn't need to prompt every time?
    (maybe save the windows username and password when the first time a user clicks yes?)
  3. What if the user click "NO" when the dialog popup (user did not grant permission), is there any ways to keep popup the dialog until user choose YES?

from sudo-prompt.

jorangreef avatar jorangreef commented on June 23, 2024

Sure, you're welcome to ask here and it might be helpful to others. Regarding your questions:

  1. Just pass {} as options on Windows.
  2. No, we delegate the privilege elevation to Windows itself. Windows shows the dialog prompt and we never receive the username and password (this would also not be safe). The best thing is to create a batch script which does everything that requires admin rights, and then execute this batch script using sudo-prompt's exec(). That way the user will only receive a single password prompt each time you run the batch script.
  3. Sure, you could just call exec() again but this would be annoying to the user. Perhaps you could explain to the user in advance why you need admin privileges, so that they are more likely to accept?

from sudo-prompt.

junjunruan avatar junjunruan commented on June 23, 2024

Thanks for your quick response @jorangreef.

Here is what I tried based on your suggestion:
1.I got an error: process.title cannot be used as a valid name after I changed to the following code:

process.title = "electron";
 sudo.exec('echo hello', {}, function(error, stdout, stderr) {});

I checked the default value of process.title, the result is some Unrecognizable code.

2.I think what you mean if put all the commands need admin right in a batch file, then have one sudo.exec() to run at once.
But in my case, I have several sudo.exec() in my code by passing different parameters to run the batch file.

sudo.exec('test.cmd param1', options, function(error, stdout, stderr) {});
sudo.exec('test.cmd param2', options, function(error, stdout, stderr) {});

My batch file is similar to:

    if(param1) run command1
    if(param2) run command2

from sudo-prompt.

jorangreef avatar jorangreef commented on June 23, 2024

Sure,

  1. It may be that your process title was not in English? sudo-prompt requires options.name to be alphanumeric, and uses the process title if options.name is not provided. To solve this, just try using{ name: 'Test' } as your options object instead of {}.
  2. Then you would need two prompts, unless you can restructure things.

from sudo-prompt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.