Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create windows service to run a powershell script?

I have infinite loop PowerShell (Testing purpose) script, which I want to run as a Service in Windows Server 2008 R2 (Standard).

I'm using the following command to create the Windows service,

sc.exe create "My PS1Service" binPath= "powershell.exe -NoLogo -Path D:\TEST\test.ps1"

And the result is [SC] CreateService SUCCESS

But when I try to run the service My PS1Service , it return the following error

Windows Could not srart the My PS1Service service on Local
Computer
Error 1053: The service did not respond to the start or control
request in a family

Any help here !!!

like image 676
HamTheAstroChimp Avatar asked Oct 22 '14 11:10

HamTheAstroChimp


2 Answers

I've been looking all over the place for a decent way to make a PS1 into a service. Easiest way I found is with Winsw: https://github.com/kohsuke/winsw

Works like a charm.

like image 85
JustAGuy Avatar answered Oct 10 '22 07:10

JustAGuy


Due to the length of the articles I don't want to paste them in the answer.

Check out this post which is the most comprehensive source of info I've found regarding powershell services: https://msdn.microsoft.com/en-us/magazine/mt703436.aspx

which links to an example powershell service: http://jf.larvoire.free.fr/progs/PSService.ps1

I didn't end up successfully modifying this as it was going to take me longer to read through it and understand it than it would to use a different method for my use case, but I'd certainly be going back to that if I needed to create a service in powershell in the future.

Also this: https://www.sapien.com/blog/2017/07/12/write-a-windows-service-in-powershell/

like image 20
Gostega Avatar answered Oct 10 '22 08:10

Gostega