远程服务检查批处理文件不能正常工作

时间:2012-04-20 03:27:24

标签: service batch-file psexec

这是我的伪:

Read from LIST
Ping to check if machine is awake | Report result
Check to see if SERVICE is running | Report result
  NO? Is it stopped?
    YES? Run it | Report result
    NO? Must not be installed, remote install please! | Report result

这让我疯狂......我可以让这段代码单独工作就好了,但当我把它组合成一个嵌套的IF结构时,如果一台机器那么它会返回'Network Error'和'Running'没有运行服务...然后在该机器上进行手动检查显示它仍然实际上已停止。对于没有安装服务的机器也是如此。请告诉我在哪里搞砸了......谢谢!

@echo off
echo.
echo.
cls
set SERVICE=MyServ


for /f %%i in (\\127.0.0.1\c$\list.txt) do call :DOIT %%i




:DOIT
echo Checking %SERVICE% on %1
@ECHO off
ping -n 2 -w 1000 %1 >trial.txt
find "Reply from" trial.txt>nul
if %ERRORLEVEL% == 1 (echo %1 Network Unresponsive>> "\\127.0.0.1\c$\list_report.txt")
if %ERRORLEVEL% == 0 (
    sc \\%1 query %SERVICE% | FIND "STATE" | FIND "RUNNING"
    if %ERRORLEVEL% == 0 (echo %1 Running>> "\\127.0.0.1\c$\list_report.txt")
    if not %ERRORLEVEL% == 0 (
        sc \\%1 query %SERVICE% | FIND "STATE" | FIND "STOPPED"
        if %ERRORLEVEL% == 0 (
            sc \\%1 start %SERVICE%
            echo %1 forced start>> "\\127.0.0.1\c$\list_report.txt""
        )
        if not %ERRORLEVEL% == 0 (
            xcopy /f /y "\\127.0.0.1\!\theAPP.exe" "\\%1\c$\temp\"
            \\127.0.0.1\!\psexec \\%1 -s -n 10 -d cmd /c "c:\temp\theAPP.exe"
            echo %1 Installed>> "\\127.0.0.1\c$\list_report.txt""
        )
    )
)

1 个答案:

答案 0 :(得分:1)

这应解决问题

    for /f %%i in (\\127.0.0.1\c$\list.txt) do call :DOIT %%i

:DOIT
    if not "%1"=="" (
        echo Checking %SERVICE% on %1
        echo.
        ping -n 2 -w 1000 %1 | find "Reply from" >nul
            if ERRORLEVEL  1 echo %1 Network Unresponsive>> \\127.0.0.1\c$\list_report.txt else (
                sc \\%1 query %SERVICE% | find "RUNNING" > nul
                    if ERRORLEVEL 1 (
                            sc \\%1 start %SERVICE% > nul
                            echo %1 Forced Start>> \\127.0.0.1\c$\list_report.txt                   
                    )  else  (
                        echo %1 Running>> \\127.0.0.1\c$\list_report.txt)
                    xcopy /f /y \\127.0.0.1\c$\text.txt \\%1\c$\temp\ > nul
                    \\127.0.0.1\c$\psexec \\%1 -s -n 10 -d cmd /c "type c:\temp\text.txt"
                    echo %1 Installed>> "\\127.0.0.1\c$\list_report.txt")
            )
    )

问题的一部分是你必须在脚本顶部测试if not%1 =“”。这就是你得到错误文本的地方..

然后你遇到了一些嵌套问题,所以我改用了ELSE。

您必须将text.txt更改为您的filename.exe(并省略“type”命令)

请检查答案是否能解决您的问题

fyi,您需要在远程计算机上拥有管理员权限才能打开和关闭服务