Git Product home page Git Product logo

Comments (6)

BenjaminMichael avatar BenjaminMichael commented on May 27, 2024

I'm writing a PR for this but its going to take some time because it will need so much testing.

Some day maybe you can explain to me your use case for not using .ps1 scripts while using multi line?

from child-shell.

suprak avatar suprak commented on May 27, 2024

👍

If I were you, I would abandon the parameter feature of addCommand. There are a lot of complicated cases and with the advent of string template literals, the user can figure out their own parameter passing mechanism pretty easily.

from child-shell.

BenjaminMichael avatar BenjaminMichael commented on May 27, 2024

I agree and you can essentially bypass it by just passing in one string for example:

ps.addCommand(`./remove-adGroupMember.ps1 -user '${user2}' -group '${groupDN}' -i ${i}`);

I arrived at this running mainly .ps1 scripts and having a lot of struggles with the interpretation of ' " and ` by the parameter logic

from child-shell.

BenjaminMichael avatar BenjaminMichael commented on May 27, 2024

I have a fork of node-powershell and I made a new branch: multi-line-suppor

https://github.com/BenjaminMichael/node-powershell/tree/multi-line-suppor

it seems to be working for this basic example:

const powershell = require('./node-powershell');

let ps = new powershell({
    executionPolicy: 'Bypass',
    noProfile: true,
    multiLine: true
    });
    ps.on('output', output => {
        console.log(output);
    })
    ps.addCommand(`$x = @"`);
    ps.addCommand(`foo`);
    ps.addCommand(`bar`);
    ps.addCommand(`"@;`);
    ps.invoke()
    ps.addCommand(`$x | out-host`)
    ps.invoke()

let ps2 = new powershell({
    executionPolicy: 'Bypass',
    noProfile: true
}); 
ps2.addCommand(`$PSVersionTable.PSVersion`);
ps2.addCommand(`$PSVersionTable.PSEdition`);
ps2.invoke()
.then(output =>  {
    ps2.dispose();
    console.log(output);
})

results in

NPS>  Process 5928 started

NPS>  Command invoke started
NPS>  $x = @"
foo
bar
"@;
NPS>  Command invoke started
NPS>  $x | out-host
NPS>  Process 240 started

NPS>  Command invoke started
NPS>  $PSVersionTable.PSVersion; $PSVersionTable.PSEdition
foo
bar

NPS>  Command invoke finished


NPS>  Command invoke finished

NPS>  Command invoke finished


Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  1944
Desktop



NPS>  Process 240 exited with code 0

It would be trivial to make a parser function that breaks down multi line JS strings into multiple addCommands. Is the goal to enter:

const x = `$x = @"
foo
bar
"@`;
addCommand(x);

and are you trying to compose with it?

addCommand(`${x} | out-host`);

I think it would help to see an example where this gives a benefit

from child-shell.

suprak avatar suprak commented on May 27, 2024

I don't see the benefit of creating a call to addCommand per line. That seems unnecessarily repetitive.

What I was trying to do,

addCommand(`
$value = @"
${JSON.stringify(req.params)}
"@
`);

from child-shell.

BenjaminMichael avatar BenjaminMichael commented on May 27, 2024

That seems more of an implementation detail. This example works and it takes any number of commands separated by commas so I added an out-host command to show its working. It seems to work.

const powershell = require('./node-powershell');

const addThenInvokeMLCommand = (...multiLineCommand) =>{
    this.ps = new powershell({
        executionPolicy: 'Bypass',
        noProfile: true,
        multiLine: true
        });
    this.ps.on('output', output => {
        console.log(output);
    });
    this.ps.on('end', code => {
        this.ps.dispose();
    });
    multiLineCommand.forEach((line) => {
        const commandArray = line.split('\n');
        commandArray.forEach((val) => {
            this.ps.addCommand(val + `\n`);
        });        
    });
    this.ps.invoke();
}


const myJSON = {
    "name":"John",
    "age":30,
    "cars":[ "Ford", "BMW", "Fiat" ]
    }

addThenInvokeMLCommand(`
$value = @"
${JSON.stringify(myJSON)}
"@
`, `$value | out-host`);

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.