Run pwsh scripts repeatedly in a high performance manner and get JSONified results.
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.
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.
PowerClient.exe is a .NET 8 AOT-optimized native executable that takes a script argument and then:
- Attempts to connect to the PowerServe server via a named pipe
- 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.
- Encode the script in base64 and send it as a single line to the server via the named pipe
- Server takes the script, decodes it, and executes it in a runspace pool
- 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.
- 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.
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.
- 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
- 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.