Yes, you can do this. No, its not ready for prime time…… yet! So what am I really talking about here? This is the beginning of creating very useful PowerShell applications on a modern application framework. It will allow you to use the same API calls to your PowerShell scripts that you might use with other services in your infrastructure.
Quick links:
okay…. but WHY?
- Consistency
- Flexibility
- Versioning
- Scalability
- Ease of use
- Security (kinda, only expose Endpoints you want/need)
This is a great platform to develop a ‘PowerShell Application Server’ that you can access with other Powershell scripts and other services can now access the same information that your PowerShell scripts can generate.
Now, the Why nots…
- While its easy, its another layer.
- Security (no SSL yet)
- Memory (you MUST properly remove variables, and garbage collect!)
- No SwaggerUI (hopefully you know what this is!)
So lets Dive in with a basic use case of getting some process information off of the OS.
First, just install the module from the PowerShell Gallery:
Install-Module -Name RestPS
There is only one exported function ‘Start-RestPSListener’ which requires no parameters. I do suggest you use them!
- -Port (default is 8080)
- -RoutesFilePath (Default uses the built in Routes which is not recommended!)
You can copy the file ‘Invoke-AvailableRouteSet.ps1’ to ‘c:\temp’ and specify this as your Routes file path. (I also recommend adding ‘Invoke-AvailableRouteSet’ as the last line of this file). This will allow you to reload the routes with each REST API call to your server.
Now, your first endpoint can be started up like this:
Import-Module -Name RestPS Start-RestPSListener -Port 8081-RoutesFilePath "c:\temp\Invoke-AvailableRouteSet.ps1"
From a second console you can now run web requests to this new Endpoint.
Invoke-RestMethod -Method Get -Uri "http://localhost:8081/proc" Invoke-RestMethod -Method Get -Uri "http://localhost:8081/process?vmware-tray"
It’s that simple! The Web listener will execute any command or script defined in your Routes file. If the Route is unknown a message will be sent back ‘Invalid Command’.
To shut the Endpoint down, send the following comand:
Invoke-RestMethod -Method Get -Uri "http://localhost:8081/Endpoint/Shutdown"
Below is an image of the Calls from each console.
You can see this is a very easy project to get started with. You can now put a pretty simple web front end on any of your scripts. Give access to outside users to run your scripts. Just use some caution! You only have to expose endpoints/scripts you want folks to have access to. I have not started the road down SSL yet.
Check it out, have some fun, give some feedback!
Reference projects:
Hi Justin, is this something I could use to listen for POSTs from another server, take data from within the POST and run a Powershell script to compose a JSON response?
LikeLike
Yes! Absolutely.
LikeLike
This may be a dumb question, but how would I capture the client’s request headers and their respective values?
LikeLike
Not dumb at all! Are you looking for anything specific? In a script you can capture the value from this object:
‘$script:Request.Headers’
Take a look at this script (it’s included in the Module as well)
https://github.com/jpsider/RestPS/blob/master/RestPS/bin/Invoke-VerifyUserAuth.ps1
Feel free to add an issue to GitHub if I’m missing something or you’d like to see a new feature. My goal is to make this module as simple and useful as possible.
Lemme know if you have additional question!
Thanks!
LikeLike
This is perfect. Thank you!
LikeLike
Hi Justin, can I use this Module to create an HTTPS endpoint and transfer a file to the server from the client over the internet?
LikeLike
Yes, Absolutely.
LikeLike
Thanks for confirming. If you don’t mind, would you please refer to the requirements on creating https endpoint server
LikeLike
There are some basic instructions on GitHub – https://github.com/jpsider/RestPS
LikeLike