Solved

Add new function to trouble-shooting script

Posted on 2015-01-07
7
155 Views
Last Modified: 2015-01-08
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!!
0
Comment
Question by:samiam41
  • 5
  • 2
7 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 40536378
It's not quite clear what you mean; something like this?
@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

 >"%logfile%" 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%

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

>>"%logfile%" echo ----[Computer Info]----
ping.exe -4 -n 2 %Computer% >>"%logfile%"
>>"%logfile%" echo.

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

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
ID: 40536418
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).
0
 
LVL 9

Author Comment

by:samiam41
ID: 40536428
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.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 9

Author Closing Comment

by:samiam41
ID: 40536429
Thanks as always!  You're brilliant.
0
 
LVL 83

Expert Comment

by:oBdA
ID: 40536477
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).
0
 
LVL 9

Author Comment

by:samiam41
ID: 40536497
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.
0
 
LVL 9

Author Comment

by:samiam41
ID: 40538623
If anyone would like some points, I would appreciate your help!

http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_28593187.html
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

First some basics on Windows 7 Backup.  It has 2 components one is a file based backup which is stored in .zip files each zip is split at around 200 Megabytes and there is the Image Backup which is as the name implies a total image of the partition …
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now