Coder Social home page Coder Social logo

powershell-host's Introduction

PowerShell-Host

PowerShell-Host is a JavaScript library which hosts a PowerShell process to allow running commands and getting the results back quickly. PowerShell Exceptions are intercepted and the Message property is emitted to stderr.

This implementation uses Promises to make things easy to use:

  • Output on stderr will be rejected.
  • Output on stdout will be resolved.

Installation

npm install powershell-host

Example

You can use the in-built ConvertTo-Json applet to make parsing the data easy.

import { PowerShell } from 'powershell-host';

const ps = new PowerShell();

const run = async () => {
    // Open a new PowerShell without any profile which might impede us
    // We could use `shell: 'pwsh'` in the options to open a newer PowerShell if installed
    await ps.open({ args: ['-NoProfile'] });

    // Log any errors that might occur after opening
    // such as invalid arguments in the open command above
    ps.on('error', (err) => {
        console.log(`ERROR: ${err}`);
    });

    // Log if shell exits for any reason
    ps.on('exit', (code, signal) => {
        // Will not log graceful exit we removed all listeners on close
        console.log(`shell exited code:${code} signal: ${signal}`);
    });

    const json = await ps.exec('Get-PsDrive | Select Name, Root | ConvertTo-Json');
    const obj = JSON.parse(json);
    // Newer versions of PowerShell can use ConvertTo-Json -ToArray to avoid forcing an array here
    const result = Array.isArray(obj) ? obj : [obj];
    result.forEach((drive) => console.log(drive.Name));
};

run()
    .catch((err) => {
        console.error(`ERROR: ${err.message}`);
    })
    .finally(() => {
        ps.close();
        ps.removeAllListeners();
    });

powershell-host's People

Contributors

rsmarples avatar

Stargazers

John Magdy Lotfy Kamel avatar Abderrahmene Merzoug avatar

Watchers

 avatar Abderrahmene Merzoug avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.