Git Product home page Git Product logo

Comments (9)

BenjaminMichael avatar BenjaminMichael commented on June 6, 2024

Can you give an example that is not solved by using backticks `` around your strings and is not typical of PowerShell's regular behavior?

from child-shell.

hsimah avatar hsimah commented on June 6, 2024

@BenjaminMichael do you mean me escaping my quotes before invoking PowerShell? If so, not really.

imo, I should be able to pass valid javascript strings to node-powershell and have it correctly format them for PowerShell. Safe by default.

I can see an argument for me having to escape the strings I pass to node-powershell too. I've done the work in my fork for my use case.

from child-shell.

BenjaminMichael avatar BenjaminMichael commented on June 6, 2024

No I'm saying use template strings which are new if you are unfamiliar with ES6. It will give you the equivalent value as if you escaped characters

`\`` === '`' // --> true

from child-shell.

hsimah avatar hsimah commented on June 6, 2024

If I escape the javascript string myself (ie insert ` before my quotes) the command execute fine. I feel a javascript library should be doing the escaping for me, since I pass in a valid javascript string. The fix was a simple: value.replace(/[\'\"]/g, '$&');`.

In the current version this will hang:

string a = 'hsimah\'s';
string b = "hsimah's";
string c = `hsimah's`;

These will work:

string a = 'hsimah`\'s';
string b = "hsimah`'s";
string c = `hsimah\`'s`;
string d = 'hsimah\'s test'; // node-powershell wraps strings with whitespace in double quotes

from child-shell.

BenjaminMichael avatar BenjaminMichael commented on June 6, 2024
ps.addCommand(`write-host "hsimah's"`) //doesn't hang

This is just how PowerShell works.

edit: not trying to say that the PowerShell interpreter is perfect but if you go to the PowerShell github they can explain their choices better than I ever could.

edit2: a more generic example:

ps.addCommand(`write-host "${userName}"`) //doesn't hang

from child-shell.

hsimah avatar hsimah commented on June 6, 2024

Yes but if you use the parameter/value syntax it does hang:

ps.addCommand('my-cmdlet', [{Name: "hsimah's"}]); // hangs

Node-powershell doesn't wrap the value in quotes unless it contains a space. PowerShell is inferring it is a string, but it needs to be escaped with a backtick.

from child-shell.

BenjaminMichael avatar BenjaminMichael commented on June 6, 2024

My opinions are not those of Rann. I don't use the params as JSON I just always use template strings.

ps.addCommand(`./scripts/get-ADUser.ps1 -u1 ${payload.user1Name} -cu ${payload.currentUser}`);

In this example I don't even use quotes because I already sanitized the user names Im gathering. In my opinion these are merely implementation details. Since you made your own fork and your PR like you have people will see it and if they like it they will ask you to make an npm module (because Rann doesn't actively maintain node-powershell anyways).

from child-shell.

hsimah avatar hsimah commented on June 6, 2024

Yeah, I considered template strings, but some of the cim cmdlets get out of hand with the number of parameters. The array of objects was easier to use in that regard.

I'll add a test to see if my fork affects the pure string commands. I don't think it would as the logic flow is different, but I didn't check that and it's a bad PR if I break your code!

from child-shell.

hsimah avatar hsimah commented on June 6, 2024

@rannn505 I see this was closed. I no longer use node-powershell (changed jobs).

Is this fixed or not deemed necessary? I have an open PR related that can be closed either way.

from child-shell.

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.