无法在批处理脚本中使用 nssm set 命令

Can't use nssm set command in batch script

在我的批处理脚本中使用此命令时,set 命令被解释为默认 windows 批处理 "set an environment command":

@echo off
echo "Always run this script as Administrator. If you don't the service will be installed but not correctly configured!"

set serviceName=MyApp
set serviceLabel=MyApp
set serviceExe=MyApp.exe

set maindir=C:\Users\MyUser\Desktop\MyApp\
set nssm=%maindir%\lib\nssm-2.24\win64\nssm.exe

%nssm% install %serviceName% "%maindir%\%serviceExe%"

如何防止这种情况发生?

想通了:

在我的原始脚本中,我将 nssm exe 作为变量:

%nssm% set %serviceName% AppDirectory "%maindir%"

不知何故这会混淆脚本。

只需使用指向 nssm.exe 的实际路径即可:

.\nssm.exe set %serviceName% AppDirectory "%maindir%"

一个例子,基于我的评论:

@Echo Off
Set "serviceName=MyApp"
Set "serviceExe=MyApp.exe"
Set "maindir=C:\Users\MyUser\Desktop\MyApp"
Set "nssm=%maindir%\lib\nssm-2.24\win64\nssm.exe"

"%nssm:\=\%" install %serviceName% "%maindir%\%serviceExe%"