user2664286
user2664286

Reputation: 11

IF STATEMENT for PINGS

I work in a small IT department that has multiple users in other external offices and have made a basic (Windows) PING command batch script that pings each office to check they are online. I simply run the .bat file and leave it in the corner of my screen.

If a ping fails it turns the background red, if it works it stays blue. Here is the content:

@echo off
COLOR 97
:start
echo EXTERNAL CHECK
PING www.google.com
IF %ERRORLEVEL% EQU 0 COLOR 97
IF %ERRORLEVEL% EQU 1 COLOR 47
echo SITE A CHECK
PING x.x.x.x
IF %ERRORLEVEL% EQU 0 COLOR 97
IF %ERRORLEVEL% EQU 1 COLOR 47
echo SITE B CHECK
PING x.x.x.x
IF %ERRORLEVEL% EQU 0 COLOR 97
IF %ERRORLEVEL% EQU 1 COLOR 47
echo SITE C CHECK
PING x.x.x.x
IF %ERRORLEVEL% EQU 0 COLOR 97
IF %ERRORLEVEL% EQU 1 COLOR 47
echo SITE D CHECK
PING x.x.x.x
IF %ERRORLEVEL% EQU 0 COLOR 97
IF %ERRORLEVEL% EQU 1 COLOR 47
goto start

I am pretty new to scripting so apologies for any stupid errors. If anyone has any suggestions to improve it I would welcome them. I hope this helps people though! Rob

Upvotes: 0

Views: 13231

Answers (2)

Robbie P
Robbie P

Reputation: 399

Since most newer Windows version include PowerShell, you can incorporate single line PowerShell command into your batch, or whatever to change the color of a single line. My example:

@ECHO OFF
FOR %%i IN (
     Server1
     Server2
     any other URL
) DO (
     PowerShell -NoProfile -Command "If (Test-Connection %%i -Count 1 -Quiet) { Write-Host "%%i - successfully pinged" -F Green } else { Write-Host "%%i FAILED" -F Red}
)

Hope this works!

Upvotes: 0

gariel
gariel

Reputation: 687

Try:

@echo off
:color 97

:start
echo EXTERNAL CHECK 
PING www.google.com 
call :color

echo SITE A CHECK 
PING www.yahoo.com
call :color

echo SITE B CHECK 
PING 127.0.0.1
call :color

echo SITE C CHECK 
PING www.nowhere.com.fail
call :color 

echo SITE D CHECK 
PING www.failfail.com.fail
call :color
goto start

:color
    IF %ERRORLEVEL% EQU 0 (
        COLOR 97
    ) else (
        COLOR 47
    )
    GOTO:EOF

Upvotes: 2

Related Questions