0

这是我的伪:

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 结构时,如果机器没有,它会返回“网络错误”和“运行”服务正在运行...然后在该机器上进行手动检查显示它实际上仍处于停止状态。对于根本没有安装服务的机器也是如此。请告诉我我在哪里搞砸了..谢谢!

@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""
        )
    )
)
4

1 回答 1

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”命令)

如果这能解决您的问题,请检查答案

仅供参考,您需要在远程计算机上具有管理员访问权限,以便您可以打开和关闭服务

于 2012-04-20T16:28:17.820 回答