JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 39557
  • Score
    100M100P100Q146074F
  • 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

Version npmNPM DownloadsDependencies

Lightweight module to run PowerShell straight from your Node app.

Installation

$ npm i -S node-powershell

🔥🔥🔥 NEW 🔥🔥🔥

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});

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:

Name Description
history An array containing the command history ever added to the shell.
streams An object containing the sdtio (in,out,err) of the PowerShell Instance.

Methods:

Name Description Syntax Return 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:

Name Description Syntax
output Emits when shell has an output. ps.on('output', data=>{});
err Emits when shell has an error. ps.on('err', error=>{});
end Emits 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