sc.exe 在批处理文件中无法识别

sc.exe is not recognized while in a batch file

这是我们必须用来构建批处理文件的代码,它将连接到我们将用于 MongoDB 的服务器。 **** 是我的用户名帐户,不想与任何人共享。

sc.exe create MongoDB binPath = 
"C:\Users\****\Desktop\FanshaweCollege\semester3\Servers\bin\mongod.exe 
--service --config=\"C:\Users\****\Desktop\FanshaweCollege\semester3\Servers\mongodb.cfg\"" 
DisplayName= "MongoDB" start= "auto"

老师不会帮助我们,他不知道问题出在哪里。我开始搜索google,但我唯一能找到的是我需要更改环境变量,并将其设置为全部。

错误是:

 'sc.exe' is not recognized as an internal or external command, operable program or batch file.

我怎样才能让它工作

您需要将sc.exe的位置添加到环境变量"Path"

尝试where sc.exe搜索它在哪里。然后使用该命令的完整路径。

sc.exe create MongoDB binPath = "C:\Users\****\Desktop\FanshaweCollege\semester3\Servers\bin\mongod.exe 
--service --config=\"C:\Users\****\Desktop\FanshaweCollege\semester3\Servers\mongodb.cfg\"" 
DisplayName= "MongoDB" start= "auto"

您必须处理 double/single 个引号("')。不确定您可以在路径中使用 \ 执行此操作,因为在您的示例中它可以被解释为一个文件夹。尝试插入符号 (^) 或双引号内的单引号

在您的示例中,正确的语法应该是:

C:\Windows\System32\sc.exe create MongoDB ^
    binPath="'C:\*****\mongod.exe' --service --config=^
    'C:\Users\****\Desktop\FanshaweCollege\semester3\Servers\mongodb.cfg'" ^
    DisplayName="MongoDB" start="auto"

注意:这里的插入符(^)是为了转义return.

或一行:

C:\Windows\System32\sc.exe create MongoDB binPath="'C:\*****\mongod.exe' --service --config='C:\Users\****\Desktop\FanshaweCollege\semester3\Servers\mongodb.cfg'" DisplayName="MongoDB" start="auto"