When creating a service with sc.exe how to pass in context parameters?

Windows Services

Windows Services Problem Overview


When creating Windows service using:

sc create ServiceName binPath= "the path"

how can arguments be passed to the Installer class's Context.Parameters collection?

My reading of the sc.exe documentation is that such arguments could only be passed on the end of binPath, but I have not found an example or been able to successfully do this.

Windows Services Solutions


Solution 1 - Windows Services

sc create <servicename> binpath= "<pathtobinaryexecutable>" [option1] [option2] [optionN]

The trick is to leave a space after the = in your create statement, and also to use " " for anything containing special characters or spaces.

It is advisable to specify a Display Name for the service as well as setting the start setting to auto so that it starts automatically. You can do this by specifying DisplayName= yourdisplayname and start= auto in your create statement.

Here is an example:

C:\Documents and Settings\Administrator> sc create asperacentral 
binPath= "C:\Program Files\Aspera\Enterprise Server\bin\Debug\asperacentral.exe" 
DisplayName= "Aspera Central" 
start= auto

If this worked you should see:

[SC] CreateService SUCCESS

UPDATE 1

http://support.microsoft.com/kb/251192

Solution 2 - Windows Services

Parameters for created services have some peculiar formating issues, in particular if the command includes spaces or quotes:

If you want to enter command line parameters for the service, you have to enclose the whole command line in quotes. (And always leave a space after binPath= and before the first quote, as mrswadge pointed out)

So, to create a service for the command PATH\COMMAND.EXE --param1=xyz you would use the following binPath parameter:

binPath= "PATH\COMMAND.EXE --param1=xyz"
        ^^                             ^
        ||                             |
  space    quote                     quote

If the path to the executable contains spaces, you have to enclose the path in quotes.

So for a command that has both parameters and a path with spaces, you need nested quotes. You have to escape the inner quotes with backslashes ". The same holds if the parameters themselves contain quotes, you will need to escape those too.

Despite using backslashes as escape characters, you do not have to escape the regular backslashes contained in the path. This is contrary to how you normally use backslashes as escape characters.

So for a command like
"PATH WITH SPACES \COMMAND.EXE" --param-with-quotes="a b c" --param2:

binPath= "\"PATH WITH SPACES \COMMAND.EXE\" --param-with-quotes=\"a b c\" --param2"
         ^ ^                 ^           ^                      ^       ^         ^
         | |                 |           |                      |       |         | 
 opening     escaped      regular     escaped                    escaped       closing
   quote     quote       backslash    closing                    quotes          quote
     for     for            in         quote                      for              for
   whole     path          path       for path                  parameter        whole
 command                                                                       command

Here is a concrete example from the SVNserve documentation, which shows all special cases:

sc create svnserve 
   binpath= "\"C:\Program Files\CollabNet Subversion Server\svnserve.exe\" --service -r \"C:\my repositories\"  "
   displayname= "Subversion Server" depend= Tcpip start= auto 

(linebreaks are added for readability, do not include them)

This would add a new service with the command line "C:\Program Files\CollabNet Subversion Server\svnserve.exe" --service -r "C:\my repositories".

So in summary

  • space after each sc parameter: binpath=_, displayname=_ and depend=_
  • each sc parameter that contains spaces must be enclosed in quotes
  • all additional quotes inside the binpath are escaped with backslashes: "
  • all backslashes inside the binpath are not escaped

Solution 3 - Windows Services

sc create "YOURSERVICENAME" binpath= ""C:\Program Files (x86)\Microsoft SQL Server\MSSQL11\MSSQL\Binn\sqlservr.exe" -sOPTIONALSWITCH" start= auto

See here: https://stackoverflow.com/questions/7190480/modifying-the-path-to-executable-of-a-windows-service

Solution 4 - Windows Services

I had issues getting this to work on Windows 7. It seemed to ignore the first argument I passed in so I used binPath= "C:\path\to\service.exe -bogusarg -realarg1 -realarg2" and it worked.

Solution 5 - Windows Services

I use to just create it without parameters, and then edit the registry HKLM\System\CurrentControlSet\Services\[YourService].

Solution 6 - Windows Services

It also important taking in account how you access the Arguments in the code of the application.

In my c# application I used the ServiceBase class:

 class MyService : ServiceBase
{

    protected override void OnStart(string[] args)
    {
       }
 }

I registered my service using

sc create myService binpath= "MeyService.exe arg1 arg2"

But I couldn't access the arguments through the args variable when I run it as a service.

The MSDN documentation suggests not using the Main method to retrieve the binPath or ImagePath arguments. Instead it suggests placing your logic in the OnStart method and then using (C#) Environment.GetCommandLineArgs();.

To access the first arguments arg1 I need to do like this:

class MyService : ServiceBase
 {

    protected override void OnStart(string[] args)
    {

                log.Info("arg1 == "+Environment.GetCommandLineArgs()[1]);

       }
 }

this would print

       arg1 == arg1

Solution 7 - Windows Services

A service creation example of using backslashes with many double quotes.

C:\Windows\system32>sc.exe create teagent binpath= "\"C:\Program Files\Tripwire\TE\Agent\bin\wrapper.exe\" -s \"C:\Program Files\Tripwire\TE\Agent\bin\agent.conf\"" DisplayName= "Tripwire Enterprise Agent"

[SC] CreateService SUCCESS

Solution 8 - Windows Services

This command works:

sc create startSvn binPath= "\"C:\Subversion\bin\svnserve.exe\" --service -r \"C:\SVN_Repository\"" displayname= "MyServer" depend= tcpip start= auto

Solution 9 - Windows Services

I found a way to use sc.

sc config binPath= ""c:\path with spaces in it\service_executable.exe" "

In other words, use \ to escape any "'s you want to survive the transit into the registry.

Solution 10 - Windows Services

Be sure to have quotes at beginning and end of your binPath value.

Solution 11 - Windows Services

I couldn't handle the issue with your proposals, at the end with the x86 folder it only worked in power shell (windows server 2012) using environment variables:

{sc.exe create svnserve binpath= "${env:programfiles(x86)}/subversion/bin/svnserve.exe --service -r C:/svnrepositories/"   displayname= "Subversion Server" depend= Tcpip start= auto}

Solution 12 - Windows Services

If you tried all of the above and still can't pass args to your service, if your service was written in C/C++, here's what could be the problem: when you start your service through "sc start arg1 arg2...", SC calls your service's ServiceMain function directly with those args. But when Windows start your service (at boot time, for example), it's your service's main function (_tmain) that's called, with params from the registry's "binPath".

Solution 13 - Windows Services

it is not working in the Powershell and should use CMD in my case

Solution 14 - Windows Services

In W2008 Server you still need the space, e.g. binPath= "", but in W2019 Server it is fixed, and binPath="" works

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsympatric gregView Question on Stackoverflow
Solution 1 - Windows ServicesMhmdView Answer on Stackoverflow
Solution 2 - Windows ServicesHugoRuneView Answer on Stackoverflow
Solution 3 - Windows ServicesimjustarahView Answer on Stackoverflow
Solution 4 - Windows ServicesCraig CarpenterView Answer on Stackoverflow
Solution 5 - Windows ServicesTonyView Answer on Stackoverflow
Solution 6 - Windows ServicesPancizView Answer on Stackoverflow
Solution 7 - Windows ServicesNash AView Answer on Stackoverflow
Solution 8 - Windows Servicesuser2535091View Answer on Stackoverflow
Solution 9 - Windows ServicesRem OnyshczakView Answer on Stackoverflow
Solution 10 - Windows ServicescripoxView Answer on Stackoverflow
Solution 11 - Windows Servicesg0r1llaView Answer on Stackoverflow
Solution 12 - Windows ServicesBlueRiverView Answer on Stackoverflow
Solution 13 - Windows ServicesWilliamView Answer on Stackoverflow
Solution 14 - Windows ServicesCoder DevView Answer on Stackoverflow