sc.exeでサービスを作成する場合、コンテキストパラメータを渡す方法は?



When Creating Service With Sc



解決:

sc create binpath= '' [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 SettingsAdministrator> sc create asperacentral binPath= 'C:Program FilesAsperaEnterprise ServerinDebugasperacentral.exe' DisplayName= 'Aspera Central' start= auto  

If this worked you should see:



[SC] CreateService SUCCESS  

UPDATE 1

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


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 PATHCOMMAND.EXE --param1=xyz you would use the following binPath parameter:

binPath= 'PATHCOMMAND.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 FilesCollabNet Subversion Serversvnserve.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 FilesCollabNet Subversion Serversvnserve.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

sc create 'YOURSERVICENAME' binpath= ''C:Program Files (x86)Microsoft SQL ServerMSSQL11MSSQLBinnsqlservr.exe' -sOPTIONALSWITCH' start= auto 

ここを参照してください:Windowsサービスの「実行可能ファイルへのパス」の変更