批处理文件用于启动/停止服务并返回所有错误代码。

huangapple go评论62阅读模式
英文:

Batchfile to start/stop services returns all error codes

问题

我想创建一个批处理文件,以便在不需要时快速启动/停止某些服务以节省资源。我制作的文件完成了工作,但在运行时,如果出现问题,它会显示我要求它显示的所有错误代码。

@echo off
net stop scvpn
if ERRORLEVEL 0 goto 0
if ERRORLEVEL 1 goto 1
if ERRORLEVEL 2 goto 2
if ERRORLEVEL 3 goto 3
if ERRORLEVEL 5 goto 5
if ERRORLEVEL 6 goto 6
if ERRORLEVEL 14 goto 14
if ERRORLEVEL 24 goto 24
exit
:0
echo Sophos Connect Service已启动。
:1
echo 不支持的功能。
:2
echo 拒绝访问,服务未停止。
:3
echo 有依赖的服务正在运行。
:5
echo 无法接受控制。
:6
echo Sophos Connect Service未运行。
:9
echo 路径未找到。
:14
echo Sophos Connect Service已禁用。
:24
echo Sophos Connect Service已暂停。
pause

这是我得到的输出。有人能告诉我我的错误在哪里吗?我对这种启动/停止服务的方式还相当陌生...

上述脚本的输出

如果希望脚本只显示与问题相关的错误代码而不是显示所有错误代码,那将会更好。

英文:

I want to make a batchfile to quickly start/stop certain services to conserve resources when they're not needed. The file I made does the job but when run it gives me all error codes that I told it to show me if something goes wrong.

@echo off
net stop scvpn
if ERRORLEVEL 0 goto 0
if ERRORLEVEL 1 goto 1
if ERRORLEVEL 2 goto 2
if ERRORLEVEL 3 goto 3
if ERRORLEVEL 5 goto 5
if ERRORLEVEL 6 goto 6
if ERRORLEVEL 14 goto 14
if ERRORLEVEL 24 goto 24
exit
:0
echo Sophos Connect Service started.
:1
echo Function not supported.
:2
echo Access is denied, service not stopped.
:3
echo A dependent service is running.
:5
echo Control cannot be accepted.
:6
echo Sophos Connect Service is not running.
:9
echo Path not found.
:14
echo Sophos Connect Service is disabled.
:24
echo Sophos Connect Service is already paused.
pause

This is the output I get. Can anyone tell me what my error is? I'm fairly new to this way of starting/stopping services...
Output of above script

It would be nice if the script only shows the one code that's relevant instead of showing all of them.

答案1

得分: 0

以下是翻译好的部分:

首先,if errorlevel x 的语法意味着“如果 %errorlevel% 大于等于 x”,这意味着 if errorlevel 0 总是会执行。解决这个问题的传统方法是要么按照从大到小的顺序列出 errorlevel 值,要么像处理普通变量一样使用 %errorlevel%

其次,标签只是你脚本中的路标,表示“你在这里”。没有什么可以阻止脚本继续执行下一节,而且由于你没有使用 gotoexit,每个标签都会被执行(因为 if errorlevel 0 总是告诉脚本去执行 :0)。

另外,你不需要使用 goto,你可以根据 %errorlevel% 的值来显示文本,就像这样:

@echo off
net stop scvpn
if "%errorlevel%"=="0" echo Sophos Connect Service started.
if "%errorlevel%"=="1" echo Function not supported.
if "%errorlevel%"=="2" echo Access is denied, service not stopped.
if "%errorlevel%"=="3" echo A dependent service is running.
if "%errorlevel%"=="5" echo Control cannot be accepted.
if "%errorlevel%"=="6" echo Sophos Connect Service is not running.
if "%errorlevel%"=="9" echo Path not found.
if "%errorlevel%"=="14" echo Sophos Connect Service is disabled.
if "%errorlevel%"=="24" echo Sophos Connect Service is already paused.
pause
英文:

Two main things are wrong with your script.

First, the syntax if errorlevel x means "if %errorlevel% is x or greater," which means that if errorlevel 0 will always be executed. The traditional ways to get around this are to either list the errorlevel values from largest to smallest or to use %errorlevel% like a regular variable.

Second, labels are just signposts in your script that say "you are here." There is nothing to prevent the script from continuing to the next section, and since you don't have gotos or exits, every single label gets entered (because if errorlevel 0 always tells the script to go to :0).

As a side note, you don't need gotos at all; you can just display the text based on the value of %errorlevel%, like this:

@echo off
net stop scvpn
if "%errorlevel%"=="0" echo Sophos Connect Service started.
if "%errorlevel%"=="1" echo Function not supported.
if "%errorlevel%"=="2" echo Access is denied, service not stopped.
if "%errorlevel%"=="3" echo A dependent service is running.
if "%errorlevel%"=="5" echo Control cannot be accepted.
if "%errorlevel%"=="6" echo Sophos Connect Service is not running.
if "%errorlevel%"=="9" echo Path not found.
if "%errorlevel%"=="14" echo Sophos Connect Service is disabled.
if "%errorlevel%"=="24" echo Sophos Connect Service is already paused.
pause

huangapple
  • 本文由 发表于 2023年7月10日 18:51:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定