obaydmerz / powerjs

Do the powershell magic... right from your script!
MIT License
6 stars 2 forks source link
api automation child-process dll node-ffi node-gyp nodejs powershell process shell windows

PowerJS cover

PowerJS - Empower Your JavaScript with PowerShell Magic

PowerJS is a powerful JavaScript library that enables you to seamlessly integrate and harness the magic of PowerShell directly from your scripts. Whether you need to automate administrative tasks, manage Windows processes, or interact with DLLs, PowerJS provides a user-friendly interface to supercharge your JavaScript applications.

Key Features

Installation

Currently there are a way to install it directly from github. ( For recent features & recommended )

npm install obaydmerz/powerjs

Or from npm: ( For stable relases )

npm install @obayd/powerjs

Examples

// Print PowerShell Version
import { PowerJS } from "@obayd/powerjs";

const instance = new PowerJS(/* options */);

instance.exec("$PSVersionTable").then((result) => {
  // You may notice some slowdown, that's because of the instance init process.
  // After the instance is started, you can enjoy a blazing fast environnement!
  console.log("Currently on Powershell v" + result.PSVersion.Major + "!");
});
// Read the local user list
import { PowerJS } from "@obayd/powerjs";

const instance = new PowerJS();

instance.exec(",(Get-LocalUser)").then(function (result) {
  // Use the , to make arrays returnable, otherwise, it will return only the first item
  // Read https://stackoverflow.com/questions/29973212/pipe-complete-array-objects-instead-of-array-items-one-at-a-time

  for (const user of result) {
    console.log(user.Name);
  }
  process.exit();
});
// Import a DLL
import { PowerJS } from "@obayd/powerjs";

const instance = new PowerJS({
  dlls: {
    "user32.dll": {
      LockWorkStation: [], // Imports LockWorkStation as a function
      MessageBox: ["int", "IntPtr", "String", "String", "int"], // Also a function, Please note that the first item is the function type.
    },
  },
});

instance.dll.user32
  .MessageBox(0, "Lock your computer?", "Warning", 3)
  .then(async ({ result }) => {
    if (result == 6) {
      await instance.dll.user32.LockWorkStation();
    }
  });

// You should take a deep lock to see how this magic happens.
// This is a super easy out-of-the-box alternative to node-ffi.
// Make an extension
import { PowerJS, Extension } from "@obayd/powerjs";

class MyAwesomeExtension extends Extension {
  name = "myawesomeext";

  async getVersion() {
    const { result } = await this.instance.exec("$PSVersionTable");

    return result.PSVersion.Major;
  }
}

const instance = new PowerJS({
  extensions: [MyAwesomeExtension],
});

const myAwesomeExt = instance.getExtension(MyAwesomeExtension);
// OR: const myAwesomeExt = instance.getExtension("myawesomeext");

myAwesomeExt.getVersion().then((versionMajor) => {
  console.log("Huh ?! Powershell v" + versionMajor);
});

Easy, isn't it?

Read more

For more information and advanced usage, check out the PowerJS Wiki.

You can join our discord server.