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

时间:2014-08-21 21:07:29

标签: 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

: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

提前致谢! - 特雷弗

2 个答案:

答案 0 :(得分:0)

校长是职能。你一遍又一遍地做着同样的事情。

创建一个包含服务列表的文件。

win32time
wscsvc 
wuauserv"

开始获取服务列表。

for /f %%A in (c:\services.txt) do call Main %%A
goto :EOF

然后主要的工作者函数我刚刚将服务名称更改为%1并添加:main at top和goto:eof at bottom。这将被称为三次,其中三个服务中的每一个都作为%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

答案 1 :(得分:0)

在编程中我们不检查然后做什么,然后检查它是否有效。通常需要花费相同的时间进行检查。因此,您使用两倍的电池电量并在大气中放置两倍的碳等。如果在服务器上,您可能需要更大的电池。

这就是你想要做的事情。

sc config %1 start= auto 
sc start %1

然后测试错误密码(1069),如果已经运行,则会收到错误1056.