Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows services 使用sc.exe创建服务时,如何传入上下文参数?_Windows Services - Fatal编程技术网

Windows services 使用sc.exe创建服务时,如何传入上下文参数?

Windows services 使用sc.exe创建服务时,如何传入上下文参数?,windows-services,Windows Services,使用以下命令创建Windows服务时: sc create ServiceName binPath= "the path" 如何将参数传递到安装程序类的Context.Parameters集合 我对sc.exe文档的理解是,这样的参数只能在binPath的末尾传递,但我没有找到一个例子,也没有成功地做到这一点。请确保在binPath值的开头和结尾都有引号。我在Windows 7上无法实现这一点。它似乎忽略了我传入的第一个参数,所以我使用了binPath=“C:\path\to\service.

使用以下命令创建Windows服务时:

sc create ServiceName binPath= "the path"
如何将参数传递到安装程序类的Context.Parameters集合


我对
sc.exe
文档的理解是,这样的参数只能在
binPath
的末尾传递,但我没有找到一个例子,也没有成功地做到这一点。

请确保在binPath值的开头和结尾都有引号。

我在Windows 7上无法实现这一点。它似乎忽略了我传入的第一个参数,所以我使用了
binPath=“C:\path\to\service.exe-bogusarg-realarg1-realarg2”
,它成功了

sc create <servicename> binpath= "<pathtobinaryexecutable>" [option1] [option2] [optionN]
如果成功,您应该看到:

[SC] CreateService SUCCESS
更新1


已创建服务的参数有一些特殊的格式化问题,特别是当命令包含空格或引号时:

如果要为服务输入命令行参数,则必须将整个命令行括在引号中。(如mrswadge所指出的,始终在
binPath=
之后和第一个引号之前留一个空格)

因此,要为命令
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
以下是SVN服务文档中的一个具体示例,其中显示了所有特殊情况:

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”
添加一个新服务

总之是这样的
  • 每个sc参数后的空格:
    binpath=\u
    displayname=\u
    dependent=\u
  • 包含空格的每个sc参数必须用引号括起来
  • binpath中的所有附加引号都用反斜杠转义:\”
  • binpath中的所有反斜杠都不会转义

我过去只创建它,不带参数,然后编辑注册表
HKLM\System\CurrentControlSet\Services\[YourService]

我无法处理您的提案的问题,最后使用x86文件夹,它只在power shell(windows server 2012)中使用环境变量工作:

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

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

我找到了一种使用sc的方法

sc config binPath=“\”c:\path中有空格\service\u executable.exe“”

换言之,使用\来转义您希望在传输到注册表中后继续生存的任何“

sc create "YOURSERVICENAME" binpath= "\"C:\Program Files (x86)\Microsoft SQL Server\MSSQL11\MSSQL\Binn\sqlservr.exe\" -sOPTIONALSWITCH" start= auto sc create“YOURSERVICENAME”binpath=“\”C:\Program Files(x86)\Microsoft SQL Server\MSSQLS11\MSSQL\Binn\sqlservr.exe\”-sooptionalswitch“start=auto
请参阅此处:

如果您尝试了上述所有操作,但仍然无法将参数传递给您的服务,如果您的服务是用C/C++编写的,那么问题可能在于:当您通过“sc start arg1 arg2…”启动服务时,sc会直接使用这些参数调用服务的ServiceMain函数。但是,当Windows启动您的服务时(例如,在启动时)调用的是服务的主函数(_tmain),带有注册表“binPath”中的参数。

考虑如何访问应用程序代码中的参数也很重要

在我的c#应用程序中,我使用了ServiceBase类:

 class MyService : ServiceBase
{

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

sc create myService binpath=“MeyService.exe arg1 arg2”

但当我将其作为服务运行时,无法通过
args
变量访问参数

MSDN文档建议不要使用Main方法检索
binPath
ImagePath
参数。相反,它建议将逻辑放在
OnStart
方法中,然后使用(C#)
环境。GetCommandLineArgs()

要访问第一个参数
arg1
,我需要执行以下操作:

class MyService : ServiceBase
 {

    protected override void OnStart(string[] args)
    {

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

       }
 }
这会打印出来

       arg1 == arg1

使用反斜杠和许多双引号的服务创建示例

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

它在Powershell中不起作用,在我的情况下应该使用CMD

给定路径“c:\abc\def.exe”,我试图像这样传入Param1=“ghi”:binPath=“c:\abc\def.exe/Param1=ghi”。但是没有用……只要看一下注册表中的服务键,就会发现任何需要的参数都包含在ImagePath值中,因此您的
binPath=”c:\abc\def.exe/Param1=ghi“
似乎是正确的想法。是否需要转义反斜杠(即“c:\\abc\\…”)?最糟糕的是,如果SC.EXE做不到,您可以在以后直接编辑注册表值。我放弃了SC.EXE,而是像这样使用installutil.EXE:installutil.EXE/ServiceName=“TheName”/targetdir=“C:\TheInstallDirectory\”/PackageRoot=“PackageRootPath”我使用installutil.EXE,对于较旧的技术,我使用Windows XP中的Instsrv.EXE/2003 Resource Ket。我发现确保binPath=和值“myservice.exe”之间有一个空格是很重要的。例如,
binPath=“myservice.exe
。命令行解释器必须预料到这一点,并要求通过使用空格作为分隔符将命令标记化。我尝试了这种方法,结果成功了。SC.exe“\\ServerName”创建“ServiceName”BinPath=“SampleService.exe”请记住,BinPath=(
BinPath=“C:\…”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