Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Service 远程服务检查批处理文件不太正常_Service_Batch File_Psexec - Fatal编程技术网

Service 远程服务检查批处理文件不太正常

Service 远程服务检查批处理文件不太正常,service,batch-file,psexec,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 这

以下是我的建议:

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结构时,如果一台机器没有运行服务,它会返回“networkerror”和“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""
        )
    )
)

这应该可以解决问题

    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=”“。这就是你得到错误文本的地方

然后你有一些嵌套问题,所以我用了其他的

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

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


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

清理代码。。有人看到这个问题吗?谢谢,我会试试的。而且,乍一看。。。只有当xcopy和psexec部分无法找到正在运行的服务或已停止时,才应运行该部分。{如果service!=running}和{service!=stopped},则不得安装该应用程序,请复制并安装它。您给出的示例看起来好像它将在不运行时安装,但不会检查它是否刚刚停止…好的,尝试一下。它运行第一台计算机。然后关闭批处理文件…输出显示正在运行,因为第一台测试机器正在运行…正确。用你的例子,我可以得到我的原始脚本的工作形式!看起来有一个)失踪。如果未运行,也会合并我的安装。非常感谢!