DSorlov / Sorlov.PowerShell

This is the public version of Sorlov.PowerShell Self-Hosted Executable Extensions for Powershell
http://the.powershell.zone/software/sorlov-powershell/
GNU General Public License v3.0
26 stars 8 forks source link

Question: Service Creation - OnStart/OnStop #4

Closed gw-bcharboneau closed 4 years ago

gw-bcharboneau commented 4 years ago

Hello @DSorlov, First, Thank you for the amazing Module. I was just curious if there is any way to specify an OnStart and OnStop Function as part of my code and have them run before/after the main function based on if the service is starting or stopping.

This would be helpful so that if the service is started or stopped, it can write a log or perform certain non-loop functions outside of the main function.

DSorlov commented 4 years ago

Not right now, there is nothing like that implemented. However it would not be very difficult to implement. If you have code I will be happy to accept it into the base, however updates have been very slow since I dont use PowerShell everyday anymore myself. I could look into it if I get the time but no promises...

DSorlov commented 4 years ago

@gw-bcharboneau I have looked into it and will do an effort to actually make that work along with actions for install and uninstall. Could be a while before I get time however! =)

DSorlov commented 4 years ago

@gw-bcharboneau Ok, I was bored this morning so implemented it pretty quickly. Just pull down and build from source; I have pushed a updated sourceto the repo. I will update PSGallery when I have the time.

What I did was if the function OnStart() is defined it will be run before the Main() method is called, and after the Main() have exited it will call the OnStop($method) before stopping completely (this is however subject to Windows server timeouts and should be kept to a very quick job. $method will either be OnStop or OnShutdown depending on how the service was stopped but can also be ignored completely.

Hope this works for you! =)

gw-bcharboneau commented 4 years ago

should work nicely, Thank you very much for the quick turnaround. I will attempt to build from source today and test.