JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 32080
  • Score
    100M100P100Q145822F
  • License MIT

Lightweight module to run PowerShell straight from your Node app

Package Exports

  • node-powershell

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-powershell) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Node-PowerShell

Lightweight module to run PowerShell straight from your Node app

Version npmNPM DownloadsDependencies

Installation

$ npm install node-powershell

Quick start

var shell = require('node-powershell').Shell;

var params = [{name:'paramName', value:'paramValue'}];
shell.executionStringBuilder("Path/To/Your/Script.ps1", params)
    .then(function(str){
        var ps = new shell(str);
        return ps.execute();
    })
    .then(function(output){
        console.log(output);
    })
    .catch(function(err){
        console.log(err);
    });

API

Shell Class require('node-powershell').Shell

Initiating:

Creates a new shell instance.

var ps = new shell(ps1ScriptPath / psCommand, options);

options:

  • debugMsg - Determines whether to log verbose to the console (Boolean) (Default: true) optional
  • outputEncoding - Sets the output encoding for the current shell (String) (Default: 'utf8') optional
  • executionPolicy - Sets the default execution policy for the current shell session (String) (Default: 'Unrestricted') optional

shellInstance.execute:

Starts executing the ps1ScriptPath / psCommand of the shell. return a promise with the output.

ps.execute()
    .then(function(output){
        console.log(output);
    })
    .catch(function(err){
        console.log(err);
    });

(static) Shell.executionStringBuilder:

Helper method that return a promise with an "Always works" Execution String.

var paramsArray =  [{name:'paramName', value:'paramValue'}];
shell.executionStringBuilder(ps1ScriptPath, paramsArray)
    .then(function(str){
        console.log(str);
    });

ShellManager Class require('node-powershell').ShellManager

Use this class when you want to execute lots of shells at once, and get their outputs together.

Initiating:

Creates a new shell instance.

var sm = new ShellManager(options);

options:

  • maxParallel - Determines the number of shells that can run in parallel (Number) (Default: 4) optional

ShellManagerInstance.queue:

Queue the shell for later execution.

sm.queue(new shell());

ShellManagerInstance.execute:

Starts executing all of the queued shells. return a promise with the output.

sm.execute()
    .then(function(output){
        console.log(output);
    })
    .catch(function(err){
        console.log(err);
    });

Examples

Use the Shell events:

var shell = require('node-powershell').Shell;

var ps = new shell('echo "node-powershell is awesome"');
ps.on('output', function(data){
    console.log(data);
});
ps.on('end', function(code) {
    //Do Something else
});

Putting an input to yor script:

Just use param ( ) instead of Read-Host in your script:

param (
    [Parameter(Mandatory = $true)]
    [string]$st
)
echo $st

and Shell.executionStringBuilder() or 'path "args"' in your node app.

for more examples please look at the example page.

License

MIT