Link to home
Start Free TrialLog in
Avatar of noodleNT
noodleNT

asked on

Batch file - Check if Machine is online first

I have a list of computers dumpted from AD. I then call that up that file and want to test if the machine is online or not. I was trying to use Net View \\Computer but would get an error 53 if the machine wasn't online. Is there any way I can do a simple test for that?

If error = 53 then Set Online=False

Something like that.

This is my current code:
*************************************************

For /F "delims=" %%i in (%FileInput%) do (
      Echo Computer: %%i
      set /a LineCount=+1
      Echo LineCount: %LineCount%

      ECHO. >>C:\Scripts\xcacls\LOG_MSFIX.txt
      ECHO. >>C:\Scripts\xcacls\LOG_MSFIX.txt
      ECHO ******************************************************** >>C:\Scripts\xcacls\LOG_MSFIX.txt
      ECHO COMPUTER: %%i >>C:\Scripts\xcacls\LOG_MSFIX.txt
      ECHO ******************************************************** >>C:\Scripts\xcacls\LOG_MSFIX.txt

      
      For /F "Tokens=1" %%a in ('Net view "\\%%i"') do set Online=%%a      
                  
      Echo Online="%online%"

      IF "%Online%" == "error" (
            ECHO %%i did not respond at %Time% >>C:\Scripts\xcacls\LOG_MSFIX.txt
      ) Else (
            ECHO %%i ALIVE! at %Time% >>C:\Scripts\xcacls\LOG_MSFIX.txt
        )
)
ASKER CERTIFIED SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of noodleNT
noodleNT

ASKER

Dont work, it always returns error of 0. This is the return I get.

Computer: Comp-XP2
LineCount: 1
Ping request could not find host Comp-XP2. Please check the name and try a
gain.
Error: 0
Press any key to continue . . .


Code:

      ping %%i -n 1 -w 100
      Echo Error: %errorlevel%
      pause
Well, I just ran a few tests and this works exactly as I expected it would.

I suggest you might have problems with DNS if your ping of a name fails.  In an Active Directory environment all your workstations should be in AD DNS and hence pinging a name should work fine.

In my tests, REPLY always got a 0 and timed out and not found got a 1 (as error level) this is on XP SP2.
These are all laptops. So they may be at the office or not. If they are not on the network DNS will not be able to resolve their names... because the don't exist.
DNS doesn't just drop their names if they aren't on the network.  My laptop hasn't been connected to my network since Thursday, but it was what I tested the "request timed out" with  Unless you've modified your DNS to get rid of "old" entries VERY quickly.

Thing is, if you are able to "NET VIEW" them, they should be in DNS long enough to resolve.  
I have the same problem with net view.


I am now dumping the resolts to a txt file. I then use find to test for the string "could not find host". This works but not sure how to set variables off of it.


Something like this???
      For /F "delims= Tokens=2" %%a in ('Find /C "could not find host" C:\Scripts\xcacls\Ping_Test.txt') do (
            Echo A=%%a
            Set online=%%a
      )
Got it to work...

@Echo Off

ECHO ****************************
ECHO ***    FIX MS PATCH     ****
ECHO ***      v 2.0          ****
ECHO ****************************

:FilePrompt
Echo Enter the name of the saved text file with the computer names.
Set /P FileInput=Input Source:

For /F "delims=. Tokens=1,2" %%a in ("%FileInput%") Do (
      Set FileName=%%a
      Set Filetype=%%b
)

If "%Filetype%"=="" (
      Echo Please type the full name of the file.
      Goto :FilePrompt
)


:PassPrompt
Echo Please enter the Domain\Admin password.
Set /P DomPass=Domain Admin Password:

If "%DomPass%"=="" (
      ECHO Please enter password.
      Goto :PassPrompt
)




type %FileInput% > FileFix.reg
type FileFix.reg > %FileInput%
Del /Q FileFix.reg


For /F "delims=" %%i in (%FileInput%) do Call :Online %%i
Goto :EOF

:ONline
set Machine=%~1
set Machine=%Machine: =%
set RESULT=

Echo Computer:%Machine%
CD C:\Scripts\xcacls      
If NOT "%Machine%" == "" (      

      ECHO. >>C:\Scripts\xcacls\LOG_MSFIX.txt
      ECHO. >>C:\Scripts\xcacls\LOG_MSFIX.txt
      ECHO ******************************************************** >>C:\Scripts\xcacls\LOG_MSFIX.txt
      ECHO COMPUTER: %Machine% >>C:\Scripts\xcacls\LOG_MSFIX.txt
      ECHO ******************************************************** >>C:\Scripts\xcacls\LOG_MSFIX.txt


      REM ping %Machine% -n 4 -w 100 >Ping_Test.txt
      
      REM findstr /C:"find" Ping_Test.txt

      
      
      ping -w 1000 %MACHINE% |findstr /r /c:"Reply from" 1>NUL 2>NUL
      if errorlevel 1 goto Return_False
      if errorlevel 0 goto Return_True
      
)

:Return_True
set RESULT=Successful Ping
Goto Run_Xcacls

:Return_False
Set RESULT=Failed to Connect
Goto Run_Xcacls


:Run_Xcacls

Echo Result:%Result%
      
if NOT "%RESULT%" == "Successful Ping" (
      ECHO %MAchine% did not respond at %Time% >>C:\Scripts\xcacls\LOG_MSFIX.txt
) Else (
      ECHO %Machine% ALIVE! at %Time% >>C:\Scripts\xcacls\LOG_MSFIX.txt
      cscript.exe /h:cscript            
            
      Xcacls \\%MAchine%\c$\Windows\registration /G everyone:R system:F administrators:F /user Domain\admin /pass %DomPass% /L C:\Scripts\xcacls\LOG_MSFIX.txt
      Xcacls \\%Machine%\c$\Windows\registration\*.clb /G everyone:R system:F administrators:F /user Domain\admin /pass %DomPass% /L C:\Scripts\xcacls\LOG_MSFIX.txt
)



:EOF


leew got me on the right track... let him get the points.
PING -n 1 192.168.1.1|find "Reply from" >NUL
IF NOT ERRORLEVEL 1 goto :ONLINE
IF     ERRORLEVEL 1 goto :OFFLINE

:OFFLINE
ECHO OFFLINE
GOTO END

:ONLINE
ECHO ONLINE
goto END

:END
pause
I got it to work without the "find" (it is not reliable because ping results are different in different languages and on different Windows version):

SETLOCAL ENABLEDELAYEDEXPANSION
set ComputerToInstallOn=SomeComputerName
ping !ComputerToInstallOn! -n 1 -w 300 > nul

IF ERRORLEVEL 1 (
    ECHO Computer offline
) ELSE (
    ECHO Computer online
)
pause