Socket
Book a DemoInstallSign in
Socket

node-powershell

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-powershell

Lightweight module to run PowerShell straight from your Node app

Source
npmnpm
Version
1.2.0
Version published
Weekly downloads
19K
-42.43%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

node-powershell

FAQs

Package last updated on 25 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts