r/PowerShell • u/NotNotWrongUsually • Oct 06 '19
PSApi: ultra-fast way to create a webservice
I've published PSApi to the Powershell Gallery today. It is intended as a super-lightweight way to create a webservice. Quickest way to explain is probably with a screenshot.
The design goals were:
To be minimalist. If you can write a function you can create a webservice from it. Defaults should do most of the heavy lifting.
To be efficient. The "server" is multi-threaded so should be able to handle quite a lot of requests.
To work cross platform.
To be relatively secure*. Commands are walked for dependencies and custom runspaces are built from them.
(Security is hard. I encourage anyone to see if they can exploit this somehow. I would love to hear about it so it can be fixed.)
The module is on the gallery, so you can install it with
Install-Module PSApi -Scope 'CurrentUser'Source is available on GitHub. This is version 0.0.9. Expect some bugs, and please do let me know, if you run into any. Contributions are welcome, as long as they fit within the design goals outlined above.
The use cases I envisioned for this was creating monitoring points, the ability to create simple mock-up services, and generally being yet another tool in the box used to "glue" PowerShell together with other things. It can do a whole lot more than that, though.
Here is a simple function for getting user pictures from your AD, provided you've populated the thumbnail property (expanded version here that also does high quality rescaling). It uses ADSI, because Core. Publish-Command UserPhoto and you have an image service.
function UserPhoto ($user) {
$user_data = ([adsisearcher]"(&(objectClass=user)(sAMAccountName=$user))").FindAll()
$stream = [System.Io.MemoryStream]::new($user_data.Properties["thumbnailphoto"][0])
$img = [System.Drawing.Image]::FromStream($stream)
$img
}Lots more information can be found on GitHub, regarding usage, how to run as non-administrator, how to post funky parameters, etc. But generally: just Publish-Command <function> and you're done! :)
Enjoy!
Edit: *Just a clarification. Security in this context doesn't mean authentication, this module provides none. Focus on security here is in trying to protect the integrity of the machine serving the commands.
4
u/Sekers Oct 06 '19
This is neat. Any issues with Powershell continuously running such as random crashes or increasing memory consumption?
4
u/NotNotWrongUsually Oct 06 '19 edited Oct 07 '19
Good question! I did some testing during development, but that was many edits ago, so decided to repeat.
The test:
Publish-Command Get-Processfrom pwshOpen two regular Windows PowerShell windows and have them hammer away
Invoke-WebRequestas fast as possible.At start pwsh was listing as using 56.9 MB of memory.
After about ten minutes and 7,556 requests, the usage was 92.9 MB when i stopped the requests. So there is some leak somewhere (after 21,000 requests you'd have leaked 100MB).
I'll see if I can track it down, but I'm not sure if it something I can release, or if it is something in the shell building up.
As for leaving the shell open with a running process for a long time, I've done that before with no problem.
Edit: fixed and uploaded to the gallery in v0.0.10
0
Oct 06 '19
[deleted]
4
u/NotNotWrongUsually Oct 06 '19 edited Oct 06 '19
Still had the window open, so yes, I can. Forced GC reduced memory consumption, but only to 85.7 MB.
Still, for the purposes I had intended for this, I suppose it could run for years without having to be restarted as it is. Pretty sure someone will restart the shell or machine for other reasons before the memory issue become a problem.
Of course, other people might have other purposes, so I am going to track down the leak if I can. Any and all help in that regard is welcome. I just went over the code, but am not seeing any red flags.
3
u/NotNotWrongUsually Oct 07 '19
Happy to say I've found the solution for this one. Memory usage is now rock solid!
Unfortunately I'm currently behind a million proxies and firewalls so can't push code to GitHub. It'll be added when I'm home ;)
5
0
Oct 07 '19
[deleted]
3
u/NotNotWrongUsually Oct 07 '19 edited Oct 07 '19
Strange. If you use another variable name than $Name everything seems to work just fine. There must be some bug in the dependency walker which I need to sort out. For now, you can workaround your problem by not using $Name. This works fine:
function GetSvcJson { param( [Parameter(Mandatory=$true)] $svc ) Get-Service -Name $svc | ConvertTo-Json }Yes. That is odd. The dependency walker can be a bit special at times.
Curious side effect of your code that I should really get around to documenting: when you set a parameter to mandatory, you will actually get prompted to supply a value for it in the shell, if you don't supply it in the browser.
This is obviously not much good to a remote user, but there is no setting to prevent this behavior in Powershell. I'm torn on whether I should manipulate the function when putting it in the runspace or not. It is possible to convert the parameter to a non-mandatory parameter that does a
throwif it isn't filled in. This prevents the behaviour.Edit: In this particular case you can remove the problem by just removing the mandatory on the parameter btw. The service will throw an error 500 with an explanation if it isn't included anyways, which is much desirable compared to the behaviour described above.
Edit2: I've located the bug. The dependency walker was looking in its own scope. There is a variable called $Name there already. It confuses yours with its own. A cascade of fail follows. I have some code sitting on my own machine to sort it out, but it needs to be more thoroughly tested before I put it on GitHub. That is going to have to be tomorrow.
2
u/NotNotWrongUsually Oct 08 '19
This is now fixed, and Update-Module should get you v.0.0.11 without the bug. It was specifically the variable name $Name that had this problem, and no other.
I officially extend my apology to any software developer at whom I have cursed 'How was this not found in testing!'.
9
u/joerod Oct 06 '19
What authentication methods does this support?