22

I'm trying to create and start a windows service using PowerShell.

The service is created but cannot be started when I use various names besides one particular name.

When I create the service with the name of the exe file it can be started but when I give it a different name it fails to start.

I'm running the PowerShell script as administrator.

Any advises?

function InstallService(
    [string] $MsDeployHost,
    [string] $ServiceName,
    [string] $DisplayName,
    [string] $ServicePath,
    [string] $ServiceDescription,
    [object] $Credential) {
    if($MsDeployHost -eq "local") {        
        New-Service -name $ServiceName -binaryPathName $ServicePath -displayName $ServiceName -StartupType Automatic
        Start-Service -name $ServiceName
    } else { ....  

The Error I get: Start-Service : Service 'Service1 (Service1)' cannot be started due to the following error: Cannot start service Service1 on computer '.'.

When I try to start it manually I get: "Error 1053: The service did not respond to the start or control request in a timely fashion"

Itai Mo
  • 333
  • 1
  • 2
  • 7

7 Answers7

8

The problem is that, unless your service is written to handle it, you need to use a particular service name in order to run a particular service (and note that the name is case-sensitive). This is because the service, on startup, needs to register with the Service Control Manager to receive start/stop notifications and send status updates, using its service name. If you install the service with a different name, but the executable has no way of knowing this (through a configuration setting or whatnot), this registration will fail and the service can't start (to the operating system, it will look as if the service is failing to respond).

You can set the display name to whatever you like, but you cannot use an arbitrary service name unless the service is designed to support this.

Jeroen Mostert
  • 27,176
  • 2
  • 52
  • 85
  • I was trying to install an API as a windows service using TopShelf. I was configuring Topshelf to use the service name "service1" but when I was installing using the powershell command "New-Service" I was using the name "service2". This comment made me realise why it wasn't working! – GIVE-ME-CHICKEN Jul 12 '19 at 13:33
  • @itai-mo How did this solve your problem? What did you change? – stackprotector Jul 20 '20 at 10:44
  • 2
    @jeroen-mostert What exactly has to be changed in the code of the question? What do you mean by "particular service name"? – stackprotector Jul 20 '20 at 10:47
  • 1
    @Thomas: ultimately, when the code calls [`StartServiceControlDispatcher`](https://docs.microsoft.com/windows/win32/api/winsvc/nf-winsvc-startservicectrldispatchera), it must, in the `SERVICE_TABLE_ENTRY` structure, supply a service name that the service control manager is expecting (i.e. the name that was used when the service was registered with [`CreateService`](https://docs.microsoft.com/windows/win32/api/winsvc/nf-winsvc-createservicea)). Now, what this actually means for your code depends a lot on how this function is ultimately called; people rarely use these Win32 functions directly. – Jeroen Mostert Jul 20 '20 at 19:20
  • 1
    In the particular case of PowerShell, it just means that when you call `New-Service`, you must use the service name the service code is expecting. When using a generic wrapper like Topshelf, this name is configurable, so you just have to make sure to use the same name in both the wrapper configuration and the service installation. For other service executables, the service name might not be configurable, and then you just have to know what name it's using. – Jeroen Mostert Jul 20 '20 at 19:23
  • 1
    And in case you're wondering why any of this is even necessary: it's possible to host multiple services in one executable, to cut down on resource usage. When the executable starts, it gives the service control manager a list of services it's hosting. When a service is started, the service control manager attempts to match the service name with the list of services the executable has provided. If there's no match, the service will appear to be missing in action. Without this feature, the SCM could simply assume the executable is hosting one service and it wouldn't need to match any names. – Jeroen Mostert Jul 20 '20 at 19:29
8

I had the same problem, for me the issue was that the service was supposed to run with a user's credentials and the user's password has changed - after which the service could not log in anymore and failed to run.

You can review the "Service Control Manager" log, immediately after the failure, like this:

> Get-EventLog -LogName System -Source 'Service Control Manager' -Newest 5

   ----- ----          ---------   ------                 ---------- -------
   19762 Sep 30 12:31  Error       Service Control M...   3221232472 The SERVICE_NAME service failed to start due to the following error: ...
   19761 Sep 30 12:31  Error       Service Control M...   3221232510 The SERVICE_NAME service was unable to log on as .\USERNAME with the currently configured password due to the following error: ...
#...
> (Get-EventLog -LogName System -Source 'Service Control Manager' -Newest 5)[1] | Format-List


Index              : 19764
EntryType          : Error
InstanceId         : 3221232510
Message            : The SERVICE_NAME service was unable to log on as .\USERNAME with the currently configured password due to the following error:
                     %%1326

                     To ensure that the service is configured properly, use the Services snap-in in Microsoft Management Console (MMC).
Category           : (0)
CategoryNumber     : 0
ReplacementStrings : {SERVICE_NAME, .\USERNAME, %%1326}
Source             : Service Control Manager
TimeGenerated      : 9/30/2021 12:35:02 PM
TimeWritten        : 9/30/2021 12:35:02 PM
UserName           :


Error 1326 is "Bad username or password".

Guss
  • 30,470
  • 17
  • 104
  • 128
  • Thanks, most helpful comment here, to get some details on why exactly it failed to start – Ali Jan 04 '22 at 15:31
3

My problem was that the service was Disabled in the services control manager.

I then put it in manual state and Start-Service worked.

hth

Boop
  • 1,217
  • 1
  • 15
  • 22
1

If this is also a .NET Console Application, you must call ServiceBase.Run in your main method in Program.cs. This fixed this error for me.

See more specific code here: .NET console application as Windows service

AndyClaw
  • 740
  • 8
  • 15
0

For me it was an incorrect value in appsettings.json. Did not escape some special characters.

SLCH000
  • 1,821
  • 2
  • 14
  • 15
0

This error message is very generic and does not say anything useful. The reason could be missing configuration files, missing .NET frameworks or anything that the program depends on.

To see the real reason open the Windows event log and look for any errors under Windows Logs -> Application.

Rye bread
  • 1,305
  • 2
  • 13
  • 36
0

I got the same error and found that I was missing installing the .Net 6 on the server. worth to look at event logs.

Rushabh Master
  • 450
  • 4
  • 13