Link to home
Start Free TrialLog in
Avatar of samiam41
samiam41Flag for United States of America

asked on

Add new function to trouble-shooting script

Hi Experts.  I have a script that I use for trouble-shooting that I need to modify.  I'd like to show an error message (%Computer% isn't online or pingable) and put it in the log file.  Lastly, the script doesn't continue.

Here is the script currently:

@echo off
setlocal

set /p "Computer=Please enter computer name: "
if "%Computer%" EQU "" (
  echo No computer name entered, ending.
  exit /b
)

set logfile=\\server\public\FG_Diagnostic\%computer%.log

echo ----[Diagnostic Run Time]----> %logfile%
echo %date:~4,2%/%date:~7,2%/%date:~10,4%>> %logfile%
echo %time:~0,2%:%time:~3,2%>> %logfile%
echo. >> %logfile%

echo ----[Computer Info]---->> %logfile%
echo %Computer% >> %logfile%
ping -n 2 %computer%>> %logfile%
echo. >> %logfile%

echo ----[Forward nslookup of IP '%Computer%']---->> %logfile%
nslookup.exe %Computer% >>"%LogFile%" 2>&1
set IP=
for /f "tokens=2 delims=[]" %%a in ('ping.exe -n 2 %computer% ^| find.exe /i "Pinging %computer%"') do set IP=%%a

Open in new window


Thanks for your help Experts!!
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

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 samiam41

ASKER

Hey oBdA!  

You have the right idea as far as what I am looking for.  My goal is to verify that the computer is online before the rest of the script continues on.  If the computer can't be pinged, I want to stop the script and output that the computer is offline or can't be pinged.

Here is what I have which is formatted a little differently than what you posted.

echo ----[Diagnostic Run Time]----> %logfile%
echo %date:~4,2%/%date:~7,2%/%date:~10,4%>> %logfile%
echo %time:~0,2%:%time:~3,2%>> %logfile%
echo. >> %logfile%

echo ----[Computer Online Verification]---->>"%logfile%" 
ping.exe -4 -n 2 %Computer% | find.exe /i "TTL" >NUL
if errorlevel 1 (
	>>"%logfile%" %Computer% isn't online or pingable
	exit /b 1
)

echo ----[Computer Info]---->> %logfile%
echo %Computer% >> %logfile%
ping -n 2 %computer%>> %logfile%
echo. >> %logfile%

Open in new window


The output of the logfile shows a blank entry under the ----[Computer Online Verification]---- label.  The logfile is not showing what it should and then the script closes out.

I need the %logfile% to be opened when the script ends under this portion (please).
Ok.  Disregard.  I made this modification to your script.  Here's what I used and it worked.


echo ----[Computer Online Verification]---->>"%logfile%"
ping.exe -4 -n 2 %Computer% | find.exe /i "TTL" >NUL
if errorlevel 0 (
      echo %Computer% isn't online or pingable >>"%logfile%"
      start notepad %logfile%
      exit /b 1
)

I think I was missing the echo portion.
Thanks as always!  You're brilliant.
Avatar of oBdA
oBdA

To maybe elaborate on my changes ...
Just for fun, paste the following directly into a command prompt:
set Index=2
echo Processing %Index%>test.log

Open in new window

Want to guess what the content of test.log will be?
Hover here for the solution ...

Having the redirection at the beginning prevents these issues without always having a trailing space at the end (or having to put the whole echo command in brackets).
Thank you for explaining that.  I knew there was a reason you used it that way and it makes sense.  From here on out, I will format my scripts accordingly (the oBdA way!)

I did like the link and message you encoded in there.  Very slick.
If anyone would like some points, I would appreciate your help!

https://www.experts-exchange.com/questions/28593187/Clean-up-log-file-from-batch-script.html