mtkennerly / shawl

Windows service wrapper for arbitrary commands
MIT License
495 stars 15 forks source link

Not able to create service with powershell #30

Closed mlenz968 closed 1 year ago

mlenz968 commented 1 year ago

I want to start a powershell script with shawl but as soon as I have powershell on the shawl command line the Windows service is broken. Of course my original service registration has more arguments but I could reduce it to this simple service registration to reproduce the problem: shawl add --name PS_Test -- "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File service_master.ps1" or even shawl add --name PS_Test -- powershell -File service_master.ps1

If I try to open the service properties in the Windows Services I get an error message The system cannot find the file specified.

Also the service description only contains an error message: <Failed to Read Description. Error Code: 2 >

Does anybody have an idea what could be the root cause and how to solve it?

mtkennerly commented 1 year ago

shawl add --name PS_Test -- "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File service_master.ps1"

There are a couple of problems with this command:

For example, if the script is in C:\scripts\service_master.ps1, then you could try one of these:

mlenz968 commented 1 year ago

I also tried the following variants but they all had the same problem:

If I put the powershell -File C:\service_test\service-master.ps1 into a service-master.cmd and register the service via shawl add --name PS_Test -- C:\service_test\service-master.cmd the service registration is ok.

Also the graceful shutdown of my application via ctrl-c works.

The only drawback is that after the graceful shutdown of the app the script does not terminate because the command interpreter waits for the "Terminate batch job (Y/N)" confirmation.

mlenz968 commented 1 year ago

I made another test.

I created a symbolic link to powershell mklink ps.exe C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe and used ps.exe in the command. This time everything is ok.

But if I use powershell.exe as link name then I have the same problems as before.

That's really strange.

But at least I have a workaround for this problem.

mtkennerly commented 1 year ago

Yeah, that's really weird. Unfortunately, I'm not able to reproduce it. This worked for me on Windows 11 with PowerShell 5.1.22621.1778:

What happens if you try using the same test script?

I was also able to just do shawl.exe add --name PS_Test --cwd C:\tmp -- script.ps1 without specifically referencing powershell. Does that work for you, so you can avoid the symbolic link?