npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@obayd/powerjs

v1.0.1

Published

Do the powershell magic... right from your script!

Downloads

36

Readme

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

  • 100% Pure javascript (no native files included): Enjoy more flexibility with a lower cost and a shorter setup process.

  • Dependency-less: PowerJS eliminates the need for additional dependencies, ensuring a lightweight and hassle-free integration with your projects.

  • Seamless PowerShell Integration: PowerJS enables you to execute PowerShell commands and scripts directly from your JavaScript or TypeScript code, making it easy to leverage the power of PowerShell within your application.

  • Extension Support: Extend PowerJS functionality with ease by adding custom extensions. These extensions can include additional PowerShell modules, functions, and capabilities to tailor PowerJS to your specific needs. ( You can make your own extensions and publish them )

  • DLL Integration: Import and interact with DLLs (Dynamic Link Libraries) in your PowerShell scripts. PowerJS simplifies the process of importing DLLs and provides a convenient interface for direct interaction.

  • Flexible Configuration: Configure PowerJS according to your requirements with options like specifying additional shell names, enabling elevated permissions (runas), and automatic startup of extensions.

  • Robust Error Handling: PowerJS includes robust error handling features, allowing you to capture and handle errors gracefully, ensuring your application remains stable even when executing complex PowerShell commands.

  • Asynchronous Execution: Execute PowerShell commands asynchronously, preventing your application from becoming unresponsive while waiting for script execution to complete.

  • Detailed Results: Access detailed results of PowerShell script executions, including standard output, standard error, and execution success status. PowerJS provides a convenient result object for easy data retrieval.

  • Comprehensive Documentation: PowerJS includes comprehensive TypeScript declaration files (.d.ts) and inline code comments, making it easy to understand and use the module in your projects.

  • Cross-Platform Compatibility: PowerJS is designed to work across different platforms, ensuring consistent PowerShell integration regardless of the operating system.

  • ~~Elevated Permissions: Run PowerShell commands with elevated permissions when necessary, providing the ability to execute administrative tasks and interact with protected system resources.~~ ( Coming Soon... )

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.