Batch file 必须有更好的方法来做到这一点。(批处理文件帮助)

Batch file 必须有更好的方法来做到这一点。(批处理文件帮助),batch-file,Batch File,好吧,所以我正在寻找一个更好的方法来做这件事,我知道这是必须的,因为这越来越荒谬了。我想在文件中添加第三个服务,但这是对我使用的数字的大量篡改,我肯定会破坏一些东西 我真的很想学习,但我对这方面还很陌生,所以不要太苛刻 @echo off echo This will start your SERVICES. echo If you do not wish to do this, please close this window, otherwise: pause :CORE :DISA

好吧,所以我正在寻找一个更好的方法来做这件事,我知道这是必须的,因为这越来越荒谬了。我想在文件中添加第三个服务,但这是对我使用的数字的大量篡改,我肯定会破坏一些东西

我真的很想学习,但我对这方面还很陌生,所以不要太苛刻

@echo off
echo This will start your SERVICES. 
echo If you do not wish to do this, please close this window, otherwise: 
pause

:CORE

:DISABLED

for /F "tokens=3 delims=: " %%H in ('sc qc "SERVICE1" ^| findstr "        START_TYPE"') do (
  if /I "%%H" EQU "DISABLED" (
   echo.
   echo SERVICE1 is disabled!
   echo.
   set A=1
   set C=3
   set E=0
   set M=1
   goto FILE
  )
  if /I "%%H" NEQ "DISABLED" (
   set A=0
   set M=0
  )
)

:STATUS

for /F "tokens=3 delims=: " %%H in ('sc query "SERVICE1" ^| findstr "        STATE"') do (
  if /I "%%H" NEQ "RUNNING" (
  echo.
   echo Starting SERVICE1... & SC Start "SERVICE1" | findstr /i /c:"1069" && (Echo.
   echo ERROR 1069: SERVICE1 failed to start due to a unknown 
   echo username or bad password.
   echo.
   set E=3
   set C=3
    ) || (
   echo Successful
   echo.
   set C=0
   set E=0
    )
  )
  if /I "%%H" EQU "RUNNING" (
   echo.
   echo SERVICE1 is already started!
   echo.
   set C=1
   set E=0
  )
)

:FILE

:DISABLED

for /F "tokens=3 delims=: " %%H in ('sc qc "SERVICE2" ^| findstr "        START_TYPE"') do (
  if /I "%%H" EQU "DISABLED" (
   echo.
   echo SERVICE2 is disabled!
   echo.
   set B=1
   set D=3
   set F=0
   set N=1
   goto FIX
  )
  if /I "%%H" NEQ "DISABLED" (
   set B=0
   set N=0
  )
)

:STATUS

for /F "tokens=3 delims=: " %%H in ('sc query "SERVICE2" ^| findstr "        STATE"') do (
  if /I "%%H" NEQ "RUNNING" (
   echo Starting SERVICE2... & SC Start "SERVICE2" | findstr /i /c:"1069" && (Echo.
   echo ERROR 1069: SERVICE2 failed to start due to a unknown 
   echo username or bad password. 
   echo.
   set F=3
   set D=3
    ) || (
   echo Successful
   echo.
   set D=0
   set F=0
    )
  )
  if /I "%%H" EQU "RUNNING" (
   echo.
   echo SERVICE2 is already started!
   echo.
   set D=1
   set F=0
  )
)

:MESSAGE

set /a G=%C%+%D%
if "%G%" LEQ "1" (
   echo.
   echo Your services should now be started!
   echo.
)
if "%G%" EQU "2" (
   echo.
   echo No problem found!
   echo.
) 

:FIX

set /a O=%M%+%N%
if "%O%" EQU "2" (set S=Services&& goto HELP)
if "%M%" EQU "1" (set S=SERVICE1&& goto HELP)
if "%N%" EQU "1" (set S=SERVICE2&& goto HELP)

:HELP

set /a H=%A%+%B%
if "%H%" NEQ "0" (
   echo.
   echo To enable the %S%, follow these steps:
   echo ^(1^) Open your start menu.
   echo ^(2^) Type in ^"Services.msc" and click on the top option.
   echo ^(3^) Scroll down in the list to find the ^"SERVICE1" and 
   echo       the "SERVICE2".
   echo ^(4^) Right-click on each respective service and select properties.
   echo ^(5^) In the middle of the screen, change ^"Startup type" to "Automatic".
   echo ^(6^) Once this is done for the respective service^(s^), run this file again.
   echo.
   set P=1
)

set /a I=%E%+%F%
if "%I%" GEQ "3" (
   echo.
   echo To correct the 1069 error, follow these steps:
   echo ^(1^) Open your start menu.
   echo ^(2^) Type in ^"Services.msc" and click on the top option.
   echo ^(3^) Scroll down in the list to find the ^"SERVICE1" and 
   echo       the "SERVICE2".
   echo ^(4^) Right-click on each respective service and select properties.
   echo ^(5^) At the top click on the ^"Log On" tab.
   echo ^(6^) Type in the correct password for the named user and confirm it just below.
   echo ^(7^) Once this is done for the respective service^(s^), run this file again.
   echo.
   set P=1
)
if "%P%" EQU "1" (
   echo.
   echo If any additional errors came up or if this same message comes up after 
   echo following the above steps, be sure to give Tech Support a call at 
   echo 1-800-XXX-XXXX. 
   echo.
)

:FOR DEBUG USE

:echo A^=%A%
:echo B^=%B%
:echo C^=%C%
:echo D^=%D%
:echo E^=%E%
:echo F^=%F%
:echo G^=%G%
:echo H^=%H%
:echo I^=%I%
:echo M^=%M%
:echo N^=%N%
:echo O^=%O%
:echo P^=%P%
:echo S^=%S%

echo To close this window:
pause

提前谢谢特雷弗

主要是函数。你一次又一次地做同样的事情

创建包含服务列表的文件

win32time
wscsvc 
wuauserv"
for /f %%A in (c:\services.txt) do call Main %%A
goto :EOF
开始获取服务列表

win32time
wscsvc 
wuauserv"
for /f %%A in (c:\services.txt) do call Main %%A
goto :EOF
然后,我将主辅助函数的服务名称更改为%1,并在顶部添加了:main,在底部添加了goto:eof。这将被调用三次,上面三个服务中的每一个都被称为%1。不过我还没有检查你的逻辑

:MAIN
for /F "tokens=3 delims=: " %%H in ('sc qc "%1" ^| findstr "        START_TYPE"') do (
if /I "%%H" EQU "DISABLED" (
echo.
echo %1 is disabled!
echo.
set A=1
set C=3
set E=0
set M=1
goto FILE
)
if /I "%%H" NEQ "DISABLED" (
set A=0
set M=0
)

)

:STATUS

for /F "tokens=3 delims=: " %%H in ('sc query "%1" ^| findstr "        STATE"') do (
  if /I "%%H" NEQ "RUNNING" (
  echo.
   echo Starting %1... & SC Start "%1" | findstr /i /c:"1069" && (Echo.
   echo ERROR 1069: %1 failed to start due to a unknown 
   echo username or bad password.
   echo.
   set E=3
   set C=3
    ) || (
   echo Successful
   echo.
   set C=0
   set E=0
    )
  )
  if /I "%%H" EQU "RUNNING" (
   echo.
   echo %1 is already started!
   echo.
   set C=1
   set E=0
  )
)

goto :eof

在编程中,我们不检查某件事,而是检查它是否有效。检查通常需要与执行类似的时间。因此,您使用的电池电量是原来的两倍,大气中的碳排放量是原来的两倍,等等。如果在服务器上,您可能需要一个更大的

这就是你似乎想做的

sc config %1 start= auto 
sc start %1
然后测试错误的密码(1069),如果已经运行,您将得到错误1056