PS script running as a service

M Smith 21 Reputation points
2020-08-24T18:41:41.407+00:00

Trying to get a script to run as a service using NSSM, following this guide
Service appears to be installed.

PS C:\powershell> Get-Service $servicename

Status Name DisplayName
Stopped PMimagewatcher PMimagewatcher

When I try to start it, get this error
PS C:\powershell> Start-Service $servicename
Start-Service : Failed to start service 'PMimagewatcher (PMimagewatcher)'.
At line:1 char:1

  • Start-Service $servicename
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], S
    erviceCommandException
  • FullyQualifiedErrorId : StartServiceFailed,Microsoft.PowerShell.Commands.StartServiceCommand

I should add, I'm only looking for someway to ensure that this script runs continually when the server is up. A service seems like the logical choice, but if there is some other way to do this then I'm open to it.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,546 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 34,346 Reputation points
    2020-08-25T13:12:10.573+00:00

    Check the system eventlog to see if there is an entry with more information about why the service did not start. Look in Services.msc. Is the service set to run as the system account? Does it need to run as some user account? If you try to start it from there, do you get a different error message?

    In the Powershell code, add a Start-Transcript cmdlet to write to a log file. That will at least let you know that NSSM/Powershell is launching the script.

    If you don't get a transcript, there must be something wrong calling Powershell. Download and run Process Monitor. Start the procmon capture, start the service, and then stop the capture. It will capture a lot of events. Then look for nssm.exe and powershell.exe and see if there are any access denied errors and if Powershell.exe is being launched with the correct parameters.


1 additional answer

Sort by: Most helpful
  1. 2020-08-25T06:48:19.193+00:00

    From the error message, the service you want to start cannot be started. But according to the explanation of the command, If a service is already running, the message is ignored without error. It is recommended that you check again whether the service you want to start exists.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.