0

Here is my pseudo:

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

This is driving me bonkers here... I can make the parts of this code work alone just fine, but when I combine it into a nested IF structure, it returns 'Network Error' and 'Running' if a machine does not have the service running...then a manual check at that machine shows it is still actually stopped. Same for machines without the service installed at all.Please tell me where I'm messing up.. Thanks!

@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 Answers1

1

this should fix the issue

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

part of the issue is that you have to test for if not %1="" at the top of the script. that is where you get the erroneous text..

then you had some nesting issues, so i used ELSE instead.

you will have to change the text.txt to your filename.exe (and omit the "type" command)

please check answer if this solves your problem

fyi, you will need to have administrator access on the remote machine so that you can turn on and off services

Doctor DOS
  • 263
  • 1
  • 2
  • 10
  • Thanks, will try that. But also, at a glance... the xcopy and psexec part should only run if it fails finding the service running, or stopped. {If service != running} AND {service != stopped}, then the app must not be installed, copy over and install it. The example you give looks like it will install if it's not running, but does not check if it's just stopped... – Just a Caffinated machine Apr 20 '12 at 17:18
  • Ok, tried that. It runs the first computer through. Then closes the batch file...out put shows running, as the first test machine is running... – Just a Caffinated machine Apr 20 '12 at 17:32
  • Alrighty. Using your example, I was able to get my original script in a working form! Looks like there was a ) missing. Also incorporated my install if not running. Thanks alot! – Just a Caffinated machine Apr 20 '12 at 19:37