How to create windows service to run a powershell script?
Asked Answered
P

4

19

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 !!!

Particularity answered 22/10, 2014 at 11:4 Comment(7)
possible duplicate of Can you write Windows services in PowershellPolarization
look at: get-help New-Service. Also specify the full path to powershell.exeBeth
You can't just run any executable as a service. It has to respond to requests of the service control manager. Typically, in C# this would involve inheriting from System.ServiceProcess.ServiceBase. Are you doing anything like that in test.ps1?Hannah
@mikez , No im not doing anything like C# stuff. its just a PS1 script which has infinite loop & writes numbers into a fileParticularity
Try creating service which runs with local ID. Also check the execution policy.Quaff
I was able to install service from .ps1 with nssm super quickly. It is much easier then WINSW an I don't understand the negative votes for nssm. Could somebody please comment on this? nssm is available in chocolatey repository, hence very painless installation.Quadrivial
You can make Powershell run as a service with just the built-in tool sc.exe. But the service will never state that it was started successfully, which kind of defies the purpose.Aeneas
R
6

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/

Repatriate answered 3/1, 2018 at 3:46 Comment(4)
I wouldn't normally post links in an answer as they are subject to breaking but these are very long articles.Repatriate
The magazine contains dead links using bit.ly. Here's the french guy repo: github.com/JFLarvoire/SysToolsLib/blob/master/PowerShell/…Bramante
You don't need to post everything, but you should post the basics.Prakrit
Thank you. This is a very useful, simple to setup infrastructure. @Bramante the github link was invaluable.Oxblood
C
3

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.

Cordate answered 8/12, 2019 at 14:17 Comment(0)
T
0

I have had success with Non-sucking Service Manager (NSSM) as well. https://nssm.cc/

Tombouctou answered 16/9, 2019 at 21:35 Comment(1)
I've subsequently used this and it works well and is easy to useRepatriate
K
-7

you can create by below step:

  1. First go to the path where PowerShell script located.
  2. Then run below Command: powershell.exe -ExecutionPolicy UnRestricted -File .\filename.ps1
Korfonta answered 22/9, 2020 at 11:35 Comment(1)
This does not work under any scenarioWoolson

© 2022 - 2024 — McMap. All rights reserved.