使用以下方式创建Windows服务:
sc create ServiceName binPath= "the path"
如何将参数传递给Installer类的Context.Parameters集合?
根据我对sc.exe
文档的理解,这些参数只能通过binPath
的末尾传递,但我没有找到示例或成功执行此操作。
使用以下方式创建Windows服务:
sc create ServiceName binPath= "the path"
如何将参数传递给Installer类的Context.Parameters集合?
根据我对sc.exe
文档的理解,这些参数只能通过binPath
的末尾传递,但我没有找到示例或成功执行此操作。
sc create <servicename> binpath= "<pathtobinaryexecutable>" [option1] [option2] [optionN]
诀窍是在创建语句中在=之后留一个空格,并且对于包含特殊字符或空格的内容,使用" "。
建议为服务指定显示名称,并将启动设置为自动启动,以便它能够自动启动。您可以在创建语句中通过指定DisplayName=yourdisplayname
和start=auto
来实现这一点。
以下是一个示例:
C:\Documents and Settings\Administrator> sc create asperacentral
binPath= "C:\Program Files\Aspera\Enterprise Server\bin\Debug\asperacentral.exe"
DisplayName= "Aspera Central"
start= auto
如果这个起作用了,你应该看到:
[SC] CreateService SUCCESS
更新 1
binPath= "C:\..."
)之后必须有空格,否则这将无法正常工作。 - Onion-KnightbinPath
中传递需要引号的额外参数,则必须进行转义 (\"
)。例如:如果路径为 c:\some long path\some.exe "first argument"
,则需要将其写成 binPath= "\"c:\some long path\some.exe\" \"first argument\""
。请注意,该翻译保留了原文的意思,并尽可能使其更加通俗易懂。 - user692942binPath= ...
和DisplayName= ...
;在我的情况下,我忘记了在DisplayName之后加上“=”),那么控制台将打印出“create”命令的用法说明; 如:DESCRIPTION: Creates a service entry... USAGE: sc <server> create....
等等。 - Nate Anderson创建服务时的参数具有某些特殊的格式问题,特别是如果命令包含空格或引号:
如果您想为服务输入命令行参数,则必须用引号括起整个命令行。(并且始终在binPath =
之后和第一个引号之前留出一个空格,正如mrswadge指出的那样)
因此,要为命令PATH\COMMAND.EXE --param1=xyz
创建服务,您将使用以下binPath参数:
binPath= "PATH\COMMAND.EXE --param1=xyz"
^^ ^
|| |
space quote quote
如果可执行文件的路径中包含空格,则需要用引号将路径括起来。因此,若命令既有参数又有带空格的路径,则需要使用嵌套引号,并在内部引号前加上反斜杠\"进行转义。如果参数本身包含引号,则也需要进行转义。尽管使用反斜杠作为转义字符,但不必转义路径中包含的常规反斜杠,这与通常使用反斜杠作为转义字符的方式相反。因此,对于类似以下命令的情况:"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
这里是从SVNserve文档中提取的一个具体示例,展示了所有特殊情况:
sc create svnserve
binpath= "\"C:\Program Files\CollabNet Subversion Server\svnserve.exe\" --service -r \"C:\my repositories\" "
displayname= "Subversion Server" depend= Tcpip start= auto
这将使用命令行 "C:\Program Files\CollabNet Subversion Server\svnserve.exe" --service -r "C:\my repositories"
添加一个新的服务。
binpath=_
,displayname=_
和depend=_
binPath= "myservice.exe"
。命令行解释器需要这样做,并要求使用空格作为分隔符将命令进行标记化。 - mrswadgesc.exe create
上。 - Josephsc create "YOURSERVICENAME" binpath= "\"C:\Program Files (x86)\Microsoft SQL Server\MSSQL11\MSSQL\Binn\sqlservr.exe\" -sOPTIONALSWITCH" start= auto
我在Windows 7上使用时遇到了问题。它似乎忽略了我传递的第一个参数,所以我使用了binPath="C:\path\to\service.exe -bogusarg -realarg1 -realarg2"
,这样它就可以工作了。
我曾经只是不带参数地创建它,然后编辑注册表HKLM\System\CurrentControlSet\Services\[YourService]
。
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
class MyService : ServiceBase
{
protected override void OnStart(string[] args)
{
//
}
}
sc create myService binpath= "MeyService.exe arg1 arg2"
args
变量访问参数。binPath
或ImagePath
参数。相反,它建议将逻辑放在OnStart
方法中,然后使用(C#)Environment.GetCommandLineArgs();
。arg1
,我需要这样做:class MyService : ServiceBase
{
protected override void OnStart(string[] args)
{
log.Info("arg1 == "+Environment.GetCommandLineArgs()[1]);
}
}
arg1 == arg1
sc create startSvn binPath= "\"C:\Subversion\bin\svnserve.exe\" --service -r \"C:\SVN_Repository\"" displayname= "MyServer" depend= tcpip start= auto
它在 Powershell 中不起作用,应该在我的情况下使用 CMD
binPath= "c:\abc\def.exe /Param1=ghi"
看起来是正确的想法。反斜杠需要转义吗(即 "c:\abc\...")?最坏的情况下,如果SC.EXE无法完成操作,您可以直接编辑注册表值。 - ewall