Skip to content

Run PowerShell scripts repeatedly in a high performance manner.

License

Notifications You must be signed in to change notification settings

JustinGrote/PowerServe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PowerServe

Run pwsh scripts repeatedly in a high performance manner and get JSONified results.

Quick Start

Download the release and extract into a folder.

PowerServeClient.exe "MyScript"

Note: This will spawn a background pwsh.exe process. To end that process when finished, use the following command: get-process pwsh | where commandline -match 'Start-PowerServe' | stop-process

To see more options, run PowerServeClient.exe --help

However it is purposefully meant to be persistent to "cache" the startup and runspace pools for better efficiency, so you would generally only do this for troubleshooting and testing.

Purpose

This is primarily targeted at monitoring programs such as PRTG that execute pwsh.exe dozens if not hundreds of times a minute, in an effort to reduce the memory and overhead of all those separate processes. It also has a good use for other programming languages being able to call into PWSH and get a JSON result quickly and simply.

Architecture

PowerClient.exe is a .NET 8 AOT-optimized native executable that takes a script argument and then:

  1. Attempts to connect to the PowerServe server via a named pipe
  2. If the server is not running, start a pwsh.exe process and inject the PowerServe server into it, loading it as a module and opening the named pipe for the server.
  3. Encode the script in base64 and send it as a single line to the server via the named pipe
  4. Server takes the script, decodes it, and executes it in a runspace pool
  5. It will take the results, serialized them to a single-lined JSON, and then return it to the client via the named pipe which in turn outputs that via stdout.
  6. If a single string is returned from the script, it is passed through (removing any newlines). This is done in the case the script has already done the JSON serialization or wants to return an alternate output.

Debugging

You can alternatively for debugging purposes start a pwsh.exe process and then run: import-module path/to/powerserve.dll;Start-PowerServe -PipeName PowerServe-USERNAME

Replacing USERNAME with your current username. If you do this then you will be able to see the logs for the normally backgrounded process. The client will then attach to this server.

Roadmap

  • Controlling number of threads
  • "Script Affinity" which will attempt to re-run the same script in the same runspace and minimize memory usage and startup time

Limitations / Known Issues

  • Only error and output streams are considered, warning/progress/verbose/debug output is discarded
  • Sometimes gets frozen on serialization due to a large default depth size, be sure to be very narrow in the objects you return. PSCustomObject and Select-Object are your friends.
  • Environment variables are currently not passed through from the PowerServeClient.exe call to the backend pwsh process, it is recommended instead you provide the environment variables as parameters to your script.

About

Run PowerShell scripts repeatedly in a high performance manner.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages