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
2.0.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 i -S node-powershell

Quick start

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

var ps = new shell({executionPolicy: 'Bypass', debugMsg: true});

ps.addCommand('echo "node-powershell rocks"');
    .then(function(){
        return ps.invoke();
    })
    .then(function(output){
        console.log(output);
        ps.dispose();
    })
    .catch(function(err){
        console.log(err);
        ps.dispose();
    });

API

PowerShell Class require('node-powershell')

Provides promise based methods that are used to create a pipeline of commands and invoke those commands within a PowerShell runspace.

initialize(constructor):

Creates a new shell instance.

var ps = new shell(options);

options:

  • debugMsg - Determines whether to log verbose to the console (Boolean) (Default: true) optional
  • inputEncoding - Sets the input encoding for the current shell (String) (Default: 'utf8') 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

Properties:

NameDescription
historyAn array containing the command history ever added to the shell.
streamsAn object containing the sdtio (in,out,err) of the PowerShell Instance.

Methods:

NameDescriptionSyntaxReturn Value
addCommand(command, params = [])Adds a command to the end of the pipeline of the shell object.ps.addCommand('./script.ps1', [{name:'str', value:'node-powershell rocks'}]);A promise of the commands.
invoke()Runs the commands of the shell object pipeline.ps.invoke();A promise with the result (can also be rejected to the catch).
dispose()Releases all resources used by the shell object and closes the PowerShell child_process.ps.dispose();A promise of the exit code.

Events:

NameDescriptionSyntax
outputEmits when shell has an output.ps.on('output', data=>{});
errEmits when shell has an error.ps.on('err', error=>{});
endEmits when shell ends.ps.on('end', code=>{});

Examples

Putting an input to yor script:

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

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

and Shell.addCommand() with the params array in your node app.

for more examples please look at the example page.

License

MIT

Keywords

node-powershell

FAQs

Package last updated on 09 Jul 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