首页 > 解决方案 > 批处理脚本函数调用工作不正常

问题描述

我编写了简单的批处理脚本来根据参数执行两个单独的功能。但是,每当我在 cmd 中使用“service test”参数运行脚本时,它都会调用 ProcessKill 函数。有人能帮我理解这里发生了什么吗?

@Echo Off
set jobName=%1
set pidORServiceName=%2
echo %jobName%
echo %pidORServiceName%
cd "%~dp0"
>ServiceRestart.log 2>&1 (

if %jobName%==pkill (
    echo "Calling ProcessKill function"
    call:ProcessKill 
)
if %jobName%==service (
    echo "Calling RestartService function"
    call:RestartService 
)   
)

:ProcessKill
echo "killing the process" + %pidORServiceName%
taskkill /f /pid %pidORServiceName%
if %ERRORLEVEL% NEQ 0 ( 
   echo "Some error occurred with errorlevel" + %ERRORLEVEL%
) else (
   echo "killed the process"
)
goto:eof

:RestartService
echo "Restarting the service"
goto:eof

cmd运行输出:

ServicesRestart.bat service test
service
test
"killing the process" + test
ERROR: The process "test" not found.
"Some error occurred with errorlevel" + 128

服务重启日志

"Calling RestartService function"
"Restarting the service"

日志文件显示没有调用 ProcessKill 函数,但 cmd 中的输出显示它正在被调用,无法理解哪个是正确的。请注意。

标签: windowsbatch-filecmdscripting

解决方案


推荐阅读