FOR / F批处理文件循环未运行

时间:2014-05-08 14:44:46

标签: windows batch-file for-loop service remote-desktop

我正在尝试创建这个批处理文件,我希望它检查一个远程系统的服务,如果它有服务检查,以确保它正在运行,如果它没有运行启动服务;如果未安装该服务,请安装它。我有一个系统基于系统的工作。我想做的是这样我可以使用FOR / F命令从列表中绘制要测试的系统名称

这是我到目前为止所拥有的。

    @echo off

    SET InputFile=C:\Users\locationoffile\complist.txt

    :FOR

    FOR /F "delims= " %%i IN (%InputFile%) DO SET comp=%%i

    :PING

    ECHO %comp%

    ping -n 1 %comp% | FIND "TTL="

    IF ERRORLEVEL 1 GOTO :FAILED 

    IF ERRORLEVEL 0 GOTO :SERVICECHECK

    :SERVICECHECK

    SC \\%comp% QUERY ServiceName > Null
    IF ERRORLEVEL 1060 GOTO INSTALL
    ECHO EXISTS
    GOTO STATE

    :STATE

    ECHO Checking Service State

    sc \\%comp% query ServiceName | findstr /i "STATE"

    IF ERRORLEVEL 0 GOTO :START

    IF ERRORLEVEL 1 GOTO :EOF

    :START

    ECHO Starting Service

    psexec \\%comp% net start ServiceName

    GOTO :FOR

    :INSTALL

    ECHO Installing ServiceName

    psexec.exe -s \\%comp% \\locationoffile/filename.exe /S /v /qn

    GOTO :FOR

    :FAILED

    ECHO Failed to connect

    GOTO :FOR

这会从列表中拉出第一个行项目,然后停止批处理,然后停止,它不会移动到批次刚刚结束的列表中的下一个名称。这是原始的“工作”批处理文件。

    @echo off

    ECHO Enter Computer Name
    set /p comp=

    ping -n 1 %comp% | FIND "TTL="

    IF ERRORLEVEL 1 GOTO :FAILED

    IF ERRORLEVEL 0 GOTO :SERVICECHECK

    :SERVICECHECK
    SC \\%comp% QUERY ServiceName > Null
    IF ERRORLEVEL 1060 GOTO INSTALL
    ECHO EXISTS
    GOTO STATE

    :STATE
    ECHO Checking Service State

    sc \\%comp% query ServiceName | findstr /i "STATE"

    IF ERRORLEVEL 0 GOTO :START

    IF ERRORLEVEL 1 GOTO :EOF

    :START
    ECHO Starting Service

    psexec \\%comp% net start ServiceName

    GOTO :EOF

    :INSTALL

    ECHO Installing Service Name

    psexec.exe -s \\%comp% \\locationoffile/filename.exe /S /v /qn

    GOTO EOF

    :FAILED

    ECHO Failed to connect

    :EOF

    ECHO END

2 个答案:

答案 0 :(得分:0)

正确使用for循环是:

for ..... do (
  a command
  another command
)

但注意:任何goto命令都会破坏它!

以下是我将如何处理(call技巧解决goto问题):

set inputfile=C:\Users\locationoffile\complist.txt
for /f "delims= " %%i in (%inputfile%) do call :doit %%i
echo finihed
exit /b

:doit
echo %1
ping -n 1 %1 |find "TTL" && goto :servicecheck || goto :failed
goto :eof

:failed
echo %1 failed
goto :eof

:servicecheck
SC \\%1 QUERY ServiceName > Null
IF ERRORLEVEL 1060 GOTO INSTALL
ECHO %1 EXISTS; Checking Service State
sc \\%1 query ServiceName | findstr /i "STATE" 
goto :eof

&&的作用类似于"如果没有错误",||喜欢"如果错误"

答案 1 :(得分:0)

OP解决方案。

以下将允许您检查系统的服务(如果未安装),安装它,如果安装它,它会检查并确保它运行它从外部列表中提取系统名称。

    @echo off

    echo Service Check STARTED on %date% at %time% 

    echo =================================================

    echo.

    setlocal EnableExtensions EnableDelayedExpansion

    set count=1

    set fnm=C:\Users\filelocation\complist1.txt

    for /f "tokens=*" %%i in (%fnm%) do (set comp=%%i

    call :SUB

    )

    :SUB

     echo !count!%1

     set /a count+=1

    Echo !comp!

    ping -n 1 %comp% | FIND "TTL="

    IF ERRORLEVEL 1 GOTO :FAILED

    SC \\!comp! QUERY <servicename> > Null

    IF ERRORLEVEL 1060 GOTO INSTALL

    ECHO <Service Name> is Installed

    GOTO STATE

    :STATE

    ECHO Checking Service State

    for /f "tokens=4" %%a in ('sc \\!comp! query <servicename> ^| findstr 

    STATE') do set CmdResult=%%a

    ECHO <Service Name> Service is !CmdResult!

    if !CmdResult!==STOPPED (

    GOTO :START

    )

    GOTO :END

    :START

    ECHO Starting Service

    psexec \\!comp! net start <servicename>

    GOTO :END

    :INSTALL

    ECHO <Service Name> is not Installed

    ECHO Installing <Service Name>

    psexec.exe -s \\!comp! \\serviceinstalllocation\filename.exe /S /v /qn

    GOTO END

    :FAILED

    ECHO Failed to connect

    :END