Solved

Add new function to trouble-shooting script

Posted on 2015-01-07
7
159 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 84

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 9

Author Closing Comment

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

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

856 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