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
3.0.1
Version published
Weekly downloads
19K
-42.43%
Maintainers
1
Weekly downloads
 
Created
Source

Node-PowerShell

Version npmNPM DownloadsDependencies

Lightweight module to run PowerShell straight from your Node app.

Installation

$ npm i -S node-powershell

:fire::fire::fire: NEW :fire::fire::fire:

As you may have heard already, lately Microsoft is taking steps towards becoming an open source company. One of these steps, brings us [PowerShell6][] , which is a cross-platform version of the amazing tool that we know and love from Windows. Node-PowerShell Welcomes the move, and started the current version, will fully support the new PS. Moreover, I will continue to follow the development of the new PS repo, and to update the module accordingly. Enjoy! [PowerShell6]: https://github.com/PowerShell/PowerShell

Quick start

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

var ps = new shell({executionPolicy: 'Bypass', debugMsg: true, noProfile: 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
  • noProfile - Determines whether to load the Windows PS profile (Boolean) (Default: true) 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 30 Sep 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